import 'dart:async';
import 'dart:convert';
import 'package:car_provider_app/models/user/change_email.dart';
import 'package:car_provider_app/models/user/change_mobile.dart';
import 'package:car_provider_app/models/user/cities.dart';
import 'package:car_provider_app/models/user/confirm_email.dart';
import 'package:car_provider_app/models/user/confirm_mobile.dart';
import 'package:car_provider_app/models/user/country.dart';
import 'package:car_provider_app/models/user/image_response.dart';
import 'package:car_provider_app/models/user/role.dart';
import 'package:car_provider_app/models/user/confirm_password.dart';
import 'package:car_provider_app/models/user/forget_password_otp_compare.dart';
import 'package:car_provider_app/models/user/forget_password_otp_request.dart';
import 'package:car_provider_app/models/user/verify_email.dart';
import 'package:car_provider_app/models/user/verify_email_otp.dart';
import 'package:http/http.dart';
import 'package:car_provider_app/classes/consts.dart';
import 'package:car_provider_app/models/content_info_model.dart';
import 'package:car_provider_app/models/member_model.dart';
import 'package:car_provider_app/models/surah_model.dart';
import 'package:car_provider_app/models/user/basic_otp.dart';
import 'package:car_provider_app/models/user/register_user.dart';

import '../../classes/app_state.dart';
import '../../models/m_response.dart';
import '../../models/user/refresh_token.dart';
import '../../models/user/user.dart';
import '../api_client.dart';
import '../shared_prefrence.dart';

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

  UserApiClent._internal();

  factory UserApiClent() => _instance;

  Future<BasicOtp> basicOtp(String phoneNo, {int otpType = 1, int roleId = 4, int countryId = 1}) async {
    var postParams = {"countryID": countryId, "userMobileNo": phoneNo, "otpType": otpType, "userRole": roleId};
    return await ApiClient().postJsonForObject((json) => BasicOtp.fromJson(json), ApiConsts.BasicOTP, postParams);
  }

  Future<RegisterUser> basicVerify(String phoneNo, String otp, String userToken) async {
    var postParams = {
      "userMobileNo": phoneNo,
      "userOTP": otp,
      "userToken": userToken,
    };
    return await ApiClient().postJsonForObject((json) => RegisterUser.fromJson(json), ApiConsts.BasicVerify, postParams);
  }

  Future<RegisterUser> basicComplete(String userId, String firstName, String lastName, String email, String password) async {
    var postParams;
    if (email.isEmpty) {
      postParams = {"userID": userId, "firstName": firstName, "lastName": lastName, "companyName": "string", "isEmailVerified": true, "password": password};
    } else {
      postParams = {"userID": userId, "firstName": firstName, "lastName": lastName, "email": email, "companyName": "string", "isEmailVerified": true, "password": password};
    }

    return await ApiClient().postJsonForObject((json) => RegisterUser.fromJson(json), ApiConsts.BasicComplete, postParams);
  }

  Future<Response> login_V1(String phoneNo, String password) async {
    var postParams = {
      "mobileorEmail": phoneNo,
      "password": password,
    };
    return await ApiClient().postJsonForResponse(ApiConsts.Login_V1, postParams);
    //return await ApiClient().postJsonForObject((json) => BasicOtp.fromJson(json), ApiConsts.Login_V1, postParams);
  }

  Future<Response> login_V2_OTP(String userToken, String loginType) async {
    var postParams = {
      "userToken": userToken,
      "loginType": loginType,
    };
    return await ApiClient().postJsonForResponse(ApiConsts.Login_V2_OTP, postParams);
  }

  Future<Response> login_V2_OTPVerify(String userToken, String otp) async {
    var postParams = {"userToken": userToken, "userOTP": otp};
    return await ApiClient().postJsonForResponse(ApiConsts.Login_V2_OTPVerify, postParams);
  }

  Future<Country> getAllCountries() async {
    var postParams = {};
    return await ApiClient().getJsonForObject((json) => Country.fromJson(json), ApiConsts.GetAllCountry);
  }

  Future<Cities> getAllCites(String countryId) async {
    var postParams = {
      "CountryID": countryId,
    };
    return await ApiClient().getJsonForObject((json) => Cities.fromJson(json), ApiConsts.GetAllCities, queryParameters: postParams);
  }

  Future<Role> getRoles() async {
    return await ApiClient().getJsonForObject((json) => Role.fromJson(json), ApiConsts.GetProviderRoles);
  }

  Future<Response> ForgetPasswordOTPRequest(String userName, int otpType) async {
    var postParams = {
      "userName": userName,
      "otpType": 1,
    };
    return await ApiClient().postJsonForResponse(ApiConsts.ForgetPasswordOTPRequest, postParams);
    //return await ApiClient().postJsonForObject((json) => PasswordOTPRequest.fromJson(json), ApiConsts.ForgetPasswordOTPRequest, postParams);
  }

  Future<Response> ForgetPasswordOTPCompare(String userToken, String userOTP) async {
    var postParams = {"userToken": userToken, "userOTP": userOTP};
    return await ApiClient().postJsonForResponse(ApiConsts.ForgetPasswordOTPCompare, postParams);
    // return await ApiClient().postJsonForObject((json) => PasswordOTPCompare.fromJson(json), ApiConsts.ForgetPasswordOTPCompare, postParams);
  }

  Future<Response> ForgetPassword(String userToken, String newPassword) async {
    var postParams = {
      "userToken": userToken,
      "newPassword": newPassword,
    };
    return await ApiClient().postJsonForResponse(ApiConsts.ForgetPassword, postParams);
    //return await ApiClient().postJsonForObject((json) => ConfirmPassword.fromJson(json), ApiConsts.ForgetPassword, postParams);
  }

  Future<MResponse> ChangePassword(String currentPasswor, String newPassword) async {
    var postParams = {
      "currentPassword": currentPasswor,
      "newPassword": newPassword,
    };
    // return await ApiClient().postJsonForResponse(ApiConsts.ChangePassword, postParams);

    String t = AppState().getUser.data!.accessToken ?? "";
    print("tokeen " + t);
    return await ApiClient().postJsonForObject((json) => MResponse.fromJson(json), ApiConsts.ChangePassword, postParams, token: t);
  }

  Future<ChangeMobile> ChangeMobileNoOTPRequest(
    countryID,
    String mobileNo,
    String password,
  ) async {
    var postParams = {"countryID": 1, "mobileNo": mobileNo, "password": password};
    String t = AppState().getUser.data!.accessToken ?? "";
    return await ApiClient().postJsonForObject((json) => ChangeMobile.fromJson(json), ApiConsts.ChangeMobileNoOTPRequest, postParams, token: t);
  }

  Future<ConfirmMobile> ChangeMobileNo(String userToken, String userOTP) async {
    var postParams = {
      "userToken": userToken,
      "userOTP": userOTP,
    };
    String t = AppState().getUser.data!.accessToken ?? "";
    return await ApiClient().postJsonForObject((json) => ConfirmMobile.fromJson(json), ApiConsts.ChangeMobileNo, postParams, token: t);
  }

  Future<ChanEmail> ChangeEmailOTPRequest(String email, String password) async {
    var postParams = {"email": email, "password": password};
    String t = AppState().getUser.data!.accessToken ?? "";
    return await ApiClient().postJsonForObject((json) => ChanEmail.fromJson(json), ApiConsts.ChangeEmailOTPRequest, postParams, token: t);
  }

  Future<ConfirmEmail> ChangeEmail(String userToken, String userOTP) async {
    var postParams = {"userToken": userToken, "userOTP": userOTP};
    String t = AppState().getUser.data!.accessToken ?? "";
    return await ApiClient().postJsonForObject((json) => ConfirmEmail.fromJson(json), ApiConsts.ChangeEmail, postParams, token: t);
  }

  Future<VerifyEmail> EmailVerify(String email, String userID) async {
    var postParams = {
      // "email": email,
      // "userID": userID,
      "email": AppState().getUser.data!.userInfo!.email ?? "",
      "userID": AppState().getUser.data!.userInfo!.userId ?? "",
    };

    String t = AppState().getUser.data!.accessToken ?? "";
    return await ApiClient().postJsonForObject((json) => VerifyEmail.fromJson(json), ApiConsts.EmailVerify, postParams, token: t);
  }

  Future<MResponse> EmailVerifyOTPVerify(String userToken, String userOTP) async {
    var postParams = {"userToken": userToken, "userOTP": userOTP};

    String t = AppState().getUser.data!.accessToken ?? "";
    return await ApiClient().postJsonForObject((json) => MResponse.fromJson(json), ApiConsts.EmailVerifyOTPVerify, postParams, token: t);
  }

  Future<ImageResponse> UpdateUserImage(String image) async {
    var postParams = {"userID": AppState().getUser.data!.userInfo!.userId, "userImage": image};
    // return await ApiClient().postJsonForResponse(ApiConsts.ChangePassword, postParams);

    String t = AppState().getUser.data!.accessToken ?? "";
    print("tokeen " + t);
    return await ApiClient().postJsonForObject((json) => ImageResponse.fromJson(json), ApiConsts.UpdateUserImage, postParams, token: t);
  }

  Future<ImageResponse> GetUserImage(String image) async {
    var postParams = {};
    // return await ApiClient().postJsonForResponse(ApiConsts.ChangePassword, postParams);

    String t = AppState().getUser.data!.accessToken ?? "";
    print("tokeen " + t);
    return await ApiClient().postJsonForObject((json) => ImageResponse.fromJson(json), ApiConsts.UpdateUserImage, postParams, token: t);
  }

  Future<RefreshToken> RefreshTokenAPI(String token, String refreshToken) async {
    var postParams = {"token": token, "refreshToken": refreshToken};
    // String t = AppState().getUser.data!.accessToken ?? "";
    return await ApiClient().postJsonForObject((json) => RefreshToken.fromJson(json), ApiConsts.RefreshToken, postParams);
  }

  Future<String> UpdateUserToken() async {
    String token = await SharedPrefManager.getUserToken();
    String refreshToken = await SharedPrefManager.getRefreshToken();
    RefreshToken refresh = await RefreshTokenAPI(token, refreshToken);
    SharedPrefManager.setUserToken(refresh.data!.accessToken ?? "");
    SharedPrefManager.setRefreshToken(refresh.data!.refreshToken ?? "");
    String mdata = await SharedPrefManager.getData();
    UserInfo info = UserInfo.fromJson(jsonDecode(mdata));
    User user = new User();
    user.data = new UserData(accessToken: refresh.data!.accessToken ?? "", refreshToken: refresh.data!.refreshToken ?? "", userInfo: info);
    AppState().setUser = user;
    return refresh.data!.accessToken??"";
  }
}
