import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/core/model/radiology/final_radiology.dart';
import 'package:hmg_patient_app/core/viewModels/medical/radiology_view_model.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/models/Authentication/authenticated_user.dart';
import 'package:hmg_patient_app/models/header_model.dart';
import 'package:hmg_patient_app/pages/base/base_view.dart';
import 'package:hmg_patient_app/uitl/gif_loader_dialog_utils.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils.dart';
import 'package:hmg_patient_app/uitl/utils_new.dart';
import 'package:hmg_patient_app/widgets/buttons/defaultButton.dart';
import 'package:hmg_patient_app/widgets/dialogs/confirm_send_email_dialog.dart';
import 'package:hmg_patient_app/widgets/new_design/doctor_header.dart';
import 'package:hmg_patient_app/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;
  bool? isRadiologyVidaPlus;

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

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    return BaseView<RadiologyViewModel>(
      onModelReady: (model) => model.getRadImageURL(
          projectId: finalRadiology!.projectID!,
          invoiceType: finalRadiology!.invoiceType,
          lineItem: finalRadiology!.invoiceLineItemNo!,
          invoiceNo: Utils.isVidaPlusProject(projectViewModel, finalRadiology!.projectID!) ? finalRadiology!.invoiceNo_VP : finalRadiology!.invoiceNo,
          isVidaPlus: Utils.isVidaPlusProject(projectViewModel, finalRadiology!.projectID!),
          examId: finalRadiology!.exam_Id ?? ""),
      builder: (_, model, widget) => AppScaffold(
        appBarTitle: TranslationBase.of(context).report,
        isShowAppBar: true,
        baseViewModel: model,
        showNewAppBar: true,
        showNewAppBarTitle: true,
        backgroundColor: Color(0xffF8F8F8),
        body: SingleChildScrollView(
          child: Column(
            mainAxisSize: MainAxisSize.max,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              DoctorHeader(
                headerModel: new HeaderModel(
                  finalRadiology!.doctorName!,
                  finalRadiology!.doctorID!,
                  finalRadiology!.doctorImageURL!,
                  finalRadiology!.speciality ?? [""],
                  Utils.isVidaPlusProject(projectViewModel, finalRadiology!.projectID!) ? finalRadiology!.invoiceNo_VP.toString() : finalRadiology!.invoiceNo.toString(),
                  finalRadiology!.projectName!,
                  finalRadiology!.orderDate,
                  null,
                  finalRadiology!.nationalityFlagURL ?? "",
                  finalRadiology!.doctorRate,
                  finalRadiology!.actualDoctorRate,
                  finalRadiology!.noOfPatientsRate,
                  projectViewModel.user.emailAddress!,
                ),
                onTap: () async {
                  GifLoaderDialogUtils.showMyDialog(context);
                  await model.sendRadReportEmail(mes: TranslationBase.of(AppGlobal.context).sendSuc, finalRadiology: finalRadiology!, userObj: projectViewModel.user, isDownload: true);
                  GifLoaderDialogUtils.hideDialog(context);
                  // showConfirmMessage(finalRadiology: finalRadiology!, model: model, userObj: projectViewModel.user!, isDownload: true);
                },
                buttonTitle: TranslationBase.of(context).download,
                buttonIcon: "assets/images/new/download_1.svg",
                showConfirmMessageDialog: false,
                isDownload: true,
                isNeedToShowButton: projectViewModel.havePrivilege(8),
                // showConfirmMessageDialog: false,
              ),
              Container(
                margin: EdgeInsets.all(24),
                decoration: cardRadius(12),
                child: Container(
                  width: double.infinity,
                  child: Padding(
                    padding: const EdgeInsets.all(12.0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Text(
                          "General Result",
                          style: TextStyle(
                            fontSize: 16,
                            letterSpacing: -0.64,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                        mHeight(10),
                        Text(
                          '${finalRadiology!.reportData}',
                          style: TextStyle(fontSize: 12, letterSpacing: -0.48),
                        ),
                        mHeight(16),
                        (projectViewModel.havePrivilege(9) || projectViewModel.havePrivilege(8))
                            ? (finalRadiology!.dIAPACSURL != "" && projectViewModel.havePrivilege(9))
                                ? Container(
                                    child: DefaultButton(
                                      TranslationBase.of(context).openRad,
                                      () => {launch(model.radImageURL)},
                                      textColor: Colors.white,
                                    ),
                                  )
                                : Container()
                            : Container(),
                      ],
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void showConfirmMessage({FinalRadiology? finalRadiology, RadiologyViewModel? model, AuthenticatedUser? userObj, required bool isDownload}) {
    showDialog(
      context: AppGlobal.context,
      builder: (cxt) => ConfirmSendEmailDialog(
        email: model!.user!.emailAddress,
        onTapSendEmail: () {
          model.sendRadReportEmail(mes: TranslationBase.of(AppGlobal.context).sendSuc, finalRadiology: finalRadiology!, userObj: userObj!, isDownload: isDownload);
        },
      ),
    );
  }
}
