import 'package:ef/ef.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:vbvs_app/common/color/ServiceConstant.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/common/util/requestWithLog.dart';
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';

part 'find_password_controller.g.dart';

@JsonSerializable()
class FindPasswordModel {
  String? phone; //手机号
  String? code; //验证码

  bool? register_agree; //注册协议

  String? pd; //密码
  String? confirm; //验证密码
  bool? pdshow = true; //是否显示密码
  bool? cpdshow = true; //是否显示密码

  FindPasswordModel();
  static FindPasswordModel fromJson(Map<String, dynamic> json) =>
      _$FindPasswordModelFromJson(json);
  Map<String, dynamic> toJson() => _$FindPasswordModelToJson(this);
}

class FindPasswordController extends GetControllerEx<FindPasswordModel> {
  // final UserRepository repository = UserRepository();
  FindPasswordController() {
    attr = GetModel(FindPasswordModel()).obs;
  }

  Future<String> getCode(BuildContext context) async {
    String message = "";
    if (model.phone == null || model.phone!.isEmpty) {
      message = "请输入手机号".tr;
      TopSlideNotification.show(context,
          text: message, textColor: themeController.currentColor.sc9);
      return message;
    }
    if (!MyUtils.isValidPhoneNumber(model.phone!)) {
      message = '请输入正确的手机号';
      TopSlideNotification.show(context,
          text: message, textColor: themeController.currentColor.sc9);
      return message;
    }
    String serviceAddress = ServiceConstant.service_address;
    String serviceName = ServiceConstant.server_service;
    String serviceApi = ServiceConstant.send_code;
    String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
    var data = {"userName": model.phone, "type": 2};
    await requestWithLog(
      logTitle: "忘记密码请求验证码",
      method: MyHttpMethod.post,
      queryUrl: queryUrl,
      data: data,
      onSuccess: (res) {
        TopSlideNotification.show(
          context,
          text: '发送验证码成功'.tr,
        );
      },
      onFailure: (res) {
        message = res.msg!;
        TopSlideNotification.show(context,
            text: message, textColor: themeController.currentColor.sc9);
      },
    );
    return message;
  }

  //确认验证码
  Future<String> confirmCode(BuildContext context) async {
    String message = "";
    if (model.phone == null || model.phone!.isEmpty) {
      message = "请输入手机号".tr;
      TopSlideNotification.show(context,
          text: message, textColor: themeController.currentColor.sc9);
      return message;
    }
    if (!MyUtils.isValidPhoneNumber(model.phone!)) {
      message = '请输入正确的手机号'.tr;
      TopSlideNotification.show(context,
          text: message, textColor: themeController.currentColor.sc9);
      return message;
    }
    if (model.code == null || model.code!.isEmpty) {
      message = "请输入验证码".tr;
      TopSlideNotification.show(context,
          text: message, textColor: themeController.currentColor.sc9);
      return message;
    }
    if (model.pd == null || model.pd!.isEmpty) {
      message = "请输入密码".tr;
      TopSlideNotification.show(context,
          text: message, textColor: themeController.currentColor.sc9);
      return message;
    }
    String passwordMsg = "密码格式提示".tr;
    bool hasUppercase = model.pd!.contains(RegExp(r'[A-Z]'));
    bool hasDigit = model.pd!.contains(RegExp(r'[0-9]'));
    bool hasSpecialCharacters =
        model.pd!.contains(RegExp(r'[!@#$%^&*(),.?":{}|<>]'));
    bool hasLetter = model.pd!.contains(RegExp(r'[a-zA-Z]'));

    if (model.pd!.length < 8) {
      message = passwordMsg;
      TopSlideNotification.show(context,
          text: message, textColor: themeController.currentColor.sc9);
      return message;
    }

    if (!hasLetter || !hasDigit) {
      message = passwordMsg;
      TopSlideNotification.show(context,
          text: message, textColor: themeController.currentColor.sc9);
      return message;
    }

    if (!(hasSpecialCharacters || hasUppercase)) {
      message = passwordMsg;
      TopSlideNotification.show(context,
          text: message, textColor: themeController.currentColor.sc9);
      return message;
    }

    if (model.confirm == null || model.confirm!.isEmpty) {
      message = "请输入确认密码".tr;
      TopSlideNotification.show(context,
          text: message, textColor: themeController.currentColor.sc9);
      return message;
    }
    if (model.pd != model.confirm) {
      message = "两次密码不一致".tr;
      TopSlideNotification.show(context,
          text: message, textColor: themeController.currentColor.sc9);
      return message;
    }
    var data = {
      "userName": model.phone,
      "password": model.pd,
      "verify": model.code
    };
    String serviceAddress = ServiceConstant.service_address;
    String serviceName = ServiceConstant.server_service;
    String serviceApi = ServiceConstant.user_forgot;
    String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
    await requestWithLog(
      logTitle: "找回密码",
      method: MyHttpMethod.post,
      queryUrl: queryUrl,
      data: data,
      onSuccess: (res) {
        TopSlideNotification.show(context, text: "密码修改成功".tr);
      },
      onFailure: (res) {
        message = res.msg!;
        TopSlideNotification.show(context,
            text: message, textColor: themeController.currentColor.sc9);
      },
    );
    return message;
  }
}
