import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/model/family-file/add_family_file_request.dart';
import 'package:diplomaticquarterapp/core/model/family-file/insert_share_file_request.dart';
import 'package:diplomaticquarterapp/core/service/client/base_app_client.dart';
import 'package:diplomaticquarterapp/models/Authentication/authenticated_user.dart';
import 'package:diplomaticquarterapp/models/Authentication/check_activation_code_response.dart';
import 'package:diplomaticquarterapp/models/Authentication/send_activation_request.dart';
import 'package:diplomaticquarterapp/models/FamilyFiles/GetAllSharedRecordByStatusResponse.dart';
import 'package:diplomaticquarterapp/models/FamilyFiles/GetAllSharedRecordsByStatusReq.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:flutter/cupertino.dart';

// SharedPreferences sharedPref = new SharedPreferences();
enum APP_STATUS { LOADING, UNAUTHENTICATED, AUTHENTICATED }
AppSharedPreferences sharedPref = new AppSharedPreferences();

const String GET_SHARED_RECORD_BY_STATUS =
    'Services/Authentication.svc/REST/GetAllSharedRecordsByStatus';
const String ADD_FAMILY_FILES =
    'Services/Patients.svc/REST/ShareFamilyFileService';
const String SEND_FAMILY_FILE_ACTIVATION =
    'Services/Authentication.svc/REST/SendActivationCodeForFamilyFile';
const String INSERT_PATIENT_FILE =
    'Services/Authentication.svc/REST/Insert_SharePatientFile';
const String CHECK_ACTIVATION_CODE =
    'Services/Authentication.svc/REST/CheckActivationCodeForFamilyFile';

const String UPDATE_FILE_STATUS =
    'Services/Authentication.svc/REST/Update_FileStatus';
const String REMOVE_FILE_STATUS =
    'Services/Authentication.svc/REST/ActiveDeactive_PatientFile';

const String ACTIVATION_CODE_URL =
    "Services/Authentication.svc/REST/CheckActivationCode";

class FamilyFilesProvider with ChangeNotifier {
  bool isLogin = false;
  bool isLoading = true;
  dynamic authenticatedUser;

  Future<GetAllSharedRecordsByStatusResponse> getSharedRecordByStatus() async {
    try {
      dynamic localRes;
      var request = GetAllSharedRecordsByStatusReq();
      request.status = 0;
      await new BaseAppClient().post(GET_SHARED_RECORD_BY_STATUS,
          onSuccess: (dynamic response, int statusCode) {
        localRes = response;
      }, onFailure: (String error, int statusCode) {
        AppToast.showErrorToast(message: error);
        throw error;
      }, body: request.toJson());
      sharedPref.setObject(FAMILY_FILE, localRes);
      return Future.value(
          GetAllSharedRecordsByStatusResponse.fromJson(localRes));
    } catch (error) {
      print(error);
      throw error;
    }
  }

  Future<dynamic> addFamilyFile(AddFamilyFileReq request) async {
    try {
      dynamic localRes;
      // var request = GetAllSharedRecordsByStatusReq();
      //request.status = 0;
      await new BaseAppClient().post(ADD_FAMILY_FILES,
          onSuccess: (dynamic response, int statusCode) {
        localRes = response;
      }, onFailure: (String error, int statusCode) {
        AppToast.showErrorToast(message: error);
        throw error;
      }, body: request.toJson());
      //sharedPref.setObject(FAMILY_FILE, localRes);
      return Future.value(localRes);
    } catch (error) {
      print(error);

      return Future.value(error);
    }
  }

  Future<dynamic> insertNewMember(InsertSharePatientFileReq request) async {
    try {
      dynamic localRes;
      // var request = GetAllSharedRecordsByStatusReq();
      //request.status = 0;
      await new BaseAppClient().post(INSERT_PATIENT_FILE,
          onSuccess: (dynamic response, int statusCode) {
        localRes = response;
      }, onFailure: (String error, int statusCode) {
        throw error;
      }, body: request.toJson());

      return Future.value(localRes);
    } catch (error) {
      print(error);
      throw error;
    }
  }

  Future<dynamic> sendActivationCode(
      cellNumber, zipCode, patientIdentificationID) async {
    try {
      dynamic localRes;
      var request = SendActivationRequest();
      request.zipCode = zipCode;
      request.patientMobileNumber = int.parse(cellNumber);
      request.patientIdentificationID = patientIdentificationID;
      request.projectOutSA = zipCode == '966' ? 0 : 1;
      request.loginType = request.searchType = 1;
      request.oTPSendType = 1;
      request.isRegister = false;
      await new BaseAppClient().post(SEND_FAMILY_FILE_ACTIVATION,
          onSuccess: (dynamic response, int statusCode) {
        localRes = response;
      }, onFailure: (String error, int statusCode) {
        AppToast.showErrorToast(message: error);
        throw error;
      }, body: request.toJson());

      return Future.value(localRes);
    } catch (error) {
      print(error);
      throw error;
    }
  }

  Future<dynamic> checkActivationCode(
      loginTokenID, activationCode, indentification, mobileNo) async {
    try {
      dynamic localRes;
      Map<String, dynamic> request = {};
      // request['ZipCode'] = user.ZipCode;
      request['PatientMobileNumber'] = mobileNo;
      request['PatientIdentificationID'] = indentification;
      request['LogInTokenID'] = loginTokenID;
      request['activationCode'] = activationCode;
      // this.authService.authenticateRequest(request);
      await new BaseAppClient().post(CHECK_ACTIVATION_CODE,
          onSuccess: (dynamic response, int statusCode) {
        localRes = response;
      }, onFailure: (String error, int statusCode) {
        AppToast.showErrorToast(message: error);
        throw error;
      }, body: request);

      return Future.value(localRes);
    } catch (error) {
      print(error);
      throw error;
    }
  }

  Future<dynamic> acceptAndRejectRecievedRequests(id, stauts) async {
    try {
      dynamic localRes;
      Map<String, dynamic> request = {};
      request['ID'] = id;
      request['Status'] = stauts;
      await new BaseAppClient().post(UPDATE_FILE_STATUS,
          onSuccess: (dynamic response, int statusCode) {
        localRes = response;
      }, onFailure: (String error, int statusCode) {
        AppToast.showErrorToast(message: error);
        throw error;
      }, body: request);

      return Future.value(localRes);
    } catch (error) {
      print(error);
      throw error;
    }
  }

  Future<dynamic> deativateActivateMemberFile(request) async {
    try {
      dynamic localRes;

      await new BaseAppClient().post(REMOVE_FILE_STATUS,
          onSuccess: (dynamic response, int statusCode) {
        localRes = response;
      }, onFailure: (String error, int statusCode) {
        AppToast.showErrorToast(message: error);
        throw error;
      }, body: request);

      return Future.value(localRes);
    } catch (error) {
      print(error);
      throw error;
    }
  }

  Future<dynamic> silentLoggin(GetAllSharedRecordsByStatusList switchUser,
      {onSuccess}) async {
    //var currentUser = this.authService.getAuthenticatedUser();
    var currentUser =
        AuthenticatedUser.fromJson(await sharedPref.getObject(USER_PROFILE));
    //const request = new SwitchUserRequest();
    Map<String, dynamic> request = {};
    request['LogInTokenID'] = '';
    request['PatientOutSA'] = currentUser.outSA; //? 1 : 0;
    request['PatientMobileNumber'] =
        switchUser.mobileNumber; //['MobileNumber'];
    request['SearchType'] = 2;
    request['SuperUser'] = currentUser.patientID; //currentUser.PatientID;
    request['PatientIdentificationID'] = '';
    request['IsSilentLogin'] = true;
    if (switchUser.mobileNumber != null &&
        switchUser.patientID != currentUser.patientID) {
      request['PatientID'] = switchUser.patientID; //['PatientID'];
    } else {
      request['PatientID'] = switchUser.responseID; //['ResponseID'];
    }
    request['ZipCode'] = currentUser.outSA == 1 ? "971" : "966";
    request['activationCode'] = '0000';
    request['isRegister'] = false;
    try {
      dynamic localRes;

      await new BaseAppClient().post(ACTIVATION_CODE_URL,
          onSuccess: (dynamic response, int statusCode) {
        localRes = response;
      }, onFailure: (String error, int statusCode) {
        AppToast.showErrorToast(message: error);
        throw error;
      }, body: request);

      return Future.value(localRes);
    } catch (error) {
      print(error);
      throw error;
    }
  }
}
