import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/model/prescription_model.dart';
import 'package:doctor_app_flutter/core/service/base/lookup-service.dart';
import 'package:doctor_app_flutter/models/SOAP/ChiefComplaint/GetChiefComplaintResModel.dart';
import 'package:doctor_app_flutter/models/SOAP/GetAssessmentResModel.dart';
import 'package:doctor_app_flutter/models/SOAP/order-procedure.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/models/patient/vital_sign/patient-vital-sign-history.dart';

class UcafService extends LookupService {

  List<GetChiefComplaintResModel> patientChiefComplaintList  = [];
  List<VitalSignHistory> patientVitalSignsHistory = [];
  List<GetAssessmentResModel> patientAssessmentList  = [];
  List<OrderProcedure> orderProcedureList  = [];
  PrescriptionModel prescriptionList;

  Future getPatientChiefComplaint(PatiantInformtion patient) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['PatientMRN'] = patient.patientMRN;
    body['AppointmentNo'] = patient.appointmentNo;
    body['EpisodeID'] = patient.episodeNo;
    body['DoctorID'] = "";

    await baseAppClient.post (GET_CHIEF_COMPLAINT,
        onSuccess: (dynamic response, int statusCode) {
          print("Success");
          patientChiefComplaintList.clear();
          response['List_ChiefComplaint']['entityList'].forEach((v) {
            patientChiefComplaintList.add(GetChiefComplaintResModel.fromJson(v));
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: body);
  }

  Future getInPatientVitalSignHistory(PatiantInformtion patient, bool isInPatient) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['PatientID'] = patient.patientId;
    body['PatientTypeID'] = 1;
    if(isInPatient){
      body['InOutPatientType'] = 1;
    }else {
      body['InOutPatientType'] = 2;
    }

    await baseAppClient.post(
      GET_PATIENT_VITAL_SIGN,
      onSuccess: (dynamic response, int statusCode) {
        patientVitalSignsHistory.clear();
        if (response['List_DoctorPatientVitalSign'] != null) {
          response['List_DoctorPatientVitalSign'].forEach((v) {
            patientVitalSignsHistory.add(new VitalSignHistory.fromJson(v));
          });
        }
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error.toString();
      },
      body: body,
    );
  }

  Future getPatientVitalSignsHistory(
      PatiantInformtion patient, String fromDate, String toDate) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['PatientMRN'] = patient.patientId; // patient.patientMRN
    // body['AppointmentNo'] = patient.appointmentNo;
    // body['EpisodeID'] = patient.episodeNo;
    body['PatientTypeID'] = 1;
    body['PatientType'] = 1;
    body['DeviceTypeID'] = 1;
    body['isDentalAllowedBackend'] = false;
    body['From'] = fromDate;
    body['To'] = toDate;

    await baseAppClient.post(
      GET_PATIENT_VITAL_SIGN_DATA,
      onSuccess: (dynamic response, int statusCode) {
        patientVitalSignsHistory.clear();
        if (response['VitalSignsHistory'] != null) {
          response['VitalSignsHistory'].forEach((v) {
            patientVitalSignsHistory.add(new VitalSignHistory.fromJson(v));
          });
        }
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error.toString();
      },
      body: body,
    );
  }

  Future getPatientAssessment(PatiantInformtion patient) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['PatientMRN'] = patient.patientMRN;
    body['AppointmentNo'] = patient.appointmentNo;
    body['EpisodeID'] = patient.episodeNo;

    await baseAppClient.post (GET_ASSESSMENT,
        onSuccess: (dynamic response, int statusCode) {
          print("Success");
          patientAssessmentList.clear();
          response['AssessmentList']['entityList'].forEach((v) {
            patientAssessmentList.add(GetAssessmentResModel.fromJson(v));
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: body);
  }

  Future getPrescription(PatiantInformtion patient) async {
    hasError = false;

    Map<String, dynamic> body = Map();
    body['PatientMRN'] = patient.patientMRN;
    body['AppointmentNo'] = patient.appointmentNo;
    body['EpisodeID'] = patient.episodeNo;

    hasError = false;
    prescriptionList = null;
    await baseAppClient.post(GET_PRESCRIPTION_LIST,
        onSuccess: (dynamic response, int statusCode) {
          prescriptionList = PrescriptionModel.fromJson(response['PrescriptionList']);
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: body);
  }

  Future getOrderProcedures(PatiantInformtion patient) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['PatientMRN'] = patient.patientMRN;
    body['AppointmentNo'] = patient.appointmentNo;
    body['EpisodeID'] = patient.episodeNo;

    await baseAppClient.post (GET_ORDER_PROCEDURE,
        onSuccess: (dynamic response, int statusCode) {
          print("Success");
          orderProcedureList.clear();
          response['OrderedProcedureList']['entityList'].forEach((v) {
            orderProcedureList.add(OrderProcedure.fromJson(v));
          });
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: body);
  }

  Future postUCAF(PatiantInformtion patient) async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['PatientMRN'] = patient.patientMRN;
    body['AppointmentNo'] = patient.appointmentNo;

    await baseAppClient.post (POST_UCAF,
        onSuccess: (dynamic response, int statusCode) {
          print("Success");
        }, onFailure: (String error, int statusCode) {
          hasError = true;
          super.error = error;
        }, body: body);
  }

}