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/viewstate.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_view_model.dart';
import 'package:doctor_app_flutter/models/SOAP/GeneralGetReqForSOAP.dart';
import 'package:doctor_app_flutter/models/SOAP/GetAssessmentResModel.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/screens/medicine/medicine_search_screen.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.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;
  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'},
  ];
  GeneralGetReqForSOAP generalGetReqForSOAP = GeneralGetReqForSOAP(
      patientMRN: 2954208, //widget.patient.patientMRN,
      episodeId: 210011002, //widget.patient.episodeNo,
      appointmentNo: 2016055308, //widget.patient.appointmentNo,
      doctorID: '',
      editedBy: '');

  VitalSignsViewModel model = new VitalSignsViewModel();
  SOAPViewModel model2 = new SOAPViewModel();

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

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

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

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

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