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/AlHabibMedicalService/HealthCalculator/ClinicCategory.dart';
import 'package:hmg_patient_app/core/model/AlHabibMedicalService/HealthCalculator/DiseasesByClinic.dart';
import 'package:hmg_patient_app/core/model/health_calcolator/get_doctor_by_time_model.dart';
import 'package:hmg_patient_app/core/model/health_calcolator/get_doctor_list_calcolation_model.dart';
import 'package:hmg_patient_app/core/service/base_service.dart';
import 'package:flutter/cupertino.dart';

import '../../../../uitl/app_toast.dart';

class BariatricsService extends BaseService {
  List<ClinicCategory> clinicCategoryList = [];
  List<DiseasesByClinic> diseasesList = [];
  List<GetDoctorListModel> doctorList = [];
  List<DoctorListByTimeModel> doctorListByTime = [];

  double? lat;
  double? long;

  Future getClinicCategory() async {
    hasError = false;
    Map<String, dynamic> body = Map();
    var languageID = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');

    body['ClinicID'] = 108; // hint: it is static depend on IONIC project
    body['isDentalAllowedBackend'] = false;
    body['LanguageID'] = languageID == 'ar' ? 1 : 2;

    await baseAppClient.post(GET_CLINIC_CATEGORY, onSuccess: (dynamic response, int statusCode) {
      clinicCategoryList.clear();
      if (response['ClinicCategoryList'] != null)
        response['ClinicCategoryList'].forEach((city) {
          clinicCategoryList.add(ClinicCategory.fromJson(city));
        });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future getDiseaseByClinicId() async {
    hasError = false;
    Map<String, dynamic> body = Map();
    body['ClinicID'] = 108; // hint: it is static depend on IONIC project

    await baseAppClient.post(GET_DISEASE_BY_CLINIC_ID, onSuccess: (dynamic response, int statusCode) {
      diseasesList.clear();
      if (response['DiseasesByClinicList'] != null)
        response['DiseasesByClinicList'].forEach((city) {
          diseasesList.add(DiseasesByClinic.fromJson(city));
        });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future searchDoctorsByTime(DiseasesByClinic disease) async {
    hasError = false;
    var languageID = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');

    Map<String, dynamic> body = Map();
    body['ClinicID'] = 108; // hint: it is static depend on IONIC project
    body['ContinueDentalPlan'] = false;
    body['DiseaseID'] = disease.diseasesID;
    body['IsGetNearAppointment'] = false;
    body['IsSearchAppointmnetByClinicID'] = true;
    body['LanguageID'] = languageID == 'ar' ? 1 : 2;
    body['Latitude'] = 0;
    body['License'] = true;
    body['Longitude'] = 0;
    body['isDentalAllowedBackend'] = false;

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

  Future getDoctorList({required DiseasesByClinic disease}) async {
    hasError = false;
    var languageID = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');

    Map<String, dynamic> body = Map();
    body['ClinicID'] = 108; // hint: it is static depend on IONIC project
    body['ContinueDentalPlan'] = false;
    body['IsGetNearAppointment'] = false;
    body['IsSearchAppointmnetByClinicID'] = true;
    body['LanguageID'] = languageID == 'ar' ? 1 : 2;
    body['Latitude'] = 0;
    body['License'] = true;
    body['Longitude'] = 0;
    body['PatientID'] = 0;
    body['isDentalAllowedBackend'] = false;

    if (disease != null) body['DiseaseID'] = disease.diseasesID;

    await baseAppClient.post(GET_DOCTOR_LIST_BY_TIME, onSuccess: (dynamic response, int statusCode) {
      doctorListByTime.clear();
      if (response['DoctorList'] != null)
        response['DoctorList'].forEach((city) {
          doctorListByTime.add(DoctorListByTimeModel.fromJson(city));
        });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: body);
  }

  Future getCalculationDoctors({required int calculationID}) async {

    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);
    }

    hasError = false;
    var languageID = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');

    Map<String, dynamic> body = Map();
    body['CalculationID'] = calculationID; // hint: it is static depend on IONIC project
    body['LanguageID'] = languageID == 'ar' ? 1 : 2;
    body['Latitude'] = lat != null ? lat.toString() : 0;
    body['Longitude'] = long != null ? long.toString() : 0;

    dynamic localRes;

    await baseAppClient.post(GET_DOCTOR_LIST_CALCULATION, onSuccess: (dynamic response, int statusCode) {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      AppToast.showErrorToast(message: error);
      hasError = true;
      super.error = error;
    }, body: body);
    return Future.value(localRes);
  }
}
