package com.haha.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SensitiveDataUtils {

    private static final Pattern PHONE_PATTERN = Pattern.compile("(1[3-9]\\d)\\d{4}(\\d{4})");
    private static final Pattern ID_CARD_PATTERN = Pattern.compile("(\\d{3})\\d{11}(\\d{4})");
    private static final Pattern BANK_CARD_PATTERN = Pattern.compile("(\\d{4})\\d+(\\d{4})");
    private static final Pattern PASSWORD_PATTERN = Pattern.compile("(password|pwd|passwd)['\"]?\\s*[:=]\\s*['\"]?([^'\"\\s,}]+)['\"]?", Pattern.CASE_INSENSITIVE);

    private SensitiveDataUtils() {
    }

    public static String maskPhone(String phone) {
        if (phone == null || phone.isEmpty()) {
            return phone;
        }
        Matcher matcher = PHONE_PATTERN.matcher(phone);
        if (matcher.matches()) {
            return matcher.group(1) + "****" + matcher.group(2);
        }
        return phone;
    }

    public static String maskIdCard(String idCard) {
        if (idCard == null || idCard.isEmpty()) {
            return idCard;
        }
        Matcher matcher = ID_CARD_PATTERN.matcher(idCard);
        if (matcher.matches()) {
            return matcher.group(1) + "***********" + matcher.group(2);
        }
        return idCard;
    }

    public static String maskBankCard(String bankCard) {
        if (bankCard == null || bankCard.isEmpty()) {
            return bankCard;
        }
        String digits = bankCard.replaceAll("\\D", "");
        if (digits.length() >= 8) {
            int length = digits.length();
            int start = 4;
            int end = length - 4;
            return digits.substring(0, start) + "****" + digits.substring(end);
        }
        return bankCard;
    }

    public static String maskPassword(String text) {
        if (text == null || text.isEmpty()) {
            return text;
        }
        Matcher matcher = PASSWORD_PATTERN.matcher(text);
        return matcher.replaceAll("$1=******");
    }

    public static String mask(String text) {
        if (text == null || text.isEmpty()) {
            return text;
        }
        String result = text;
        result = maskPhoneInText(result);
        result = maskIdCardInText(result);
        result = maskBankCardInText(result);
        result = maskPassword(result);
        return result;
    }

    private static String maskPhoneInText(String text) {
        if (text == null || text.isEmpty()) {
            return text;
        }
        return PHONE_PATTERN.matcher(text).replaceAll("$1****$2");
    }

    private static String maskIdCardInText(String text) {
        if (text == null || text.isEmpty()) {
            return text;
        }
        return ID_CARD_PATTERN.matcher(text).replaceAll("$1***********$2");
    }

    private static String maskBankCardInText(String text) {
        if (text == null || text.isEmpty()) {
            return text;
        }
        Matcher matcher = BANK_CARD_PATTERN.matcher(text);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            String group = matcher.group(0);
            String masked = maskBankCard(group);
            matcher.appendReplacement(sb, masked);
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
