import 'dart:async';

import 'package:mohem_flutter_app/api/api_client.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/consts.dart';
import 'package:mohem_flutter_app/models/basic_member_information_model.dart';
import 'package:mohem_flutter_app/models/check_mobile_app_version_model.dart';
import 'package:mohem_flutter_app/models/generic_response_model.dart';
import 'package:mohem_flutter_app/models/get_mobile_login_info_list_model.dart';
import 'package:mohem_flutter_app/models/member_login_list_model.dart';

class LoginApiClient {
  static final LoginApiClient _instance = LoginApiClient._internal();

  LoginApiClient._internal();

  factory LoginApiClient() => _instance;

  Future<GetMobileLoginInfoListModel?> getMobileLoginInfoNEW(String deviceToken, String deviceType) async {
    String url = "${ApiConsts.erpRest}Mohemm_GetMobileLoginInfo_NEW";
    Map<String, dynamic> postParams = {};
    postParams["DeviceToken"] = deviceToken;
    postParams["DeviceType"] = deviceType;
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return (responseData.mohemmGetMobileLoginInfoList?.length ?? 0) > 0 ? (responseData.mohemmGetMobileLoginInfoList!.first) : null;
    }, url, postParams);
  }

  Future<GenericResponseModel?> insertMobileLoginInfoNEW(
      String email, int sessionId, String employeeName, int loginType, String mobileNumber, String userName, String deviceToken, String deviceType) async {
    String url = "${ApiConsts.erpRest}Mohemm_InsertMobileLoginInfo";
    Map<String, dynamic> postParams = {
      "MobileNumber": mobileNumber,
      "P_USER_NAME": userName,
      "UserName": userName,
      "CompanyID": 1, // todo 'sikander' @discuss umer for companyID
      "DeviceToken": deviceToken,
      "LoginType": loginType,
      "EmployeeName": employeeName,
      "P_SESSION_ID": sessionId,
      "P_EMAIL_ADDRESS": email
    };
    postParams["DeviceToken"] = deviceToken;
    postParams["DeviceType"] = deviceType;
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      AppState().setLogged = true;
      return responseData;
    }, url, postParams);
  }

  Future<CheckMobileAppVersionModel> checkMobileAppVersion() async {
    String url = "${ApiConsts.utilitiesRest}CheckMobileAppVersion";
    Map<String, dynamic> postParams = {};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) => CheckMobileAppVersionModel.fromJson(json), url, postParams);
  }

  Future<MemberLoginListModel?> memberLogin(String username, String password) async {
    String url = "${ApiConsts.erpRest}MemberLogin";
    Map<String, dynamic> postParams = {"P_APP_VERSION": "CS", "P_LANGUAGE": "US", "P_PASSWORD": password, "P_USER_NAME": username};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      AppState().postParamsObject?.setLogInTokenID = responseData.logInTokenID;
      return responseData.memberLoginList;
    }, url, postParams);
  }

  Future<BasicMemberInformationModel?> mohemmSendActivationCodeByOTPNotificationType(int isMobileFingerPrint, String? mobileNumber, int optSendType, String? pUserName) async {
    String url = "${ApiConsts.erpRest}Mohemm_SendActivationCodebyOTPNotificationType";
    Map<String, dynamic> postParams = {"IsMobileFingerPrint": isMobileFingerPrint, "MobileNumber": mobileNumber, "OTP_SendType": optSendType, "P_USER_NAME": pUserName};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) => GenericResponseModel.fromJson(json).basicMemberInformation, url, postParams);
  }

  Future<GenericResponseModel?> checkActivationCode(bool isDeviceNFC, String? mobileNumber, String activationCode, String? pUserName) async {
    String url = "${ApiConsts.erpRest}CheckActivationCode";
    Map<String, dynamic> postParams = {"IsDeviceNFC": isDeviceNFC, "MobileNumber": mobileNumber, "activationCode": activationCode, "P_USER_NAME": pUserName};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      AppState().postParamsObject?.setTokenID = responseData.tokenID;
      AppState().postParamsObject?.mobileNumber = responseData.basicMemberInformation!.pMOBILENUMBER;
      AppState().postParamsObject?.userName = AppState().getUserName;
      AppState().postParamsObject?.pEmailAddress = responseData.basicMemberInformation!.pEMAILADDRESS;
      AppState().postParamsObject?.pSessionId = responseData.pSESSIONID;
      AppState().postParamsObject?.pUserName = AppState().getUserName;
      AppState().postParamsObject?.pSelectedEmployeeNumber = AppState().getUserName;
      AppState().postParamsObject?.setPLegislationCode = responseData.basicMemberInformation!.pLEGISLATIONCODE;
      AppState().postParamsObject?.setPayrollCodeStr = responseData.memberInformationList!.first.pAYROLLCODE;
      AppState().setBusinessCardPrivilege = responseData.businessCardPrivilege ?? false;
      return responseData;
    }, url, postParams);
  }

  Future<BasicMemberInformationModel?> getBasicUserInformation(String pAppVersion, String pUsername) async {
    String url = "${ApiConsts.erpRest}Get_BasicUserInformation";
    Map<String, dynamic> postParams = {"P_APP_VERSION": pAppVersion, "P_USER_NAME": pUsername};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) => GenericResponseModel.fromJson(json).basicMemberInformation, url, postParams);
  }

  Future<GenericResponseModel?> sendPublicActivationCode(String? mobileNumber, String? pUsername) async {
    String url = "${ApiConsts.erpRest}SendPublicActivationCode";
    Map<String, dynamic> postParams = {"MobileNumber": mobileNumber, "P_MOBILE_NUMBER": mobileNumber, "P_USER_NAME": pUsername};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      AppState().postParamsObject?.setLogInTokenID = responseData.logInTokenID;
      return responseData;
    }, url, postParams);
  }

  Future<GenericResponseModel?> checkPublicActivationCode(String activationCode, String? pUserName) async {
    String url = "${ApiConsts.erpRest}checkPublicActivationCode";
    Map<String, dynamic> postParams = {"activationCode": activationCode, "P_USER_NAME": pUserName};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      AppState().setForgetPasswordTokenID = responseData.forgetPasswordTokenID;
      return responseData;
    }, url, postParams);
  }

  Future<GenericResponseModel?> changePasswordForget(String forgetPasswordTokenID, String pNewPassword, String pConfirmNewPassword, String? pUserName) async {
    String url = "${ApiConsts.erpRest}ChangePassword_Forget";
    Map<String, dynamic> postParams = {"P_USER_NAME": pUserName, "ForgetPasswordTokenID": forgetPasswordTokenID, "P_Confirm_NEW_PASSWORD": pConfirmNewPassword, "P_NEW_PASSWORD": pNewPassword};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);

      return responseData;
    }, url, postParams);
  }

  Future<GenericResponseModel?> changePasswordFromActiveSession(String pOldPassword, String pNewPassword, String pConfirmNewPassword) async {
    String url = "${ApiConsts.erpRest}ChangePassword_FromActiveSession";
    Map<String, dynamic> postParams = {"P_OLD_PASSWORD": pOldPassword, "P_Confirm_NEW_PASSWORD": pConfirmNewPassword, "P_NEW_PASSWORD": pNewPassword};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData;
    }, url, postParams);
  }
}
