import 'package:flyinsono/managers/server_config_manager.dart';
import 'package:get/get.dart';

///登出界面状态
class SignUpState {
  final Rx<bool> _onSubmit = Rx(false);
  final Rx<bool> _isShowCodeRow = Rx(true);
  final Rx<bool> _isCodeSend = Rx(false);
  final Rx<bool> _isAgreeClause = Rx(false);
  final Rx<String> _account = Rx('');
  final Rx<String> _password = Rx('');
  final Rx<String> _reenterPassword = Rx('');
  final Rx<String> _verificationCode = Rx('');
  final Rx<int> _codeWaitSecond = Rx(0);

  /// 账号(手机/邮箱/用户名)
  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 isShowCodeRow => _isShowCodeRow.value;
  set isShowCodeRow(bool val) => _isShowCodeRow(val);

  /// 验证码是否已发送
  bool get isCodeSend => _isCodeSend.value;
  set isCodeSend(bool val) => _isCodeSend(val);

  /// 验证码等待秒数
  int get codeWaitSecond => _codeWaitSecond.value;
  set codeWaitSecond(int val) => _codeWaitSecond(val);

  /// 是否同意条款和政策
  bool get isAgreeClause => _isAgreeClause.value;
  set isAgreeClause(bool val) => _isAgreeClause(val);

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

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

class SignUpFormValidateState {
  SignUpFormValidateState(this.data);
  final SignUpState data;

  final Rx<bool> _asyncValidating = Rx(false);
  final Rx<bool> _accountBeOccupied = Rx(false);

  /// 正在异步验证
  bool get asyncValidating => _asyncValidating.value;
  set asyncValidating(bool val) => _asyncValidating.value = val;

  /// 账号被占用
  bool get accountBeOccupied => _accountBeOccupied.value;
  set accountBeOccupied(bool val) => _accountBeOccupied.value = val;

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

  /// 账号格式正确
  bool get accountFormatCheck {
    final account = data.account;
    // 判断是否为邮箱
    final RegExp emailRegExp = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
    if (emailRegExp.hasMatch(account)) {
      return true;
    }
    if (ServerConfigManager.isSMSOn) {
      // 判断是否为手机号
      final RegExp phoneRegExp = RegExp(r'^1[0-9]{10}$');
      if (phoneRegExp.hasMatch(account)) {
        return true;
      }
    }
    return false;
  }

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

  /// 密码不为空
  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 {
    if (asyncValidating) return false;
    if (data.isShowCodeRow && data.verificationCode.isEmpty) {
      return false;
    }
    return accountChecked && pwdChecked && reenterPwdChecked;
  }
}

class SignUpFormEditState {
  final Rx<bool> _accountEdited = Rx(false);
  final Rx<bool> _pwdEdited = Rx(false);
  final Rx<bool> _reenterPwdEdited = Rx(false);
  final Rx<bool> _verificationCodeEdited = Rx(false);

  /// 账号已编辑
  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;
  }
}
