import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/model/admissionRequest/admission-request.dart';
import 'package:doctor_app_flutter/core/service/base/lookup-service.dart';

class AdmissionRequestService extends LookupService {
  List<dynamic> clinicList = [];
  List<dynamic> doctorsList = [];
  List<dynamic> specialityList = [];
  List<dynamic> floorList = [];
  List<dynamic> wardList = [];
  List<dynamic> roomCategoryList = [];
  List<dynamic> diagnosisTypesList = [];
  List<dynamic> allergiesLookupList = [];
  List<dynamic> dietTypesList = [];
  List<dynamic> icdCodes = [];


  setSpecialityList() {
    specialityList.clear();
    listOfSpeciality.forEach((element) {
      specialityList.add(element.toJson());
    });
  }

  setAllergies() {
    allergiesLookupList.clear();
    allergiesList.forEach((element) {
      allergiesLookupList.add(element.toJson());
    });
  }

  Future getDoctorsList(int clinicId) async {
    hasError = false;

    Map<String, dynamic> body = Map();
    body['ClinicID'] = clinicId;

    await baseAppClient.post(
      PATIENT_GET_DOCTOR_BY_CLINIC_URL,
      onSuccess: (dynamic response, int statusCode) {
        doctorsList = [];
        doctorsList = response['List_Doctors_All'];
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: body,
    );
  }

  Future getClinics() async {
    hasError = false;

    await baseAppClient.post(
      GET_CLINICS,
      onSuccess: (dynamic response, int statusCode) {
        clinicList.clear();
        if (response['listClinics'] != null &&
            response['listClinics']['entityList'] != null) {
          clinicList.addAll(response['listClinics']['entityList']);
        }
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: Map(),
    );
  }

  Future getFloors() async {
    hasError = false;

    await baseAppClient.post(
      GET_FLOORS,
      onSuccess: (dynamic response, int statusCode) {
        floorList.clear();
        if (response['AdmissionMasterList'] != null &&
            response['AdmissionMasterList']['entityList'] != null) {
          response['AdmissionMasterList']['entityList'].forEach((v) {
            floorList.add(v);
          });
        }
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: Map(),
    );
  }

  Future getWardList() async {
    hasError = false;

    await baseAppClient.post(
      GET_WARDS,
      onSuccess: (dynamic response, int statusCode) {
        wardList.clear();
        if (response['AdmissionMasterList'] != null &&
            response['AdmissionMasterList']['entityList'] != null) {
          response['AdmissionMasterList']['entityList'].forEach((v) {
            // wardList.add(WardModel.fromJson(v));
            wardList.add(v);
          });
        }
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: Map(),
    );
  }

  Future getRoomCategories() async {
    hasError = false;

    await baseAppClient.post(
      GET_ROOM_CATEGORIES,
      onSuccess: (dynamic response, int statusCode) {
        roomCategoryList.clear();
        if (response['AdmissionMasterList'] != null &&
            response['AdmissionMasterList']['entityList'] != null) {
          response['AdmissionMasterList']['entityList'].forEach((v) {
            roomCategoryList.add(v);
          });
        }
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: Map(),
    );
  }

  Future getDiagnosisTypesList() async {
    hasError = false;

    await baseAppClient.post(
      GET_DIAGNOSIS_TYPES,
      onSuccess: (dynamic response, int statusCode) {
        diagnosisTypesList.clear();
        if (response['AdmissionDiagnosisList'] != null &&
            response['AdmissionDiagnosisList']['entityList'] != null) {
          response['AdmissionDiagnosisList']['entityList'].forEach((v) {
            // diagnosisTypesList.add(MasterKeyModel.fromJson(v));
            diagnosisTypesList.add(v);
          });
        }
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: Map(),
    );
  }

  Future getDietTypesList(int patientMrn) async {
    hasError = false;

    Map<String, dynamic> body = Map();
    body['patientMRN'] = patientMrn;

    await baseAppClient.post(
      GET_DIET_TYPES,
      onSuccess: (dynamic response, int statusCode) {
        dietTypesList.clear();
        if (response['DietTypeList'] != null &&
            response['DietTypeList']['entityList'] != null) {
          response['DietTypeList']['entityList'].forEach((v) {
            // diagnosisTypesList.add(MasterKeyModel.fromJson(v));
            dietTypesList.add(v);
          });
        }
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: body,
    );
  }

  Future getICDCodes(int patientMrn) async {
    hasError = false;

    Map<String, dynamic> body = Map();
    body['PatientMRN'] = patientMrn;
    body['isPatientDiagnosisOnly'] = true;

    await baseAppClient.post(
      GET_ICD_CODES,
      onSuccess: (dynamic response, int statusCode) {
        icdCodes.clear();
        if (response['ICDCodesList'] != null) {
          icdCodes.addAll(response['ICDCodesList']);
          // response['ICDCodesList'].forEach((v) {
            // diagnosisTypesList.add(MasterKeyModel.fromJson(v));
            // icdCodes.add(v);
          // });
        }
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: body,
    );
  }

  Future makeAdmissionRequest(AdmissionRequest admissionRequest) async{
    hasError = false;

    Map<String, dynamic> body = Map();
    body['AdmissionRequestViewModel'] = admissionRequest.toJson();

    await baseAppClient.post(
      POST_ADMISSION_REQUEST,
      onSuccess: (dynamic response, int statusCode) {
        print(response["admissionResponse"]["success"]);
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
      body: body,
    );
  }

}
