import 'dart:async';
import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:http/http.dart';
import 'package:mc_common_app/api/api_client.dart';
import 'package:mc_common_app/classes/app_state.dart';
import 'package:mc_common_app/classes/consts.dart';
import 'package:mc_common_app/config/dependencies.dart';
import 'package:mc_common_app/models/m_response.dart';
import 'package:mc_common_app/models/user/basic_otp.dart';
import 'package:mc_common_app/models/user/change_email.dart';
import 'package:mc_common_app/models/user/change_mobile.dart';
import 'package:mc_common_app/models/user/cities.dart';
import 'package:mc_common_app/models/user/confirm_email.dart';
import 'package:mc_common_app/models/user/confirm_mobile.dart';
import 'package:mc_common_app/models/user/confirm_password.dart';
import 'package:mc_common_app/models/user/country.dart';
import 'package:mc_common_app/models/user/forget_password_otp_compare.dart';
import 'package:mc_common_app/models/user/image_response.dart';
import 'package:mc_common_app/models/user/login_password.dart';
import 'package:mc_common_app/models/user/refresh_token.dart';
import 'package:mc_common_app/models/user/register_user.dart';
import 'package:mc_common_app/models/user/role.dart';
import 'package:mc_common_app/models/user/user.dart';
import 'package:mc_common_app/models/user/verify_email.dart';
import 'package:mc_common_app/utils/shared_prefrence.dart';

abstract class UserRepo {
  Future<BasicOtpRespModel> basicOtp(String phoneNo, {int otpType = 1, int roleId = 4, int countryId = 1});

  Future<RegisterUserRespModel> basicVerify(String phoneNo, String otp, String userToken);

  Future<RegisterUserRespModel> basicComplete(String userId, String firstName, String lastName, String email, String password);

  Future<Response> loginV1(String phoneNo, String password);

  Future<LoginPasswordRespModel> loginV2OTP(String userToken, String loginType);

  Future<Response> loginV2OTPVerify(String userToken, String otp);

  Future<RefreshToken> refreshTokenAPI(String token, String refreshToken);

  Future<Country> getAllCountries();

  Future<Cities> getAllCites(String countryId);

  Future<Role> getRoles();

  Future<Response> forgetPasswordOTPRequest(String userName, int otpType);

  Future<PasswordOTPCompareResModel> forgetPasswordOTPCompare(String userToken, String userOTP);

  Future<ConfirmPasswordRespModel> forgetPassword(String userToken, String newPassword);

  Future<MResponse> changePassword(String currentPassword, String newPassword);

  Future<ChangeMobileRespModel> changeMobileNoOTPRequest(countryID, String mobileNo, String password);

  Future<ConfirmMobileRespModel> changeMobileNo(String userToken, String userOTP);

  Future<ChanEmailRespModel> changeEmailOTPRequest(String email, String password);

  Future<ConfirmEmailRespModel> changeEmail(String userToken, String userOTP);

  Future<VerifyEmailRespModel> emailVerify(String email, String userID);

  Future<MResponse> emailVerifyOTPVerify(String userToken, String userOTP);

  Future<ImageResponse> updateUserImage(String image);

  Future<ImageResponse> getUserImage(String image);

  Future<String> updateUserToken();
}

class UserRepoImp implements UserRepo {
  @override
  Future<BasicOtpRespModel> 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 injector.get<ApiClient>().postJsonForObject((json) => BasicOtpRespModel.fromJson(json), ApiConsts.BasicOTP, postParams);
  }

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

  @override
  Future<RegisterUserRespModel> basicComplete(String userId, String firstName, String lastName, String email, String password) async {
    Map<String, Object> 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 injector.get<ApiClient>().postJsonForObject((json) => RegisterUserRespModel.fromJson(json), ApiConsts.BasicComplete, postParams);
  }

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

  @override
  Future<LoginPasswordRespModel> loginV2OTP(String userToken, String loginType) async {
    var postParams = {
      "userToken": userToken,
      "loginType": loginType,
    };

    Response response = await injector.get<ApiClient>().postJsonForResponse(ApiConsts.Login_V2_OTP, postParams);
    LoginPasswordRespModel user = LoginPasswordRespModel.fromJson(jsonDecode(response.body));
    return user;
  }

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

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

  @override
  Future<Country> getAllCountries() async {
    return await injector.get<ApiClient>().getJsonForObject((json) => Country.fromJson(json), ApiConsts.GetAllCountry);
  }

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

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

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

  @override
  Future<PasswordOTPCompareResModel> forgetPasswordOTPCompare(String userToken, String userOTP) async {
    var postParams = {"userToken": userToken, "userOTP": userOTP};
    Response response = await injector.get<ApiClient>().postJsonForResponse(ApiConsts.ForgetPasswordOTPCompare, postParams);

    PasswordOTPCompareResModel otpCompare = PasswordOTPCompareResModel.fromJson(jsonDecode(response.body));
    return otpCompare;
  }

  @override
  Future<ConfirmPasswordRespModel> forgetPassword(String userToken, String newPassword) async {
    var postParams = {
      "userToken": userToken,
      "newPassword": newPassword,
    };

    Response response = await injector.get<ApiClient>().postJsonForResponse(ApiConsts.ForgetPassword, postParams);
    ConfirmPasswordRespModel data = ConfirmPasswordRespModel.fromJson(jsonDecode(response.body));

    return data;
    //return await injector.get<ApiClient>().postJsonForObject((json) => ConfirmPassword.fromJson(json), ApiConsts.ForgetPassword, postParams);
  }

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

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

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

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

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

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

  @override
  Future<VerifyEmailRespModel> 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 injector.get<ApiClient>().postJsonForObject((json) => VerifyEmailRespModel.fromJson(json), ApiConsts.EmailVerify, postParams, token: t);
  }

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

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

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

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

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

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

  @override
  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 = User();
    user.data = UserData(accessToken: refresh.data!.accessToken ?? "", refreshToken: refresh.data!.refreshToken ?? "", userInfo: info);
    AppState().setUser = user;
    return refresh.data!.accessToken ?? "";
  }
}
