package com.ruoyi.web.modules.industryservice.research.support;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.industrypolicy.support.SubsidyGrantRecordMasking;

/**
 * 科研开放数据集脱敏规则。
 */
public final class ResearchOpenApiMasking
{
    private ResearchOpenApiMasking()
    {
    }

    /** 科研主体 pseudonym：YS + 8 位本地 ID */
    public static String subjectCode(Long id)
    {
        if (id == null)
        {
            return null;
        }
        return String.format("YS%08d", id);
    }

    /** 由原始编号生成不可逆短码（用于系谱关联，非明文编号） */
    public static String pseudonymCode(String raw, String prefix)
    {
        if (StringUtils.isEmpty(raw))
        {
            return null;
        }
        int hash = Math.abs(raw.hashCode());
        return prefix + String.format("%08d", hash % 100_000_000);
    }

    public static String maskPersonName(String name)
    {
        if (StringUtils.isEmpty(name))
        {
            return name;
        }
        String trimmed = name.trim();
        if (trimmed.length() == 1)
        {
            return trimmed + "*";
        }
        return trimmed.charAt(0) + repeatChar('*', Math.min(trimmed.length() - 1, 2));
    }

    public static String maskPhone(String phone)
    {
        return SubsidyGrantRecordMasking.maskPhone(phone);
    }

    /** 区域摘要：区县 + 乡镇，不含详细门牌 */
    public static String regionSummary(String county, String town)
    {
        StringBuilder sb = new StringBuilder();
        if (StringUtils.isNotEmpty(county))
        {
            sb.append(county.trim());
        }
        if (StringUtils.isNotEmpty(town))
        {
            if (sb.length() > 0)
            {
                sb.append(' ');
            }
            sb.append(town.trim());
        }
        return sb.length() > 0 ? sb.toString() : null;
    }

    public static Double roundCoordinate(Double value)
    {
        if (value == null)
        {
            return null;
        }
        return BigDecimal.valueOf(value).setScale(3, RoundingMode.HALF_UP).doubleValue();
    }

    public static Integer birthYear(Date birthDate)
    {
        if (birthDate == null)
        {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(birthDate);
        return cal.get(Calendar.YEAR);
    }

    public static Integer birthMonth(Date birthDate)
    {
        if (birthDate == null)
        {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(birthDate);
        return cal.get(Calendar.MONTH) + 1;
    }

    public static String assetStatusLabel(Integer assetStatus)
    {
        if (assetStatus == null)
        {
            return null;
        }
        switch (assetStatus)
        {
            case 1:
                return "正常";
            case 2:
                return "死淘";
            case 3:
                return "丢失";
            case 4:
                return "出栏";
            default:
                return "未知";
        }
    }

    private static String repeatChar(char c, int count)
    {
        if (count <= 0)
        {
            return "";
        }
        StringBuilder sb = new StringBuilder(count);
        for (int i = 0; i < count; i++)
        {
            sb.append(c);
        }
        return sb.toString();
    }
}
