import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/config/shared_pref_kay.dart';
import 'package:hmg_patient_app/core/model/er/PatientER.dart';
import 'package:hmg_patient_app/core/service/base_service.dart';
import 'package:hmg_patient_app/models/Appointments/AppoimentAllHistoryResultList.dart';
import 'package:hmg_patient_app/models/Appointments/DoctorListResponse.dart';
import 'package:hmg_patient_app/pages/MyAppointments/models/DoctorScheduleResponse.dart';
import 'package:flutter/cupertino.dart';

class MedicalService extends BaseService {
  List<AppoitmentAllHistoryResultList> appoitmentAllHistoryResultList =[];
  List<DoctorScheduleResponse> doctorScheduleResponse =[];
  List<String> freeSlots = [];
  getAppointmentHistory({bool isActiveAppointment = false, bool isForTimeLine = false}) async {
    hasError = false;
    super.error = "";
    Map<String, dynamic> body = Map();
    if (isActiveAppointment) {
      body['IsActiveAppointment'] = true;
      body['isDentalAllowedBackend'] = false;
    }
    body['IsForTimeLine'] = isForTimeLine;
    var appoHistory = await sharedPref.getObject(APPOINTMENT_HISTORY_MEDICAL);
    if (appoHistory != null) {
      appoitmentAllHistoryResultList.clear();
      appoHistory.forEach((appoitment) {
        appoitmentAllHistoryResultList.add(AppoitmentAllHistoryResultList.fromJson(appoitment));
      });
    } else {
      await baseAppClient.post(GET_PATIENT_APPOINTMENT_HISTORY, onSuccess: (response, statusCode) async {
        appoitmentAllHistoryResultList.clear();
        response['AppoimentAllHistoryResultList'].forEach((appoitment) {
          appoitmentAllHistoryResultList.add(AppoitmentAllHistoryResultList.fromJson(appoitment));
        });
        sharedPref.setObject(APPOINTMENT_HISTORY_MEDICAL, response['AppoimentAllHistoryResultList']);
      }, onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      }, body: body);
    }
  }

  addAmbulanceRequest({required PatientER patientER}) async {
    hasError = false;
    super.error = "";
    Map<String, dynamic> body = Map();
    body['RequesterFileNo'] = user.patientID;
    body['RequesterMobileNo'] = user.mobileNumber;
    body['RequesterIsOutSA'] = user.outSA;
    await baseAppClient.post(GET_PATIENT_APPOINTMENT_HISTORY, onSuccess: (response, statusCode) async {}, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  getSchedule(DoctorList doctorRequest) async {
    Map<String, dynamic> request;
    request = {'DoctorID': doctorRequest.doctorID, 'ProjectID': doctorRequest.projectID, 'ClinicID': doctorRequest.clinicID, 'DoctorWorkingHoursDays': 90};
    dynamic localRes;
    await baseAppClient.post(DOCTOR_SCHEDULE_URL, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request);
    doctorScheduleResponse.clear();
    localRes['List_DoctorWorkingHoursTable'].forEach((item) => {doctorScheduleResponse.add(DoctorScheduleResponse.fromJson(item))});
  }

  getFreeSlot(DoctorList doctorRequest) async {
    Map<String, dynamic> request;
    request = {'DoctorID': doctorRequest.doctorID, 'ProjectID': doctorRequest.projectID, 'ClinicID': doctorRequest.clinicID, 'DoctorWorkingHoursDays': 90};
    dynamic localRes;
    await baseAppClient.post(GET_DOCTOR_FREE_SLOTS, onSuccess: (response, statusCode) async {
      localRes = response;
      freeSlots.clear();
      localRes['FreeTimeSlots'].forEach((item) => freeSlots.add(item));
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
      // throw error;
    }, body: request);
    // freeSlots.clear();
    // localRes['FreeTimeSlots'].forEach((item) => freeSlots.add(item));
    // localRes['List_DoctorWorkingHoursTable'].forEach((item) =>
    //     {doctorScheduleResponse.add(DoctorScheduleResponse.fromJson(item))});
  }
}
