import 'package:fis_common/extensions/string.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:get/get.dart';

///找回密码页面状态
class RetrievePasswordState {
  final Rx<String> _account = Rx('');
  final Rx<String> _password = Rx('');
  final Rx<String> _reenterPassword = Rx('');
  final Rx<String> _verificationCode = Rx('');
  final Rx<bool> _onSubmit = Rx(false);

  /// 账号(手机/邮箱)
  String get account => _account.value;
  set account(String val) => _account(val);

  /// 密码
  String get password => _password.value;
  set password(String val) => _password(val);

  /// 确认密码
  String get reenterPassword => _reenterPassword.value;
  set reenterPassword(String val) => _reenterPassword(val);

  /// 验证码
  String get verificationCode => _verificationCode.value;
  set verificationCode(String val) => _verificationCode(val);

  /// 提交中
  bool get onSubmit => _onSubmit.value;
  set onSubmit(bool val) => _onSubmit(val);

  /// 重置状态
  reset() {
    account = '';
    password = '';
    reenterPassword = '';
    verificationCode = '';
  }
}

///找回密码表单验证状态
class RetrievePasswordFormValidateState {
  RetrievePasswordFormValidateState(this.data);
  final RetrievePasswordState data;

  /// 账号不为空
  bool get accountIsNotEmpty => data.account.isNotEmpty;

  /// 账号格式正确
  bool get accountFormatCheck {
    final account = data.account;
    if (i18nBook.isCurrentChinese) {
      return account.isEmail || account.isChineseMobileNumber;
    } else {
      return account.isEmail;
    }
  }

  /// 账号表单验证通过
  bool get accountChecked => accountIsNotEmpty && accountFormatCheck;

  /// 密码不为空
  bool get pwdIsNotEmpty => data.password.isNotEmpty;

  /// 密码不包含空格
  bool get pwdNotContainBlank => !data.password.contains(' ');

  /// 密码长度正确
  bool get pwdLengthChecked =>
      data.password.length >= 6 && data.password.length <= 20;

  /// 密码字符组合正确
  bool get pwdCharCombinChecked {
    final pwd = data.password;
    int pwdMatchNum = 0;
    // 验证包含符号 - 非字母数字
    if (RegExp(r'[^\da-zA-Z\s]').hasMatch(pwd)) pwdMatchNum++;
    // 验证包含字母
    if (RegExp(r'[a-zA-Z]').hasMatch(pwd)) pwdMatchNum++;
    // 验证包含数字
    if (RegExp(r'\d').hasMatch(pwd)) pwdMatchNum++;
    return pwdMatchNum > 1;
  }

  /// 密码表单验证通过
  bool get pwdChecked =>
      pwdIsNotEmpty &&
      pwdNotContainBlank &&
      pwdLengthChecked &&
      pwdCharCombinChecked;

  /// 确认密码不为空
  bool get reenterPwdIsNotEmpty => data.reenterPassword.isNotEmpty;

  /// 两次密码输入一致
  bool get reenterPwdEqualsPwd => data.password == data.reenterPassword;

  /// 确认密码表单验证通过
  bool get reenterPwdChecked => reenterPwdIsNotEmpty && reenterPwdEqualsPwd;

  /// 验证码验证通过
  bool get verificationCodeChecked => data.verificationCode.isNotEmpty;

  /// 是否可提交
  bool get isCanSubmit {
    return accountChecked &&
        pwdChecked &&
        reenterPwdChecked &&
        verificationCodeChecked;
  }
}

///找回密码表达编辑状态
class RetrievePasswordFormEditState {
  final _accountEdited = false.obs;
  final _pwdEdited = false.obs;
  final _reenterPwdEdited = false.obs;
  final _verificationCodeEdited = false.obs;

  /// 账号已编辑
  bool get accountEdited => _accountEdited.value;
  set accountEdited(bool val) => _accountEdited.value = val;

  /// 密码已编辑
  bool get pwdEdited => _pwdEdited.value;
  set pwdEdited(bool val) => _pwdEdited.value = val;

  /// 确认密码已编辑
  bool get reenterPwdEdited => _reenterPwdEdited.value;
  set reenterPwdEdited(bool val) => _reenterPwdEdited.value = val;

  /// 验证码已编辑
  bool get verificationCodeEdited => _verificationCodeEdited.value;
  set verificationCodeEdited(bool val) => _verificationCodeEdited.value = val;

  /// 清除状态
  void clear() {
    accountEdited = false;
    pwdEdited = false;
    reenterPwdEdited = false;
    verificationCodeEdited = false;
  }
}
