import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/operation_report/create_update_operation_report_request_model.dart';
import 'package:doctor_app_flutter/core/model/operation_report/get_operation_details_response_modle.dart';
import 'package:doctor_app_flutter/core/model/operation_report/get_reservations_response_model.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/provider/robot_provider.dart';
import 'package:doctor_app_flutter/core/viewModel/profile/operation_report_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update/shared_soap_widgets/bottom_sheet_title.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/utils/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/app-textfield-custom.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:speech_to_text/speech_to_text.dart' as stt;

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

class UpdateOperationReport extends StatefulWidget {
  final GetReservationsResponseModel? reservation;

  // final OperationReportViewModel operationReportViewModel;
  final PatiantInformtion? patient;
  final int? visitType;
  final bool isUpdate;

  const UpdateOperationReport(
      {Key? key,
      // this.operationReportViewModel,
      this.patient,
      this.visitType,
      required this.isUpdate,
      this.reservation})
      : super(key: key);

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

class _UpdateOperationReportState extends State<UpdateOperationReport> {
  int? selectedType;
  bool isSubmitted = false;
  stt.SpeechToText speech = stt.SpeechToText();
  var recognizedWord;
  var event = RobotProvider();
  late ProjectViewModel projectViewModel;

  TextEditingController preOpDiagmosisController = TextEditingController();
  TextEditingController postOpDiagmosisNoteController = TextEditingController();
  TextEditingController surgeonController = TextEditingController();
  TextEditingController assistantNoteController = TextEditingController();
  TextEditingController operationController = TextEditingController();
  TextEditingController inasionController = TextEditingController();
  TextEditingController findingController = TextEditingController();
  TextEditingController surgeryProcedureController = TextEditingController();
  TextEditingController postOpInstructionController = TextEditingController();
  TextEditingController complicationDetailsController = TextEditingController();
  TextEditingController bloodLossDetailController = TextEditingController();
  TextEditingController histopathSpecimenController = TextEditingController();
  TextEditingController microbiologySpecimenController = TextEditingController();
  TextEditingController otherSpecimenController = TextEditingController();
  TextEditingController scrubNurseController = TextEditingController();
  TextEditingController circulatingNurseController = TextEditingController();
  TextEditingController BloodTransfusedDetailController = TextEditingController();
  TextEditingController anasthetistController = TextEditingController();
  TextEditingController OTReservationID = TextEditingController();

  setSelectedType(int val) {
    setState(() {
      selectedType = val;
    });
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    //TODO Elham* add translation to hints

    return BaseView<OperationReportViewModel>(
        onModelReady: (model) async {
          await model.getOperationReportDetails(widget.reservation!);
          OTReservationID.text = widget.reservation!.oTReservationID.toString();

          if (model.operationDetailsList.isNotEmpty) {
            GetOperationDetailsResponseModel oldDetails = model.operationDetailsList[0];

            postOpDiagmosisNoteController.text = oldDetails.postOpInstruction!;
            preOpDiagmosisController.text = oldDetails.preOpDiagnosis!;
            surgeonController.text = oldDetails.surgeon!;
            assistantNoteController.text = oldDetails.assistant!;
            operationController.text = oldDetails.operation!;
            inasionController.text = oldDetails.inasion!;
            findingController.text = oldDetails.finding!;
            surgeryProcedureController.text = oldDetails.surgeryProcedure!;
            postOpInstructionController.text = oldDetails.postOpInstruction!;
            complicationDetailsController.text = oldDetails.complicationDetail!;
            bloodLossDetailController.text = oldDetails.bloodLossDetail!;
            histopathSpecimenController.text = oldDetails.histopathSpecimen!;
            microbiologySpecimenController.text = oldDetails.microbiologySpecimen!;
            otherSpecimenController.text = oldDetails.otherSpecimen!;
            scrubNurseController.text = oldDetails.scrubNurse;
            circulatingNurseController.text = oldDetails.circulatingNurse;
            BloodTransfusedDetailController.text = oldDetails.bloodTransfusedDetail;
            anasthetistController.text = oldDetails.anasthetist!;
          }
        },
        builder: (_, model, w) => AppScaffold(
              isShowAppBar: true,
              baseViewModel: model,
              backgroundColor: Theme.of(context).scaffoldBackgroundColor,
              appBar: BottomSheetTitle(
                title: TranslationBase.of(context).operationReports,
              ),
              body: SingleChildScrollView(
                child: Container(
                  height: MediaQuery.of(context).size.height * 1.0,
                  child: Padding(
                    padding: EdgeInsets.all(0.0),
                    child: SingleChildScrollView(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          SizedBox(
                            height: 10.0,
                          ),
                          SingleChildScrollView(
                            child: Center(
                              child: FractionallySizedBox(
                                widthFactor: 0.9,
                                child: Column(
                                  children: [
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).reservation,
                                      controller: OTReservationID,
                                      maxLines: 1,
                                      minLines: 1,
                                      enabled: false,
                                      hasBorder: true,
                                      onChanged: (value) {},
                                      onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).preOperationDiagnosis,
                                      controller: preOpDiagmosisController,
                                      maxLines: 20,
                                      minLines: 8,
                                      hasBorder: true,

                                      // isTextFieldHasSuffix: true,
                                      validationError: preOpDiagmosisController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null, onChanged: (value) {},
                                      onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).postOperationDiagnosis,
                                      controller: postOpDiagmosisNoteController,
                                      maxLines: 20,
                                      minLines: 8,
                                      hasBorder: true,
                                      // isTextFieldHasSuffix: true,
                                      validationError: postOpDiagmosisNoteController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null, onChanged: (value) {},
                                      onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).surgeon,
                                      controller: surgeonController,
                                      maxLines: 1,
                                      minLines: 1,
                                      hasBorder: true,
                                      // isTextFieldHasSuffix: true,
                                      validationError: surgeonController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null, onChanged: (value) {},
                                      onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).assistant,
                                      controller: assistantNoteController,
                                      maxLines: 1,
                                      minLines: 1,
                                      hasBorder: true,

                                      // isTextFieldHasSuffix: true,
                                      validationError: assistantNoteController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null, onChanged: (value) {},
                                      onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).operation,
                                      controller: operationController,
                                      maxLines: 20,
                                      minLines: 4,
                                      hasBorder: true,

                                      // isTextFieldHasSuffix: true,
                                      validationError: operationController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null,
                                      onChanged: (value) {}, onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: "inasion",
                                      //TranslationBase.of(context).addoperationReports,
                                      controller: inasionController,
                                      maxLines: 1,
                                      minLines: 1,
                                      hasBorder: true,

                                      // isTextFieldHasSuffix: true,
                                      validationError: inasionController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null,
                                      onChanged: (value) {}, onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).finding,
                                      controller: findingController,
                                      maxLines: 20,
                                      minLines: 4,
                                      hasBorder: true,

                                      // isTextFieldHasSuffix: true,
                                      validationError: findingController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null,
                                      onChanged: (value) {}, onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).surgeryProcedure,
                                      controller: surgeryProcedureController,
                                      maxLines: 20,
                                      minLines: 8,
                                      hasBorder: true,

                                      // isTextFieldHasSuffix: true,
                                      validationError: surgeryProcedureController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null,
                                      onChanged: (value) {}, onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).postOperationInstruction,
                                      controller: postOpInstructionController,
                                      maxLines: 20,
                                      minLines: 8,
                                      hasBorder: true,

                                      // isTextFieldHasSuffix: true,
                                      validationError: postOpInstructionController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null,
                                      onChanged: (value) {}, onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).complicationDetails1,
                                      controller: complicationDetailsController,
                                      maxLines: 20,
                                      minLines: 4,
                                      hasBorder: true,
                                      // isTextFieldHasSuffix: true,
                                      validationError: complicationDetailsController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null,
                                      onChanged: (value) {}, onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).bloodLossDetail,
                                      controller: bloodLossDetailController,
                                      maxLines: 20,
                                      minLines: 4,
                                      hasBorder: true,

                                      // isTextFieldHasSuffix: true,
                                      validationError: bloodLossDetailController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null,
                                      onChanged: (value) {}, onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).histopathSpecimen,
                                      controller: histopathSpecimenController,
                                      maxLines: 20,
                                      minLines: 4,
                                      hasBorder: true,

                                      // isTextFieldHasSuffix: true,
                                      validationError: histopathSpecimenController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null,
                                      onChanged: (value) {}, onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).microbiologySpecimen,
                                      controller: microbiologySpecimenController,
                                      maxLines: 20,
                                      minLines: 4,
                                      hasBorder: true,

                                      // isTextFieldHasSuffix: true,
                                      validationError: microbiologySpecimenController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null,
                                      onChanged: (value) {}, onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).otherSpecimen,
                                      controller: otherSpecimenController,
                                      maxLines: 20,
                                      minLines: 4,
                                      hasBorder: true,

                                      // isTextFieldHasSuffix: true,
                                      validationError: otherSpecimenController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null,
                                      onChanged: (value) {}, onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).scrubNurse,
                                      controller: scrubNurseController,
                                      maxLines: 1,
                                      minLines: 1,
                                      hasBorder: true,

                                      // isTextFieldHasSuffix: true,
                                      validationError: scrubNurseController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null,
                                      onChanged: (value) {}, onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).circulatingNurse,
                                      controller: circulatingNurseController,
                                      maxLines: 1,
                                      minLines: 1,
                                      hasBorder: true,

                                      // isTextFieldHasSuffix: true,
                                      validationError: circulatingNurseController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null,
                                      onChanged: (value) {}, onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).bloodTransfusedDetail,
                                      controller: BloodTransfusedDetailController,
                                      maxLines: 20,
                                      minLines: 4,
                                      hasBorder: true,

                                      // isTextFieldHasSuffix: true,
                                      validationError: BloodTransfusedDetailController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null,
                                      onChanged: (value) {}, onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 4,
                                    ),
                                    AppTextFieldCustom(
                                      hintText: TranslationBase.of(context).anesthetist,
                                      controller: anasthetistController,
                                      maxLines: 1,
                                      minLines: 1,
                                      hasBorder: true,

                                      // isTextFieldHasSuffix: true,
                                      validationError: anasthetistController.text.isEmpty && isSubmitted ? TranslationBase.of(context).emptyMessage : null,
                                      onChanged: (value) {}, onFieldSubmitted: () {},
                                    ),
                                    SizedBox(
                                      height: 250,
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ),
              bottomSheet: model.state != ViewState.Idle
                  ? Container(
                      height: 0,
                    )
                  : Container(
                      height: 70,
                      margin: EdgeInsets.all(SizeConfig.widthMultiplier! * 5),
                      child: Column(
                        children: <Widget>[
                          Container(
                            margin: EdgeInsets.all(5),
                            child: AppButton(
                              title: (widget.isUpdate ? TranslationBase.of(context).noteUpdate : TranslationBase.of(context).noteAdd) + " " + TranslationBase.of(context).operationReports,
                              color: AppGlobal.appGreenColor,
                              // disabled: operationReportsController.text.isEmpty,
                              fontWeight: FontWeight.w700,
                              onPressed: () async {
                                setState(() {
                                  isSubmitted = true;
                                });
                                if (isFormValid()) {
                                  GifLoaderDialogUtils.showMyDialog(context);
                                  await model.getDoctorProfile();

                                  CreateUpdateOperationReportRequestModel createUpdateOperationReportRequestModel = CreateUpdateOperationReportRequestModel(
                                    inasion: inasionController.text,

                                    /// TODO Elham* Add dynamic reservation
                                    reservationNo: widget.reservation!.oTReservationID,
                                    preOpDiagmosis: preOpDiagmosisController.text,
                                    postOpDiagmosis: postOpDiagmosisNoteController.text,
                                    surgeon: surgeonController.text,
                                    assistant: assistantNoteController.text,
                                    anasthetist: assistantNoteController.text,
                                    operation: operationController.text,
                                    finding: findingController.text,
                                    surgeryProcedure: surgeonController.text,
                                    postOpInstruction: postOpInstructionController.text,
                                    complicationDetails: complicationDetailsController.text,
                                    bloodLossDetail: bloodLossDetailController.text,
                                    histopathSpecimen: histopathSpecimenController.text,
                                    microbiologySpecimen: microbiologySpecimenController.text,
                                    otherSpecimen: otherSpecimenController.text,
                                    scrubNurse: surgeonController.text,
                                    circulatingNurse: circulatingNurseController.text,
                                    bloodTransfusedDetail: bloodLossDetailController.text,
                                    patientID: widget.patient!.patientId,
                                    admissionNo: int.parse(widget.patient!.admissionNo!),
                                    createdBy: model.doctorProfile!.doctorID,
                                  );
                                  await model.updateOperationReport(createUpdateOperationReportRequestModel);

                                  if (model.state == ViewState.ErrorLocal) {
                                    Utils.showErrorToast(model.error);
                                  } else {
                                    // await model.(
                                    //     operationReportsRequest.toJson());

                                    DrAppToastMsg.showSuccesToast("Your Order added Successfully");
                                  }
                                  GifLoaderDialogUtils.hideDialog(context);
                                }
                              },
                            ),
                          ),
                        ],
                      ),
                    ),
            ));
  }

  isFormValid() {
    if (preOpDiagmosisController.text != null &&
        preOpDiagmosisController.text.isNotEmpty &&
        postOpDiagmosisNoteController.text != null &&
        postOpDiagmosisNoteController.text.isNotEmpty &&
        surgeonController.text != null &&
        surgeonController.text.isNotEmpty &&
        assistantNoteController.text != null &&
        assistantNoteController.text.isNotEmpty &&
        operationController.text != null &&
        operationController.text.isNotEmpty &&
        inasionController.text != null &&
        inasionController.text.isNotEmpty &&
        findingController.text != null &&
        findingController.text.isNotEmpty &&
        surgeryProcedureController.text != null &&
        surgeryProcedureController.text.isNotEmpty &&
        postOpInstructionController.text != null &&
        postOpInstructionController.text.isNotEmpty &&
        complicationDetailsController.text != null &&
        complicationDetailsController.text.isNotEmpty &&
        bloodLossDetailController.text != null &&
        bloodLossDetailController.text.isNotEmpty &&
        histopathSpecimenController.text != null &&
        histopathSpecimenController.text.isNotEmpty &&
        microbiologySpecimenController.text != null &&
        microbiologySpecimenController.text.isNotEmpty &&
        otherSpecimenController.text != null &&
        otherSpecimenController.text.isNotEmpty &&
        scrubNurseController.text != null &&
        scrubNurseController.text.isNotEmpty &&
        circulatingNurseController.text != null &&
        circulatingNurseController.text.isNotEmpty &&
        BloodTransfusedDetailController.text != null &&
        BloodTransfusedDetailController.text.isNotEmpty &&
        anasthetistController.text != null &&
        anasthetistController.text.isNotEmpty) {
      return true;
    } else {
      return false;
    }
  }
}
