import 'package:autocomplete_textfield/autocomplete_textfield.dart';
import 'package:doctor_app_flutter/core/model/Prescriptions/prescription_model.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/model/search_drug/get_medication_response_model.dart';
import 'package:doctor_app_flutter/core/provider/robot_provider.dart';
import 'package:doctor_app_flutter/core/viewModel/medicine_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/prescription/prescription_view_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/prescription/add_prescription/prescription_form_widget.dart';
import 'package:doctor_app_flutter/screens/prescription/add_prescription/search_preescription_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/loader/gif_loader_dialog_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/network_base_view.dart';
import 'package:flutter/material.dart';
import 'package:speech_to_text/speech_to_text.dart' as stt;

class AddPrescription extends StatefulWidget {
  final PrescriptionViewModel prescriptionViewModel;
  final PatiantInformtion patient;
  final List<PrescriptionModel> prescriptionList;

  AddPrescription(this.prescriptionViewModel, this.patient, this.prescriptionList);

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

class _AddPrescriptionState extends State<AddPrescription> {
  String routeError = '';
  String frequencyError = '';
  String doseTimeError = '';
  String durationError = '';
  String unitError = '';
  String strengthError = '';
  int selectedType = 0;

  bool visibilitySearch = true;

  TextEditingController strengthController = TextEditingController();
  TextEditingController unitController = TextEditingController();
  TextEditingController indicationController = TextEditingController();
  TextEditingController instructionController = TextEditingController();

  bool visibilityPrescriptionForm = false;

  final myController = TextEditingController();
  DateTime? selectedDate;
  int strengthChar = 0;
  GetMedicationResponseModel _selectedMedication = GetMedicationResponseModel();
  MedicineViewModel? medicineViewModel;

  GlobalKey<AutoCompleteTextFieldState<GetMedicationResponseModel>> key = GlobalKey<AutoCompleteTextFieldState<GetMedicationResponseModel>>();

  TextEditingController drugIdController = TextEditingController();
  TextEditingController doseController = TextEditingController();
  final searchController = TextEditingController();
  stt.SpeechToText speech = stt.SpeechToText();
  var event = RobotProvider();
  var recognizedWord;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  final double spaceBetweenTextFields = 12;
  dynamic route;
  dynamic frequency;
  dynamic duration;
  dynamic doseTime;
  dynamic indication;
  dynamic units;
  dynamic uom;
  dynamic box;
  dynamic x;

  PatiantInformtion? patient;

  @override
  Widget build(BuildContext context) {
    return BaseView<MedicineViewModel>(
      builder: (
        BuildContext context,
        MedicineViewModel model,
        Widget? child,
      ) =>
          NetworkBaseView(
        baseViewModel: model,
        child: GestureDetector(
          onTap: () {
            FocusScope.of(context).requestFocus(new FocusNode());
          },
          child: DraggableScrollableSheet(
              initialChildSize: 0.98,
              maxChildSize: 0.98,
              minChildSize: 0.9,
              builder: (BuildContext context, ScrollController scrollController) {
                return SingleChildScrollView(
                  child: Container(
                    height: MediaQuery.of(context).size.height * 1.65,
                    color: Color(0xffF8F8F8),
                    child: Padding(
                      padding: EdgeInsets.symmetric(horizontal: 12.0, vertical: 10.0),
                      child: Column(
                        children: [
                          Column(
                            children: [
                              SizedBox(
                                height: 60,
                              ),
                            ],
                          ),
                          SizedBox(
                            height: spaceBetweenTextFields,
                          ),
                          Container(
                            child: Column(
                              children: [
                                Visibility(
                                  visible: visibilitySearch,
                                  child: SearchPrescriptionWidget(
                                    spaceBetweenTextFields: spaceBetweenTextFields,
                                    medicineViewModel: model,
                                    prescriptionViewModel: widget.prescriptionViewModel,
                                    onItemSelected: (GetMedicationResponseModel medication) async {
                                      setState(() {
                                        visibilitySearch = !visibilitySearch;
                                        _selectedMedication = medication;
                                      });
                                      GifLoaderDialogUtils.showMyDialog(context);
                                      await model.getAssessmentList(patientInfo: widget.patient, isLocalBusy: true);
                                      GifLoaderDialogUtils.hideDialog(context);
                                    },
                                  ),
                                ),
                                Visibility(
                                  visible: !visibilitySearch,
                                  child: Container(
                                    child: Column(
                                      children: [
                                        PrescriptionFormWidget(
                                          patient: widget.patient,
                                          prescriptionViewModel: widget.prescriptionViewModel,
                                          medicineViewModel: model,
                                          prescriptionList: widget.prescriptionList,
                                          selectedMedication: _selectedMedication,
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              }),
        ),
      ),
    );
  }
}
