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/Request.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;
    }

    var languageID = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');
    Request req = appGlobal.getPublicRequest();
    request = {
      "LanguageID": languageID == 'ar' ? 1 : 2,
      "IPAdress": "10.20.10.20",
      "VersionID": req.VersionID,
      "Channel": req.Channel,
      "generalid": 'Cs2020@2016\$2958',
      "PatientOutSA": 0,
      "TokenID": "",
      "DeviceTypeID": req.DeviceTypeID,
      "SessionID": "YckwoXhUmWBsnHKEKig",
      "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;
    }

    var languageID = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');
    Request req = appGlobal.getPublicRequest();
    request = {
      "LanguageID": languageID == 'ar' ? 1 : 2,
      "IPAdress": "10.20.10.20",
      "VersionID": req.VersionID,
      "Channel": req.Channel,
      "generalid": 'Cs2020@2016\$2958',
      "PatientOutSA": 0,
      "TokenID": "",
      "DeviceTypeID": req.DeviceTypeID,
      "SessionID": "YckwoXhUmWBsnHKEKig",
      "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;
    }

    var languageID = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');
    Request req = appGlobal.getPublicRequest();
    request = {
      "LanguageID": languageID == 'ar' ? 1 : 2,
      "IPAdress": "10.20.10.20",
      "VersionID": req.VersionID,
      "Channel": req.Channel,
      "generalid": 'Cs2020@2016\$2958',
      "PatientOutSA": 0,
      "TokenID": "",
      "DeviceTypeID": req.DeviceTypeID,
      "ServiceID": serviceID,
      "SessionID": "YckwoXhUmWBsnHKEKig",
      "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;

    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 = {
      "LanguageID": languageID == 'ar' ? 1 : 2,
      "TokenID": "",
      "SessionID": "YckwoXhUmWBsnHKEKig"
    };

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

    var languageID = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');
    Request req = appGlobal.getPublicRequest();
    request = {
      "LanguageID": languageID == 'ar' ? 1 : 2,
      "IPAdress": "10.20.10.20",
      "VersionID": req.VersionID,
      "Channel": req.Channel,
      "generalid": 'Cs2020@2016\$2958',
      "PatientOutSA": 0,
      "ServiceID": serviceID,
      "DeviceTypeID": req.DeviceTypeID,
      "SessionID": "YckwoXhUmWBsnHKEKig",
      "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;
    }

    var languageID = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');
    Request req = appGlobal.getPublicRequest();
    request = {
      "LanguageID": languageID == 'ar' ? 1 : 2,
      "IPAdress": "10.20.10.20",
      "VersionID": req.VersionID,
      "Channel": req.Channel,
      "generalid": 'Cs2020@2016\$2958',
      "PatientOutSA": 0,
      "ServiceID": serviceID,
      "ProjectID": 15,
      "DeviceTypeID": req.DeviceTypeID,
      "PatientType": authUser.patientType != null ? authUser.patientType : 0,
      "PatientTypeID": authUser.patientType != null ? authUser.patientType : 0,
      "SessionID": "YckwoXhUmWBsnHKEKig",
      "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_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;
    }

    var languageID = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');
    Request req = appGlobal.getPublicRequest();
    request = {
      "LanguageID": languageID == 'ar' ? 1 : 2,
      "IPAdress": "10.20.10.20",
      "VersionID": req.VersionID,
      "Channel": req.Channel,
      "generalid": 'Cs2020@2016\$2958',
      "PatientOutSA": 0,
      "ServiceID": serviceID,
      "ProjectID": 15,
      "DeviceTypeID": req.DeviceTypeID,
      "PatientType": authUser.patientType != null ? authUser.patientType : 0,
      "PatientTypeID": authUser.patientType != null ? authUser.patientType : 0,
      "SessionID": "YckwoXhUmWBsnHKEKig",
      "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;
    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;
    }

    var languageID = await sharedPref.getStringWithDefaultValue(APP_LANGUAGE, 'ar');
    Request req = appGlobal.getPublicRequest();
    request = {
      "LanguageID": languageID == 'ar' ? 1 : 2,
      "IPAdress": "10.20.10.20",
      "VersionID": req.VersionID,
      "Channel": req.Channel,
      "generalid": 'Cs2020@2016\$2958',
      "PatientOutSA": 0,
      "ErServiceID": serviceID,
      "ClientRequestID": clientRequestID,
      "DeviceToken": deviceToken,
      "VoipToken": "",
      "Latitude": "24.708488",
      "Longitude": "46.665925",
      "DeviceType": Platform.isIOS ? 'iOS' : 'Android',
      "PatientType": authUser.patientType != null ? authUser.patientType : 0,
      "PatientTypeID": authUser.patientType != null ? authUser.patientType : 0,
      "SessionID": "YckwoXhUmWBsnHKEKig",
      "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(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> 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);
  }
}
