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';
import 'package:vbvs_app/controller/user_info_controller.dart';

part 'update_password_controller.g.dart';

@JsonSerializable()
class UpdatePasswordModel {
  String? phone; //手机号
  String? code; //验证码
  String? pd; //密码
  String? confirm; //验证密码
  bool? pdshow = true; //是否显示密码
  bool? cpdshow = true; //是否显示密码

  UpdatePasswordModel();
  static UpdatePasswordModel fromJson(Map<String, dynamic> json) =>
      _$UpdatePasswordModelFromJson(json);
  Map<String, dynamic> toJson() => _$UpdatePasswordModelToJson(this);
}

class UpdatePasswordController extends GetControllerEx<UpdatePasswordModel> {
  UpdatePasswordController() {
    attr = GetModel(UpdatePasswordModel()).obs;
  }

  UserInfoController userInfoController = Get.find();

  //确认验证码
  Future<String> resetPassword(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;
    }
    String serviceAddress = ServiceConstant.service_address;
    String serviceName = ServiceConstant.server_service;
    String serviceApi = ServiceConstant.user_changePassword;
    String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
    final user = userInfoController.model.user!;
    var data = {
      "userName": user.email ?? user.phone,
      "password": model.pd,
      "verify": model.code
    };
    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> 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 = '请输入正确的手机号'.tr;
      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": 3};
    await requestWithLog(
      data: data,
      logTitle: "发送验证码",
      method: MyHttpMethod.post,
      queryUrl: queryUrl,
      onSuccess: (res) {
        TopSlideNotification.show(context, text: "发送验证码成功".tr);
      },
      onFailure: (res) {
        message = res.msg!;
        TopSlideNotification.show(context,
            text: message, textColor: themeController.currentColor.sc9);
      },
    );
    return message;
  }
}
