import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/config/shared_pref_kay.dart';
import 'package:hmg_patient_app/core/service/base_service.dart';
import 'package:hmg_patient_app/models/Authentication/authenticated_user.dart';
import 'package:hmg_patient_app/models/Request.dart';
import 'package:hmg_patient_app/services/authentication/auth_provider.dart';
import 'package:hmg_patient_app/uitl/app_shared_preferences.dart';
import 'package:flutter/material.dart';

import '../../models/InPatientServices/get_admission_info_response_model.dart';
import '../../uitl/date_uitl.dart';

class ClinicListService extends BaseService {
  AppSharedPreferences sharedPref = AppSharedPreferences();
  AppGlobal appGlobal = new AppGlobal();

  AuthenticatedUser authUser = new AuthenticatedUser();
  AuthProvider authProvider = new AuthProvider();

  double? lat;
  double? long;

  Future<Map> getClinicsList(context) async {
    Map<String, dynamic> request = {};

    dynamic localRes;

    await baseAppClient.post(GET_CLINICS_LIST_URL, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request);
    sharedPref.setObject(CLINICS_LIST, localRes);
    return Future.value(localRes);
  }

  Future<Map> getClinicByHospital({required String projectID}) async {
    Map<String, dynamic> request = {"ProjectID": projectID};

    dynamic localRes;

    await baseAppClient.post(GET_CLINICS_LIST_WRT_HOSPITAL_URL, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request);
    return Future.value(localRes);
  }

  Future<Map> getActiveAppointmentNo(context) async {
    Map<String, dynamic> request = {};

    request = {"IsActiveAppointment": true, "LanguageID": 1};

    dynamic localRes;

    await baseAppClient.post(GET_ACTIVE_APPOINTMENTS_LIST_URL, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request);
    return Future.value(localRes);
  }

  Future<Map> getProjectsList(int languageID, context) async {
    Map<String, dynamic> request = {};

    // request = {"LanguageID": languageID};

    dynamic localRes;

    await baseAppClient.post(GET_PROJECTS_LIST, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request);
    return Future.value(localRes);
  }

  Future<Map> getChiefComplaintsList(int patientID, int clinicID, int projectID, int languageID, bool isContinueDentalPlan, BuildContext context, {doctorId}) async {
    //Utils.showProgressDialog(context);
    Map<String, dynamic> request;

    if (await this.sharedPref.getObject(USER_PROFILE) != null) {
      var data = AuthenticatedUser.fromJson(await this.sharedPref.getObject(USER_PROFILE));
      authUser = data;
    }

    var languageID = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');
    Request req = appGlobal.getPublicRequest();
    request = {
      "ClinicID": clinicID,
      "ProjectID": projectID,
      "SelectedDate": "",
      "SelectedTime": "",
      "License": true,
      // "VersionID": 5.6,
      // "Channel": 3,
      "LanguageID": languageID,
      "IPAdress": "10.20.10.20",
      "generalid": "Cs2020@2016\$2958",
      "SessionID": null,
      "isDentalAllowedBackend": true,
      "DeviceTypeID": 1,
      "PatientID": patientID,
      "ContinueDentalPlan": isContinueDentalPlan,
      "IsSearchAppointmnetByClinicID": false,
      "DateofBirth": authUser.dateofBirth
    };

    dynamic localRes;

    await baseAppClient.post(GET_DOCTORS_LIST_URL, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request);
    return Future.value(localRes);
  }

  Future<Map> getChiefComplaintDoctorList(int chiefComplaintID, int projectID, int languageID, BuildContext context, {doctorId}) async {
    Map<String, dynamic> request;

    if (await this.sharedPref.getObject(USER_PROFILE) != null) {
      var data = AuthenticatedUser.fromJson(await this.sharedPref.getObject(USER_PROFILE));
      authUser = data;
    }

    if (await this.sharedPref.getDouble(USER_LAT) != null && await this.sharedPref.getDouble(USER_LONG) != null) {
      lat = await this.sharedPref.getDouble(USER_LAT);
      long = await this.sharedPref.getDouble(USER_LONG);
    }

    var languageID = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');
    Request req = appGlobal.getPublicRequest();
    request = {
      "ChiefComplaintID": chiefComplaintID,
      "ProjectID": projectID,
      // "VersionID": 5.6,
      // "Channel": 3,
      "LanguageID": languageID,
      // "IPAdress": req.IPAdress,
      // "generalid": req.generalid,
      "SessionID": null,
      "isDentalAllowedBackend": true,
      "Latitude": lat != null ? lat.toString() : "0.0",
      "Longitude": long != null ? long.toString() : "0.0",
      "DeviceTypeID": req.DeviceTypeID,
      "IsPublicRequest": true
    };

    dynamic localRes;

    await baseAppClient.post(GET_DENTAL_DOCTORS_LIST_URL, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request);
    return Future.value(localRes);
  }

  Future<Map> getCountries() async {
    Map<String, dynamic> request = {};
    dynamic localRes;
    await baseAppClient.post(GET_NATIONALITY, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request);
    return Future.value(localRes);
  }

  Future<Map> getInPatientAdvancePaymentHistory(int projectID, int admissionNo, int admissionReqNo, context) async {
    Map<String, dynamic> request;
    request = {"ProjectID": projectID, "AdmissionNo": admissionNo, "AdmissionReqNo": admissionReqNo};

    dynamic localRes;

    await baseAppClient.post(GET_INPATIENT_PAID_ADVANCE_PAYMENT, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }

  Future<Map> getInPatientPaymentLink(int paymentRequestID, int projectID, int admissionNo, int orderID, String name, String email, num amount, String nationalID, int clinicID, context) async {
    Map<String, dynamic> request;
    // request = {"ProjectID": projectID, "AdmissionReqNo": admissionReqNo, "AdmissionNo": admissionNo};

    request = {
      "ProjectID": projectID,
      "PaymentRequestId": paymentRequestID,
      "ClientOrderID": orderID,
      "OrderDescription": "InPatient Advance Payment",
      "CustomerName": name,
      "CustomerEmail": email,
      "Amount": amount,
      "IsPaid": 0,
      "AppointmentID": admissionNo.toString(),
      "PaymentOption": "0",
      "PaymentReferenceNumber": admissionNo,
      "SourceType": "1",
      "NationalID": nationalID,
      "ClinicID": clinicID,
      "createdBy": 102
    };

    dynamic localRes;

    await baseAppClient.post(GET_INPATIENT_ADVANCE_PAYMENT_LINK, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }

  Future<Map> submitBirthNotification(num motherMRN, int projectID, String fatherNameAR, String fatherNameEN, String babyNameAR, String babyNameEN, String contactNum, String idImage, context) async {
    Map<String, dynamic> request;
    request = {
      "ProjectID": projectID,
      "mothermrn": motherMRN,
      "fatherName": fatherNameEN,
      "fatherNameAr": fatherNameAR,
      "babyName": babyNameEN,
      "babyNameAr": babyNameEN,
      "contactNumber": contactNum,
      "iDImage": idImage,
    };

    dynamic localRes;

    await baseAppClient.post(SAVE_BIRTH_NOTIFICATION, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }

  Future<Map> insertInPatientOrder(
      GetAdmissionInfoResponseModel getAdmissionInfoResponseModel, int typeID, String patientName, String patientNameAR, String patientMobileNo, String comments, context) async {
    Map<String, dynamic> request;
    request = {
      "ProjectID": getAdmissionInfoResponseModel.projectID,
      "TypeID": typeID,
      "RoomNo": getAdmissionInfoResponseModel.roomID,
      "GenderId": 2,
      "AdmissionNo": getAdmissionInfoResponseModel.admissionNo,
      "prescriptionNo": 250420,
      "patientName": patientName,
      "patientNameN": patientNameAR,
      "PatientMobileNumber": patientMobileNo,
      "comments": comments
    };

    dynamic localRes;

    await baseAppClient.post(INSERT_INPATIENT_ORDER, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }

  Future<Map> getGeneralInstructions(int projectID, int languageID, context) async {
    Map<String, dynamic> request;
    request = {"ProjectID": projectID, "LanguageID": languageID};

    dynamic localRes;

    await baseAppClient.post(GET_GENERAL_INSTRUCTIONS, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }

  Future<Map> getMedicalInstructions(int projectID, int languageID, context) async {
    Map<String, dynamic> request;
    request = {"ProjectID": projectID, "LanguageID": languageID};

    dynamic localRes;

    await baseAppClient.post(GET_MEDICAL_INSTRUCTIONS, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }

  Future<Map> getInPatientAdvancePaymentRequests(int projectID, int admissionNo, int admissionReqNo, int languageID, context) async {
    Map<String, dynamic> request;
    request = {"ProjectID": projectID, "AdmissionReqNo": admissionReqNo, "AdmissionNo": admissionNo, "LanguageID": languageID};

    dynamic localRes;

    await baseAppClient.post(GET_INPATIENT_ADVANCE_PAYMENT_REQUESTS, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }

  Future<Map> getBirthNotification(num motherMRN, int projectID, int languageID, context) async {
    Map<String, dynamic> request;
    request = {"mothermrn": motherMRN, "LanguageID": languageID, "ProjectID": projectID};

    dynamic localRes;

    await baseAppClient.post(GET_BIRTH_NOTIFICATION, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }

  Future<Map> getAdmittedPatientDetails(int patientID, int projectID, int admissionNo, int buildingID, int floorID, int nursingStationID, context) async {
    Map<String, dynamic> request;
    request = {"ProjectID": projectID, "PatientID": patientID, "AdmissionNo": admissionNo, "BuildingID": buildingID, "FloorID": floorID, "NursingStationID": nursingStationID};

    dynamic localRes;

    await baseAppClient.post(GET_ADMITTED_PATIENTS, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }

  Future<Map> getCurrentWeekIDAndDay(BuildContext context) async {
    dynamic localRes;
    await baseAppClient.get(GET_CURRENT_WEEKID_WEEKDAY, isExternal: false, isRCService: false, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    });
    return localRes;
  }

  Future<Map> getMealsOfScheduleID(int projectID, int admissionNo, int dietType, int weekID, int weekDay, context) async {
    Map<String, dynamic> request;
    request = {
      "ProjectID": projectID,
      "AdmissionNo": admissionNo,
      "BuildingID": 0,
      "FloorID": 0,
      "NursingStationID": 0,
      "DietType": dietType,
      "WeekID": weekID,
      "WeekDay": weekDay,
    };

    dynamic localRes;

    await baseAppClient.post(GET_MEALS_OF_SCHEDULE_ID, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }

  Future<Map> getDefaultItemsOfScheduleID(int projectID, int scheduleID, context) async {
    Map<String, dynamic> request;
    request = {
      "ProjectID": projectID,
      "MealScheduleID": scheduleID,
    };

    dynamic localRes;

    await baseAppClient.post(GET_MEAL_ITEMS_OF_SCHEDULE_ID, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }

  Future<Map> placeMealPlanOrder(int projectID, int scheduleID, String itemIDList, int patientID, int admissionNo, bool isCompanion, bool isFasting, context) async {
    Map<String, dynamic> request;
    request = {
      "ProjectID": projectID,
      "ItemIDList": itemIDList,
      "IsActive": true,
      "OrderDate": DateUtil.getISODateFormat(DateTime.now()).replaceAll("T", " "),
      "PatientID": patientID,
      "AdmissionNo": admissionNo,
      "IsCompanion": isCompanion,
      "IsFasting": isFasting,
      "CreatedBy": patientID,
      "EditedBy": patientID,
      "MealScheduleID": scheduleID,
      "MealStatus": 1,
      "ItemStatus": 0,
      "ConfirmedBy": patientID,
      "Remarks": "",
      "IsVIP": false,
      "IsFixedRemarks": false
    };

    dynamic localRes;

    await baseAppClient.post(PLACE_MEAL_PLAN_ORDER, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }

  Future<Map> checkIfInPatientAPI(context) async {
    Map<String, dynamic> request;
    request = {
      "IsActiveAppointment": false,
    };

    dynamic localRes;

    await baseAppClient.post(CHECK_IF_PATIENT_ADMITTED, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }

  Future<Map> getWeCareURL(int projectID) async {
    Map<String, dynamic> request = {"ProjectID": projectID};
    dynamic localRes;
    await baseAppClient.post(GET_WE_CARE_TOUR_URL, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request);
    return Future.value(localRes);
  }

  Future<Map> getDentalInstructions() async {
    Map<String, dynamic> request = {};
    dynamic localRes;
    await baseAppClient.post(GET_DENTAL_INSTRUCTIONS, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request);
    return Future.value(localRes);
  }

  Future<Map> getEROnlineCheckInPaymentDetails(int projectID, int clinicID) async {
    Map<String, dynamic> request = {"ProjectID": projectID, "ClinicID": clinicID};
    dynamic localRes;
    await baseAppClient.post(GET_ER_ONLINE_PAYMENT_DETAILS, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request);
    return Future.value(localRes);
  }

  Future<Map> checkIfPatientHasArrived(int projectID, int clinicID) async {
    Map<String, dynamic> request = {"ProjectID": projectID, "ClinicID": clinicID};
    dynamic localRes;
    await baseAppClient.post(CHECK_IF_PATIENT_ARRIVED_ER_ONLINE_CHECKIN, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }

  Future<Map> checkPatientERAdvanceBalance(int clinicID) async {
    Map<String, dynamic> request = {"ClinicID": clinicID};
    dynamic localRes;
    await baseAppClient.post(CHECK_PATIENT_ER_ADVANCE_BALANCE, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }

  Future<Map> getProjectIDFromNFC(String nfcID) async {
    Map<String, dynamic> request = {"nFC_Code": nfcID};
    dynamic localRes;
    await baseAppClient.post(GET_PROJECT_FROM_NFC, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }

  Future<Map> getDischargeMedicationOrder(GetAdmissionInfoResponseModel getAdmissionInfoResponseModel) async {
    Map<String, dynamic> request;
    request = {
      "ProjectID": getAdmissionInfoResponseModel.projectID,
      "ClinicID": getAdmissionInfoResponseModel.clinicID,
      "DoctorID": getAdmissionInfoResponseModel.doctorID,
      "AdmissionNo": getAdmissionInfoResponseModel.admissionNo
    };

    // request = {
    //   "ProjectID": 12,
    //   "VersionID": 10.8,
    //   "Channel": 3,
    //   "LanguageID": 2,
    //   "IPAdress": "10.20.10.20",
    //   "generalid": "Cs2020@2016\$2958",
    //   "DeviceTypeID": 2,
    //   "PatientType": 1,
    //   "PatientTypeID": 1,
    //   "TokenID": "@dm!n",
    //   "PatientID": 869588,
    //   "PatientOutSA": 0,
    //   "SessionID": "rVuK3nzN4UKN0SW95un0jQ==",
    //   "ClinicID": 2,
    //   "DoctorID": "7600",
    //   "AdmissionNo": 2011001258
    // };

    dynamic localRes;

    await baseAppClient.post(INPATIENT_DISCHARGE_MEDICATIONS, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }

  Future<Map> insertForGeneralAdmissionConsent(int patientID, int admissionReqNo, int clinicID, int projectID, int status, context) async {
    Map<String, dynamic> request;
    request = {
      "AdmissionRequestNo": admissionReqNo,
      "ClinicID": clinicID,
      "CreatedBy": 102,
      "PatientID": patientID,
      "ProjectID": projectID,
      "status": status,
      "searchKey": "",
      "pageIndex": 0,
      "pageSize": 0,
    };

    dynamic localRes;

    await baseAppClient.post(INSERT_GENERAL_ADMISSION_CONSENT, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isAllowAny: true);
    return Future.value(localRes);
  }
}
