import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/radiology/final_radiology.dart';
import 'package:diplomaticquarterapp/core/viewModels/medical/radiology_view_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/buttons/button.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/dialogs/confirm_send_email_dialog.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';

class RadiologyDetailsPage extends StatelessWidget {
  final FinalRadiology finalRadiology;

  RadiologyDetailsPage({Key key, this.finalRadiology});

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    return BaseView<RadiologyViewModel>(
      onModelReady: (model) => model.getRadImageURL(
          projectId: finalRadiology.projectID,
          lineItem: finalRadiology.invoiceLineItemNo,
          invoiceNo: finalRadiology.invoiceNo),
      builder: (_, model, widget) => AppScaffold(
          appBarTitle: TranslationBase.of(context).report,
          isShowAppBar: true,
          baseViewModel: model,
          body: SingleChildScrollView(
            child: Column(
              mainAxisSize: MainAxisSize.max,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Texts(
                  '${finalRadiology.reportData}',
                  textAlign: TextAlign.center,
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Texts(
                    '${finalRadiology.reportData}',
                    textAlign: TextAlign.start,
                    fontSize: 17,
                  ),
                ),
                SizedBox(
                  height: MediaQuery.of(context).size.height * 0.2,
                )
              ],
            ),
          ),
          bottomSheet: (projectViewModel.havePrivilege(9) ||
                  projectViewModel.havePrivilege(8))
              ? Container(
                  width: double.infinity,
                  height: finalRadiology.dIAPACSURL != ""
                      ? MediaQuery.of(context).size.height * 0.25
                      : MediaQuery.of(context).size.height * 0.14,
                  color: Colors.grey[100],
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Divider(),
                      if (finalRadiology.dIAPACSURL != "" &&
                          projectViewModel.havePrivilege(9))
                        Container(
                          width: MediaQuery.of(context).size.width * 0.8,
                          child: Button(
                            onTap: () {
                              launch(model.radImageURL);
                            },
                            label: TranslationBase.of(context).openRad,
                            backgroundColor: Colors.grey[800],
                          ),
                        ),
                      if (projectViewModel.havePrivilege(8))
                        Container(
                          width: MediaQuery.of(context).size.width * 0.8,
                          child: Button(
                            onTap: () {
                              showConfirmMessage(
                                  finalRadiology: finalRadiology, model: model);
                            },
                            label: TranslationBase.of(context).sendCopyRad,
                            loading: model.state == ViewState.BusyLocal,
                            backgroundColor: Theme.of(context).primaryColor,
                          ),
                        )
                    ],
                  ),
                )
              : Container()),
    );
  }

  void showConfirmMessage(
      {FinalRadiology finalRadiology, RadiologyViewModel model}) {
    showDialog(
      context: AppGlobal.context,
      child: ConfirmSendEmailDialog(
        email: model.user.emailAddress,
        onTapSendEmail: () {
          model.sendRadReportEmail(
              mes: TranslationBase.of(AppGlobal.context).sendSuc,
              finalRadiology: finalRadiology);
        },
      ),
    );
  }
}
