/// 脱敏函数集合
abstract class SensitiveUtils {
  // /// 身份证号
  // ///
  // /// 显示前六位和后四位
  // ///
  // /// - 如：123456********0011
  // static String desensitizeIdCard(String zjhm) {
  //   if (zjhm.isEmpty) {
  //     return "";
  //   }
  //   int encLength = zjhm.length - 10;
  //   String regex = "(\\d{6})\\d{$encLength}(\\w{4})";
  //   return zjhm.replaceAll(RegExp(regex), "\$1********\$2");
  // }

  /// 身份证号
  ///
  /// 前六位，后四位，其他用星号隐藏每位1个星号
  ///
  /// - 如：123456*********0011
  static String desensitizeIdCard(String zjhm) {
    if (zjhm.isEmpty) {
      return "";
    }
    StringBuffer sb = StringBuffer();
    sb.write(zjhm.substring(0, 6));
    for (int i = 0; i < zjhm.length - 10; i++) {
      sb.write("*");
    }
    sb.write(zjhm.substring(zjhm.length - 4));
    return sb.toString();
  }

  /// 身份证号
  ///
  ///  显示最后四位，其他隐藏
  ///
  /// 共计18位或者15位
  ///
  /// - 如：*************5762
  static String desensitizeIdCardNum(String zjhm) {
    if (zjhm.isEmpty) {
      return "";
    }
    StringBuffer sb = StringBuffer();
    for (int i = 0; i < zjhm.length - 4; i++) {
      sb.write("*");
    }
    sb.write(zjhm.substring(zjhm.length - 4));
    return sb.toString();
  }

  /// 中文名
  ///
  /// 如果长度=2，后一位隐藏为星号
  ///
  /// - 如： 张*
  ///
  /// 如果长度>2，中间隐藏为星号
  ///
  /// - 如：张*锤
  static String desensitizeChineseName(String fullName) {
    if (fullName.isEmpty) {
      return "";
    }
    StringBuffer sb = StringBuffer();
    sb.write(fullName.substring(0, 1));
    for (int i = 0; i < fullName.length - 2; i++) {
      sb.write("*");
    }
    if (fullName.length > 2) {
      sb.write(fullName.substring(fullName.length - 1));
    } else {
      sb.write("*");
    }
    return sb.toString();
  }

  /// 固定电话
  ///
  /// 显示后四位，其他隐藏
  ///
  /// - 如 : ****1234
  static String desensitizeFixedPhone(String num) {
    if (num.isEmpty) {
      return "";
    }
    StringBuffer sb = StringBuffer();
    for (int i = 0; i < num.length - 4; i++) {
      sb.write("*");
    }
    sb.write(num.substring(num.length - 4));
    return sb.toString();
  }

  /// 手机号
  ///
  /// 前三位，后两位，其他隐藏
  ///
  /// - 如：138********34
  static String desensitizeMobilePhone(String num) {
    if (num.isEmpty) {
      return "";
    }
    StringBuffer sb = StringBuffer();
    sb.write(num.substring(0, 3));
    for (int i = 0; i < num.length - 5; i++) {
      sb.write("*");
    }
    sb.write(num.substring(num.length - 2));
    return sb.toString();
  }

  /// 地址
  ///
  /// 只显示到地区，不显示详细地址
  static String desensitizeAddress(String address) {
    if (address.isEmpty) {
      return "";
    }

    const areaKeys = ["县", "区", "市", "省", "自治州"];

    int publicPartTail = 0;
    for (int i = 0; i < areaKeys.length; i++) {
      final key = areaKeys[i];

      final index = address.indexOf(key);
      if (index > -1) {
        publicPartTail = index;

        if (key == "市") {
          // 县级市
          final subCityIndex = address.substring(index + 1).indexOf("市");
          if (subCityIndex > -1 && subCityIndex < 6) {
            publicPartTail = (index + 1) + subCityIndex;
          }
        }

        break;
      }
    }

    if (publicPartTail < 1) {
      publicPartTail = 8; // 找不到区域范围，只显示前8个字符
    }

    final publicPartString = address.substring(0, publicPartTail + 1);
    final desensitizedString =
        List<String>.filled(address.length - publicPartTail, "*").join();
    return "$publicPartString$desensitizedString";
  }

  // /// 地址
  // ///
  // /// 只显示到地区，不显示详细地址(我们要对个人信息增强保护)
  // ///
  // /// - 如：北京市海淀区****
  // ///
  // /// [sensitiveSize] 敏感信息长度
  // static String desensitizeAddress(String address, int sensitiveSize) {
  //   if (address.isEmpty) {
  //     return "";
  //   }
  //   StringBuffer sb = StringBuffer();
  //   for (int i = 0; i < address.length - sensitiveSize; i++) {
  //     sb.write("*");
  //   }
  //   sb.write(address.substring(address.length - sensitiveSize));
  //   return sb.toString();
  // }

  /// 电子邮箱
  ///
  /// 前缀仅显示第一个字母，前缀其他隐藏，用星号代替，@及后面的地址显示
  ///
  /// - 如：g**@163.com
  static String desensitizeEmail(String email) {
    if (email.isEmpty) {
      return "";
    }
    int index = email.indexOf("@");
    String end = email.substring(index);
    StringBuffer sb = StringBuffer();
    sb.write(email.substring(0, 1));
    for (int i = 0; i < email.length - end.length - 1; i++) {
      sb.write("*");
    }
    sb.write(end);
    return sb.toString();
  }

  /// 银行卡
  ///
  /// 前六位，后四位，其他用星号隐藏每位1个星号
  ///
  /// - 如：6222600**********1234
  static String desensitizeBankCard(String cardNum) {
    if (cardNum.isEmpty) {
      return "";
    }
    StringBuffer sb = StringBuffer();
    sb.write(cardNum.substring(0, 6));
    for (int i = 0; i < cardNum.length - 10; i++) {
      sb.write("*");
    }
    sb.write(cardNum.substring(cardNum.length - 4));
    return sb.toString();
  }

  /// 公司开户银行联行号
  ///
  /// 显示前两位，其他用星号隐藏，每位1个星号
  ///
  /// - 如：12********
  static String desensitizeCnapsCode(String code) {
    if (code.isEmpty) {
      return "";
    }
    StringBuffer sb = StringBuffer();
    sb.write(code.substring(0, 2));
    for (int i = 0; i < code.length - 2; i++) {
      sb.write("*");
    }
    return sb.toString();
  }
}
