import 'package:doctor_app_flutter/core/enum/viewstate.dart';
import 'package:doctor_app_flutter/core/viewModel/PatientMedicalReportViewModel.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/models/patient/MedicalReport/MeidcalReportModel.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/util/dr_app_toast_msg.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/shared/app_loader_widget.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 'package:provider/provider.dart';

class AddVerifyMedicalReport extends StatefulWidget {
  @override
  _AddVerifyMedicalReportState createState() => _AddVerifyMedicalReportState();
}

class _AddVerifyMedicalReportState extends State<AddVerifyMedicalReport> {
  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of<ProjectViewModel>(context);
    final routeArgs = ModalRoute.of(context).settings.arguments as Map;
    PatiantInformtion patient = routeArgs['patient'];
    MedicalReportStatus status = routeArgs['status'];
    MedicalReportModel medicalReport = routeArgs.containsKey("medicalReport")
        ? routeArgs['medicalReport']
        : null;

    return BaseView<PatientMedicalReportViewModel>(
        onModelReady: (model) => model.getMedicalReportTemplate(),
        builder: (_, model, w) => AppScaffold(
              baseViewModel: model,
              isShowAppBar: true,
              appBarTitle: status == MedicalReportStatus.ADD
                  ? TranslationBase.of(context).medicalReportAdd
                  : TranslationBase.of(context).medicalReportVerify,
              backgroundColor: Theme.of(context).scaffoldBackgroundColor,
              body: model.state == ViewState.BusyLocal
                  ? AppLoaderWidget()
                  : 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: (medicalReport != null
                                            ? medicalReport.reportDataHtml
                                            : model.medicalReportTemplate
                                            .length > 0 ? model
                                            .medicalReportTemplate[0] : ""),
                                        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: 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 {
                              String txtOfMedicalReport =
                                  await HtmlEditor.getText();

                              if (txtOfMedicalReport.isNotEmpty) {
                                GifLoaderDialogUtils.showMyDialog(context);
                                model.insertMedicalReport(
                                    patient, txtOfMedicalReport);
                                GifLoaderDialogUtils.hideDialog(context);
                                if (model.state == ViewState.ErrorLocal) {
                                  DrAppToastMsg.showErrorToast(model.error);
                                }
                              }
                            },
                          ),
                        ),
                        SizedBox(
                          width: 8,
                        ),
                        if (medicalReport != null)
                          Expanded(
                            child: AppButton(
                              title: status == MedicalReportStatus.ADD
                                  ? TranslationBase.of(context).add
                                  : TranslationBase.of(context).verify,
                              color: Color(0xff359846),
                              fontWeight: FontWeight.w700,
                              onPressed: () async {
                                GifLoaderDialogUtils.showMyDialog(context);
                                await model.verifyMedicalReport(
                                    patient, medicalReport);
                                GifLoaderDialogUtils.hideDialog(context);
                                if (model.state == ViewState.ErrorLocal) {
                                  DrAppToastMsg.showErrorToast(model.error);
                                }
                              },
                            ),
                          ),
                      ],
                    ),
                  ),
                ],
              ),
            ));
  }

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

enum MedicalReportStatus { ADD, VERIFY }
