import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/viewModel/PatientMedicalReportViewModel.dart';
import 'package:doctor_app_flutter/core/model/patient/MedicalReport/MeidcalReportModel.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/utils/dr_app_toast_msg.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/buttons/app_buttons_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/loader/gif_loader_dialog_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/text_fields/html_rich_editor.dart';
import 'package:flutter/material.dart';
import 'package:html_editor_enhanced/html_editor.dart';
import 'package:permission_handler/permission_handler.dart';

import '../../../../config/config.dart';

class AddVerifyMedicalReport extends StatefulWidget {
  final PatiantInformtion? patient;
  final String? patientType;
  final String? arrivalType;
  final MedicalReportModel? medicalReport;
  final PatientMedicalReportViewModel? model;
  final MedicalReportStatus? status;
  final String? medicalNote;
  final bool? isVidaPlusProject;

  const AddVerifyMedicalReport({Key? key, this.patient, this.patientType, this.arrivalType, this.medicalReport, this.model, this.status, this.medicalNote, this.isVidaPlusProject}) : super(key: key);

  @override
  _AddVerifyMedicalReportState createState() => _AddVerifyMedicalReportState();
}

class _AddVerifyMedicalReportState extends State<AddVerifyMedicalReport> {
  HtmlEditorController _controller = HtmlEditorController();

  @override
  Widget build(BuildContext context) {
    String txtOfMedicalReport;

    return BaseView<PatientMedicalReportViewModel>(
        onModelReady: (model) async {
          model.getMedicalReportTemplate();
        },
        builder: (_, model, w) => AppScaffold(
              baseViewModel: model,
              isShowAppBar: true,
              appBarTitle: widget.status == MedicalReportStatus.ADD ? TranslationBase.of(context).medicalReportAdd : TranslationBase.of(context).medicalReportVerify,
              backgroundColor: Theme.of(context).scaffoldBackgroundColor,
              body: Column(
                children: [
                  Expanded(
                    child: Container(
                      margin: EdgeInsets.all(16),
                      child: Column(
                        children: [
                          Expanded(
                            child: SingleChildScrollView(
                              child: Container(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    if (model.medicalReportTemplate.length > 0)
                                      HtmlRichEditor(
                                        initialText: (widget.medicalReport != null
                                            ? widget.medicalNote!
                                            : widget.model!.medicalReportTemplate[0].templateText!.length > 0
                                                ? widget.model!.medicalReportTemplate[0].templateText!
                                                : ""),
                                        controller: _controller,
                                        hint: "Write the medical report ",
                                        height: MediaQuery.of(context).size.height * 0.75,
                                      ),
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.all(16.0),
                    color: Colors.white,
                    child: Row(
                      children: [
                        Expanded(
                          child: AppButton(
                            title: widget.status == MedicalReportStatus.ADD ? TranslationBase.of(context).save : TranslationBase.of(context).save,
                            color: Color(0xffEAEAEA),
                            fontColor: Colors.black,
                            // disabled: progressNoteController.text.isEmpty,
                            fontWeight: FontWeight.w700,
                            onPressed: () async {
                              txtOfMedicalReport = (await _controller.getText())!;

                              if (txtOfMedicalReport.isNotEmpty) {
                                GifLoaderDialogUtils.showMyDialog(context);
                                widget.medicalReport != null
                                    ? await widget.model!.updateMedicalReport(widget.patient!, txtOfMedicalReport, widget.medicalReport != null ? widget.medicalReport!.lineItemNo??0: 0,
                                        widget.medicalReport != null ? widget.medicalReport!.invoiceNo??"" : "", widget.isVidaPlusProject, widget.medicalReport?.summaryId, widget.medicalReport!)
                                    : await widget.model!.addMedicalReport(widget.patient!, txtOfMedicalReport, widget.isVidaPlusProject);
                                //model.getMedicalReportList(patient);

                                Navigator.pop(context);

                                GifLoaderDialogUtils.hideDialog(context);
                                if (widget.model!.state == ViewState.ErrorLocal) {
                                  DrAppToastMsg.showErrorToast(widget.model!.error);
                                }
                              } else {
                                DrAppToastMsg.showErrorToast("Please enter medical note");
                              }
                            },
                          ),
                        ),
                        SizedBox(
                          width: 8,
                        ),
                        if (widget.medicalReport != null)
                          Expanded(
                            child: AppButton(
                              title: widget.status == MedicalReportStatus.ADD ? TranslationBase.of(context).add : TranslationBase.of(context).verify,
                              color: AppGlobal.appGreenColor,
                              fontWeight: FontWeight.w700,
                              onPressed: () async {
                                txtOfMedicalReport = (await _controller.getText())!;
                                if (txtOfMedicalReport.isNotEmpty) {
                                  GifLoaderDialogUtils.showMyDialog(context);
                                  print("isVidaPlusProject: ${widget.isVidaPlusProject}");
                                  await widget.model!.verifyMedicalReport(widget.patient!, widget.medicalReport!, widget.isVidaPlusProject);
                                  GifLoaderDialogUtils.hideDialog(context);
                                  Navigator.pop(context);
                                  if (widget.model!.state == ViewState.ErrorLocal) {
                                    DrAppToastMsg.showErrorToast(widget.model!.error);
                                  }
                                } else {
                                  DrAppToastMsg.showErrorToast("Please enter medical note");
                                }
                              },
                            ),
                          ),
                      ],
                    ),
                  ),
                ],
              ),
            ));
  }

  void requestPermissions() async {
    Map<Permission, PermissionStatus> statuses = await [
      Permission.microphone,
    ].request();
  }
}

enum MedicalReportStatus { ADD, VERIFY }
