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 'mht_register_controller.g.dart';

@JsonSerializable()
class RegisterModel {
  String? register_phone; //注册手机号
  String? register_pd; //注册密码
  String? register_confirm_pd; //注册确认密码
  String? register_code; //注册验证码

  bool? register_agree = false; //注册协议
  bool? save_password = false; //记住密码

  bool? pdshow = true; //是否显示密码
  bool? cpdshow = true; //是否显示密码

  RegisterModel();
  static RegisterModel fromJson(Map<String, dynamic> json) =>
      _$RegisterModelFromJson(json);
  Map<String, dynamic> toJson() => _$RegisterModelToJson(this);
}

class MHTRegisterController extends GetControllerEx<RegisterModel> {
  MHTRegisterController() {
    attr = GetModel(RegisterModel()).obs;
  }

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

    if (model.register_pd!.length < 8) {
      message = passwordMsg;
      TopSlideNotification.show(context,
          text: message, textColor: stringToColor("#FF7159"));
      return message;
    }

    if (!hasLetter || !hasDigit) {
      message = passwordMsg;
      TopSlideNotification.show(context,
          text: message, textColor: stringToColor("#FF7159"));
      return message;
    }

    if (!(hasSpecialCharacters || hasUppercase)) {
      message = passwordMsg;
      TopSlideNotification.show(context,
          text: message, textColor: stringToColor("#FF7159"));
      return message;
    }
    if (model.register_confirm_pd == null ||
        model.register_confirm_pd!.isEmpty) {
      message = "请输入确认密码".tr;
      TopSlideNotification.show(context,
          text: message, textColor: stringToColor("#FF7159"));
      return message;
    }
    if (model.register_pd != model.register_confirm_pd) {
      message = "两次密码不一致".tr;
      TopSlideNotification.show(context,
          text: message, textColor: stringToColor("#FF7159"));
      return message;
    }
    if (model.register_code == null || model.register_code!.isEmpty) {
      message = "请输入验证码".tr;
      TopSlideNotification.show(context,
          text: message, textColor: stringToColor("#FF7159"));
      return message;
    }
    if (model.register_agree == null || model.register_agree != true) {
      message = "需要同意协议".tr;
      TopSlideNotification.show(context,
          text: message, textColor: stringToColor("#FF7159"));
      return message;
    }
    String serviceAddress = ServiceConstant.service_address;
    String serviceName = ServiceConstant.server_service;
    String serviceApi = ServiceConstant.user_register;
    String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
    var data = {
      "type": 1,
      "userName": model.register_phone!,
      "password": model.register_pd!,
      "verify": model.register_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.register_phone == null || model.register_phone!.isEmpty) {
      message = "请输入手机号".tr;
      TopSlideNotification.show(context,
          text: message, textColor: stringToColor("#FF7159"));
      return message;
    }
    if (!MyUtils.isValidPhoneNumber(model.register_phone!)) {
      message = '请输入正确的手机号'.tr;
      TopSlideNotification.show(context,
          text: message, textColor: stringToColor("#FF7159"));
      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.register_phone!,
      'type':4,
    };
    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.tr,
          textColor: themeController.currentColor.sc9,
        );
      },
    );
    return message;
  }
}
