import 'package:domain/entity/auth_login_entity.dart';
import 'package:domain/entity/captcha_img_entity.dart';
import 'package:domain/entity/server_time.dart';
import 'package:plugin_platform/platform_export.dart';
import 'package:plugin_platform/http/dio_engine.dart';
import 'package:plugin_platform/http/http_result.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:shared/utils/util.dart';

import '../constants/api_constants.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:plugin_basic/provider/http_provider/http_provider.dart';

part 'auth_repository.g.dart';

@Riverpod(keepAlive: true)
AuthRepository authRepository(Ref ref) {
  final dioEngine = ref.watch(dioEngineProvider);
  return AuthRepository(dioEngine: dioEngine);
}

/*
 * 用户模块的数据仓库
 */
class AuthRepository {
  DioEngine dioEngine;

  AuthRepository({required this.dioEngine});

  /// 用户登录
  Future<HttpResult<AuthLoginEntity>> authLogin(
    String? userName,
    String? password, {
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    if (userName != null && userName.startsWith('+')) {
      userName = userName.substring(1); // 去掉开头的 "+"
    }
    params['username'] = userName!;
    params['password'] = password!;

    final result = await dioEngine.requestNetResult(
      ApiConstants.apiUserLogin,
      params: params,
      method: HttpMethod.POST,
      isShowLoadingDialog: true,
      networkDebounce: true,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      final json = result.getDataJson();
      var data = AuthLoginEntity.fromJson(json!);
      return result.convert<AuthLoginEntity>(data: data);
    }
    return result.convert();
  }

  /// 图片验证码
  Future<HttpResult<CaptchaImgEntity>> fetchCaptchaImage({
    CancelToken? cancelToken,
  }) async {
    final result = await dioEngine.requestNetResult(
      ApiConstants.apiCaptchaImage,
      method: HttpMethod.GET,
      isShowLoadingDialog: true,
      networkDebounce: true,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      final json = result.getDataJson();
      var data = CaptchaImgEntity.fromJson(json!);
      return result.convert<CaptchaImgEntity>(data: data);
    }
    return result.convert();
  }

  /// 发送短信
  Future<HttpResult> sendSMS({
    required String? countryCode,
    required String? phone,
    required String? captchaKey,
    required String? captchaValue,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    if (countryCode != null && countryCode.startsWith('+')) {
      countryCode = countryCode.substring(1); // 去掉开头的 "+"
    }
    params['country_code'] = countryCode!;
    params['phone'] = phone!;
    params['captcha_key'] = captchaKey!;
    params['captcha_value'] = captchaValue!;

    final result = await dioEngine.requestNetResult(
      ApiConstants.apiSendSMS,
      params: params,
      method: HttpMethod.POST,
      isShowLoadingDialog: true,
      networkDebounce: true,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      return result.convert();
    }
    return result.convert();
  }

  /// 用户注册
  Future<HttpResult<AuthLoginEntity>> userRegister({
    required String firstName,
    required String lastName,
    required String email,
    required String countryCode,
    required String phone,
    required String password,
    required String confirmPassword,
    required String? smsCode,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    if (countryCode.startsWith('+')) {
      countryCode = countryCode.substring(1); // 去掉开头的 "+"
    }
    params['first_name'] = firstName;
    params['last_name'] = lastName;
    params['email'] = email;
    params['country_code'] = countryCode;
    params['phone'] = phone;
    params['password'] = password;
    params['password_confirmation'] = confirmPassword;
    params['sms_code'] = smsCode ?? "";

    final result = await dioEngine.requestNetResult(
      ApiConstants.apiAuthRegister,
      params: params,
      method: HttpMethod.POST,
      isShowLoadingDialog: true,
      networkDebounce: true,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      final json = result.getDataJson();
      var data = AuthLoginEntity.fromJson(json!);
      return result.convert<AuthLoginEntity>(data: data);
    }
    return result.convert();
  }

  /// 修改密码
  Future<HttpResult> resetPassword({
    required String? smsCode,
    required String? password,
    required String? confirmPassword,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};

    params['sms_code'] = smsCode!;
    params['password'] = password!;
    params['password_confirmation'] = confirmPassword!;

    final result = await dioEngine.requestNetResult(
      ApiConstants.apiResetPassword,
      params: params,
      method: HttpMethod.POST,
      isShowLoadingDialog: true,
      networkDebounce: true,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      return result.convert();
    }
    return result.convert();
  }

  /// 忘记密码
  Future<HttpResult> forgotPassword({
    required String? smsCode,
    required String? countryCode,
    required String? phone,
    required String? password,
    required String? confirmPassword,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    if (countryCode != null && countryCode.startsWith('+')) {
      countryCode = countryCode.substring(1); // 去掉开头的 "+"
    }
    params['sms_code'] = smsCode!;
    params['country_code'] = countryCode!;
    params['phone'] = phone!;
    params['password'] = password!;
    params['password_confirmation'] = confirmPassword!;

    final result = await dioEngine.requestNetResult(
      ApiConstants.apiForgotPassword,
      params: params,
      method: HttpMethod.POST,
      isShowLoadingDialog: true,
      networkDebounce: true,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      return result.convert();
    }
    return result.convert();
  }

  /// 注册 FCM Token
  Future<HttpResult> registerFCMToken({
    required String? fcmToken,
    CancelToken? cancelToken,
  }) async {
    Map<String, String> params = {};
    params['fcm_token'] = fcmToken!;

    final result = await dioEngine.requestNetResult(
      ApiConstants.apiFCMToken,
      params: params,
      method: HttpMethod.POST,
      networkDebounce: true,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      return result.convert();
    }
    return result.convert();
  }

  /// 用户登出
  Future<HttpResult> authLogout({
    CancelToken? cancelToken,
  }) async {

    final result = await dioEngine.requestNetResult(
      ApiConstants.apiUserLogout,
      method: HttpMethod.POST,
      isShowLoadingDialog: true,
      networkDebounce: true,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      return result.convert();
    }
    return result.convert();
  }

  /// 删除账号
  Future<HttpResult> accountDeactivate({
    CancelToken? cancelToken,
  }) async {

    final result = await dioEngine.requestNetResult(
      ApiConstants.apiAuthDeactivate,
      method: HttpMethod.POST,
      isShowLoadingDialog: true,
      networkDebounce: true,
      cancelToken: cancelToken,
    );

    if (result.isSuccess) {
      return result.convert();
    }
    return result.convert();
  }
}
