import 'package:doctor_app_flutter/config/shared_pref_kay.dart';
import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/enum/master_lookup_key.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/SOAP/Assessment/get_assessment_res_model.dart';
import 'package:doctor_app_flutter/core/model/SOAP/general_get_req_for_SOAP.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/viewModel/SOAP_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/patient-vital-sign-viewmodel.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/medicine/medicine_search_screen.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/loader/gif_loader_container.dart';
import 'package:doctor_app_flutter/widgets/shared/network_base_view.dart';
import 'package:flutter/material.dart';

class DrugToDrug extends StatefulWidget {
  final PatiantInformtion patient;
  final prescription;
  final List<GetAssessmentResModel> listAssessment;

  DrugToDrug(this.patient, this.prescription, this.listAssessment);

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

class _DrugToDrug extends State<DrugToDrug> {
  int typeID = 1;
  bool isLoaded = false;

  /// TODO Elham* fix this
  List<Map<dynamic, dynamic>> expandableList = [
    {'name': 'CRITICAL', 'level': 'LEVEL_4'},
    {'name': 'HIGH', 'level': 'LEVEL_3'},
    {'name': 'MODERATE', 'level': 'LEVEL_2'},
    {'name': 'LOW', 'level': 'LEVEL_1'},
    {'name': 'INFO', 'level': 'INFO'},
  ];

  VitalSignsViewModel vitalSignsViewModel = new VitalSignsViewModel();
  SOAPViewModel soapViewModel = new SOAPViewModel();

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

  @override
  Widget build(BuildContext context) {
    return isLoaded == true
        ? BaseView<PrescriptionViewModel>(
            onModelReady: (prescriptionViewModel) =>
                prescriptionViewModel.getDrugToDrug(vitalSignsViewModel.patientVitalSigns, widget.listAssessment, soapViewModel.patientAllergiesList, widget.patient, widget.prescription),
            builder: (BuildContext context, PrescriptionViewModel prescriptionViewModel, Widget? child) => NetworkBaseView(
                baseViewModel: prescriptionViewModel,
                child: Container(
                    height: SizeConfig.realScreenHeight! * .9,
                    child: new ListView.builder(
                        itemCount: expandableList.length,
                        itemBuilder: (context, i) {
                          return new ExpansionTile(
                              title: new AppText(
                                expandableList[i]['name'] + ' ' + '(' + getDrugInfo(expandableList[i]['level'], prescriptionViewModel).length.toString() + ')',
                                fontSize: 20,
                                fontWeight: FontWeight.bold,
                              ),
                              children: getDrugInfo(expandableList[i]['level'], prescriptionViewModel).map((item) {
                                return Container(
                                    padding: EdgeInsets.all(10),
                                    child: AppText(
                                      item['comment'],
                                      color: Colors.red[900]!,
                                    ));
                              }).toList());
                        }))))
        : Container(height: SizeConfig.realScreenHeight! * .45, child: GifLoaderContainer());
  }

  getTypeID() async {
    var type = await sharedPref.getString(SLECTED_PATIENT_TYPE) == '0' ? 2 : 1;
    setState(() {
      typeID = type;
    });
  }

  getRequestedData() async {
    GeneralGetReqForSOAP generalGetReqForSOAP = GeneralGetReqForSOAP(

        /// TODO Elham* fix this today
        patientMRN: widget.patient.patientMRN,
        episodeId: widget.patient.episodeNo,
        appointmentNo: widget.patient.appointmentNo,
        doctorID: '',
        editedBy: '');

    await vitalSignsViewModel.getPatientVitalSign(widget.patient);
    await soapViewModel.getPatientAllergy(generalGetReqForSOAP);
    await soapViewModel.getMasterLookup(MasterKeysService.DiagnosisType);
    if (vitalSignsViewModel.state == ViewState.Idle && soapViewModel.state == ViewState.Idle) {
      setState(() {
        isLoaded = true;
      });
    }
  }

  List<dynamic> getDrugInfo(level, prescriptionViewModel) {
    return prescriptionViewModel.drugToDrug.where((i) => i['Severity'] == level).toList();
  }
}
