import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/enum/patient_type.dart';
import 'package:doctor_app_flutter/core/model/patient_muse/PatientSearchRequestModel.dart';
import 'package:doctor_app_flutter/core/viewModel/PatientSearchViewModel.dart';
import 'package:doctor_app_flutter/core/viewModel/authentication_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/patient_search/patient_search_result_screen.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update/shared_soap_widgets/bottom_sheet_title.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_text_field_custom_serach.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:provider/provider.dart';

class PatientSearchScreen extends StatefulWidget {
  @override
  _PatientSearchScreenState createState() => _PatientSearchScreenState();
}

class _PatientSearchScreenState extends State<PatientSearchScreen> {
  bool showOther = false;
  bool isFormSubmitted = false;
  TextEditingController patientFileInfoController = TextEditingController();
  TextEditingController firstNameInfoController = TextEditingController();
  TextEditingController middleNameInfoController = TextEditingController();
  TextEditingController lastNameFileInfoController = TextEditingController();
  PatientType selectedPatientType = PatientType.inPatient;
  AuthenticationViewModel authenticationViewModel = AuthenticationViewModel();
  ProjectViewModel projectViewModel = ProjectViewModel();

  @override
  Widget build(BuildContext context) {
    authenticationViewModel = Provider.of(context);
     projectViewModel= Provider.of(context);
    return BaseView<PatientSearchViewModel>(
      onModelReady: (model) async {},
      builder: (_, model, w) => AppScaffold(
        baseViewModel: model,
        isShowAppBar: false,
        backgroundColor: Theme.of(context).scaffoldBackgroundColor,
        body: SingleChildScrollView(
          child: Center(
            child: Column(
              children: [
                BottomSheetTitle(title: TranslationBase.of(context).searchPatient),
                FractionallySizedBox(
                  // widthFactor: 0.9,
                  child: Container(
                      color: Theme.of(context).scaffoldBackgroundColor,
                      child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                        SizedBox(
                          height: 16,
                        ),
                        SizedBox(
                          height: 10,
                        ),
                        AppTextFieldCustomSearch(
                          searchController: patientFileInfoController,
                          onChangeFun: (value) {},
                          marginTop: 5,
                          inputFormatters: [FilteringTextInputFormatter.allow(RegExp(ONLY_NUMBERS))],
                          inputType: TextInputType.number,
                          validationError: (isFormSubmitted &&
                                  (patientFileInfoController.text.isEmpty && firstNameInfoController.text.isEmpty && middleNameInfoController.text.isEmpty && lastNameFileInfoController.text.isEmpty))
                              ? TranslationBase.of(context).emptyMessage
                              : "",
                          onFieldSubmitted: () {},
                        ),
                        SizedBox(
                          height: 5,
                        ),
                        SizedBox(
                          height: MediaQuery.of(context).size.height * 0.12,
                        ),
                      ])),
                ),
              ],
            ),
          ),
        ),
        bottomSheet: Container(
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(
              Radius.circular(0.0),
            ),
            border: Border.all(color: HexColor('#707070'), width: 0),
          ),
          height: MediaQuery.of(context).size.height * 0.1,
          width: double.infinity,
          child: Column(
            children: [
              SizedBox(
                height: 10,
              ),
              Container(
                child: FractionallySizedBox(
                  widthFactor: .80,
                  child: Center(
                    child: AppButton(
                      fontWeight: FontWeight.w700,
                      title: TranslationBase.of(context).search,
                      onPressed: () {
                        onSubmitSearch();
                      },
                      color: AppGlobal.appRedColor,
                    ),
                  ),
                ),
              ),
              SizedBox(
                height: 5,
              ),
            ],
          ),
        ),
      ),
    );
  }

  onSubmitSearch() {
    GifLoaderDialogUtils.showMyDialog(context);
    setState(() {
      isFormSubmitted = true;
    });
    PatientSearchRequestModel patientSearchRequestModel = PatientSearchRequestModel(doctorID: authenticationViewModel.doctorProfile!.doctorID);
    var isVidaPlusProject =
        Utils.isVidaPlusProject(projectViewModel,  authenticationViewModel.doctorProfile!.projectID ?? -1);
   if(isVidaPlusProject){
     patientSearchRequestModel.transformDataForVidaPlus();
   }
    if (showOther) {
      patientSearchRequestModel.firstName = firstNameInfoController.text.trim().isEmpty ? "0" : firstNameInfoController.text.trim();
      patientSearchRequestModel.middleName = middleNameInfoController.text.trim().isEmpty ? "0" : middleNameInfoController.text.trim();
      patientSearchRequestModel.lastName = lastNameFileInfoController.text.isEmpty ? "0" : lastNameFileInfoController.text.trim();
    }

    if (patientFileInfoController.text.isNotEmpty) {
      if (patientFileInfoController.text.length == 10 && (patientFileInfoController.text[0] == '2' || patientFileInfoController.text[0] == '1')) {
        patientSearchRequestModel.identificationNo = patientFileInfoController.text;
        patientSearchRequestModel.searchType = 2;
        patientSearchRequestModel.patientID = 0;
      } else if ((patientFileInfoController.text.length == 10 || patientFileInfoController.text.length == 9) &&
          ((patientFileInfoController.text[0] == '0' && patientFileInfoController.text[1] == '5') || patientFileInfoController.text[0] == '5')) {
        patientSearchRequestModel.mobileNo = patientFileInfoController.text;
        patientSearchRequestModel.searchType = 0;
      } else {
        patientSearchRequestModel.patientID = int.parse(patientFileInfoController.text);
        patientSearchRequestModel.searchType = 1;
      }
    }

    GifLoaderDialogUtils.hideDialog(context);

    if (patientFileInfoController.text.isNotEmpty || firstNameInfoController.text.isNotEmpty || middleNameInfoController.text.isNotEmpty || lastNameFileInfoController.text.isNotEmpty) {
      setState(() {
        isFormSubmitted = false;
      });
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (BuildContext context) => PatientsSearchResultScreen(
            selectedPatientType: selectedPatientType,
            patientSearchRequestModel: patientSearchRequestModel,
            isSearchWithKeyInfo: patientFileInfoController.text.isNotEmpty ? true : false,
            isSearch: true,
            isSearchAndOut: true,
            searchKey: patientFileInfoController.text,
            isInpatient: false,
          ),
          settings: RouteSettings(name: 'PatientsSearchResultScreen'),
        ),
      );
    }
  }
}
