import 'dart:io';

import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/service/base_service.dart';
import 'package:diplomaticquarterapp/models/Authentication/authenticated_user.dart';
import 'package:diplomaticquarterapp/models/LiveCare/ApplePayInsertRequest.dart';
import 'package:diplomaticquarterapp/models/tamara_request_model.dart';
import 'package:diplomaticquarterapp/services/authentication/auth_provider.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
import 'package:flutter/material.dart';

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

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

  Future<Map> getLivecareClinics(BuildContext context) 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;
    }

    request = {"Age": authUser.age != null ? authUser.age : 0, "PatientID": authUser.patientID != null ? authUser.patientID : 0, "Gender": authUser.gender != null ? authUser.gender : 0};

    dynamic localRes;

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

  Future<Map> getLiveCareScheduledClinics(BuildContext context) 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;
    }

    request = {"Age": authUser.age != null ? authUser.age : 0, "PatientID": authUser.patientID != null ? authUser.patientID : 0, "Gender": authUser.gender != null ? authUser.gender : 0};

    dynamic localRes;

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

  Future<Map> getLiveCareScheduledDoctorList(BuildContext context, int serviceID) 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;
    }

    request = {
      "ServiceID": serviceID,
      "Age": authUser.age != null ? authUser.age : 0,
      "PatientID": authUser.patientID != null ? authUser.patientID : 0,
      "Gender": authUser.gender != null ? authUser.gender : 0
    };

    dynamic localRes;

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

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

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

    dynamic localRes;

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

  Future<Map> getPatientInfoByQR(String clientID, BuildContext context) async {
    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');
    Map<String, dynamic> request;
    request = {"clientid": clientID, "patientid": authUser.patientID, "isOutKsa": authUser.outSA == 1 ? true : false, "appLang": languageID == 'ar' ? "1" : "2"};

    dynamic localRes;

    await baseAppClient.post("https://vcallapi.hmg.com/api/PharmaLiveCare/getPatientInfoByQR", onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: request, isExternal: true);
    return Future.value(localRes);
  }

  Future<Map> getLivecareClinicTiming(int serviceID, BuildContext context) 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;
    }

    request = {
      "ServiceID": serviceID,
      "Age": authUser.age != null ? authUser.age : 0,
      "PatientID": authUser.patientID != null ? authUser.patientID : 0,
      "Gender": authUser.gender != null ? authUser.gender : 0
    };

    dynamic localRes;

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

  Future<Map> getERAppointmentFees(int serviceID, BuildContext context) 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;
    }

    request = {
      "ServiceID": serviceID,
      "ProjectID": 12,
      "PatientID": authUser.patientID != null ? authUser.patientID : 0,
      "Age": authUser.age != null ? authUser.age : 0,
      "Gender": authUser.gender != null ? authUser.gender : 0
    };

    dynamic localRes;

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

  Future<Map> getERAppointmentTime(int serviceID, BuildContext context) 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;
    }

    request = {
      "ServiceID": serviceID,
      "ProjectID": 12,
      "Age": authUser.age != null ? authUser.age : 0,
      "PatientID": authUser.patientID != null ? authUser.patientID : 0,
      "Gender": authUser.gender != null ? authUser.gender : 0
    };

    dynamic localRes;

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

  Future<Map> addNewCallForPatientER(int serviceID, String clientRequestID, BuildContext context) async {
    Map<String, dynamic> request;

    String deviceToken;
    String voipToken = await AppSharedPreferences().getString(ONESIGNAL_APNS_TOKEN);
    getDeviceToken().then((value) {
      print(value);
      deviceToken = value;
    });

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

    request = {
      "ErServiceID": serviceID,
      "ClientRequestID": clientRequestID,
      "DeviceToken": deviceToken,
      "VoipToken": voipToken,
      "IsFlutter": true,
      "Latitude": await this.sharedPref.getDouble(USER_LAT),
      "Longitude": await this.sharedPref.getDouble(USER_LONG),
      "DeviceType": Platform.isIOS ? 'iOS' : 'Android',
      "Age": authUser.age != null ? authUser.age : 0,
      "PatientID": authUser.patientID != null ? authUser.patientID : 0,
      "Gender": authUser.gender != null ? authUser.gender : 0,
      "IsVoip": Platform.isIOS ? true : false
    };

    dynamic localRes;

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

  Future<String> getDeviceToken() async {
    String deviceToken = await sharedPref.getString(PUSH_TOKEN);
    return deviceToken;
  }

  Future<Map> cancelLiveCareRequest(int vc_id, BuildContext context) 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;
    }
    request = {"VCID": vc_id};

    dynamic localRes;

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

  Future<Map> applePayInsertRequest(ApplePayInsertRequest applePayInsertRequest, BuildContext context) async {
    if (await this.sharedPref.getObject(USER_PROFILE) != null) {
      var data = AuthenticatedUser.fromJson(await this.sharedPref.getObject(USER_PROFILE));
      authUser = data;
    }
    dynamic localRes;
    await baseAppClient.post(APPLE_PAY_INSERT_REQUEST, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: applePayInsertRequest.toJson());
    return Future.value(localRes);
  }

  Future<Map> sendLiveCareInvoiceEmail(String appoNo, int projectID, String emailAddress, BuildContext context) 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;
    }
    request = {"To": emailAddress, "ProjectID": projectID, "AppointmentNo": appoNo};

    dynamic localRes;

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

  Future<Map> endCallAPI(String sessionID, int sessionStatus, BuildContext context) 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;
    }
    request = {"Open_SessionID": sessionID, "SessionStatus": sessionStatus, "SessionEndedBy": "Patient"};

    dynamic localRes;

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

  Future<Map> tamaraInsertRequest(TamaraRequestModel tamaraInsertRequest, BuildContext context) async {
    dynamic localRes;
    await baseAppClient.post(TAMARA_REQUEST_INSERT, onSuccess: (response, statusCode) async {
      localRes = response;
    }, onFailure: (String error, int statusCode) {
      throw error;
    }, body: tamaraInsertRequest.toJson());
    return Future.value(localRes);
  }

  Future<Map> getOneSignalVOIPToken(String voipToken, BuildContext context) async {
    Map<String, dynamic> request;
    request = {"app_id": "b87a754b-9a2a-437c-960b-39a079c57586", "identifier": voipToken, "device_type": 0};

    dynamic localRes;

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

  Future<Map> cancelPharmaLiveCareRequest(String pharmaClientRequestID, BuildContext context) async {
    Map<String, dynamic> request;
    request = {"clientid": pharmaClientRequestID, "status": "Cancel"};

    dynamic localRes;

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

}
