package cc.winboll.studio.contacts.beans;

/**
 * @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
 * @Date 2025/08/30 14:32
 * @Describe 联系人信息数据模型
 */
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class ContactModel {

    public static final String TAG = "ContactModel";

    private String name;
    private String number;
    private String pinyin;
    // 新增：存储姓名的拼音首字母（如"啊牛"→"an"）
    private String pinyinFirstLetter;

    public ContactModel(String name, String number) {
        this.name = name;
        this.number = number.replaceAll("\\s", "");
        this.pinyin = convertToPinyin(name);
        // 初始化时生成拼音首字母
        this.pinyinFirstLetter = convertToPinyinFirstLetter(name);
    }

    // 原方法：转换为全拼（如"啊牛"→"aniu"）
    private String convertToPinyin(String chinese) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        StringBuilder pinyin = new StringBuilder();
        for (int i = 0; i < chinese.length(); i++) {
            char ch = chinese.charAt(i);
            if (Character.toString(ch).matches("[\\u4e00-\\u9fa5]")) { // 仅处理汉字
                try {
                    String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(ch, format);
                    if (pinyinArray != null && pinyinArray.length > 0) {
                        pinyin.append(pinyinArray[0]); // 取第一个拼音（多音字默认首选项）
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyin.append(ch); // 非汉字直接拼接（如字母、数字、符号）
            }
        }
        return pinyin.toString();
    }

    // 新增：转换为拼音首字母（如"啊牛"→"an"）
    private String convertToPinyinFirstLetter(String chinese) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        StringBuilder firstLetters = new StringBuilder();
        for (int i = 0; i < chinese.length(); i++) {
            char ch = chinese.charAt(i);
            if (Character.toString(ch).matches("[\\u4e00-\\u9fa5]")) { // 仅处理汉字
                try {
                    String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(ch, format);
                    if (pinyinArray != null && pinyinArray.length > 0) {
                        // 取拼音的第一个字母（如"a"、"niu"→"a"、"n"）
                        firstLetters.append(pinyinArray[0].charAt(0));
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                // 非汉字可根据需求处理：此处保留原字符（如"李3"→"l3"，"张A"→"za"）
                firstLetters.append(ch);
            }
        }
        return firstLetters.toString();
    }

    // 新增：获取拼音首字母
    public String getPinyinFirstLetter() {
        return pinyinFirstLetter;
    }

    // 原有getter方法
    public String getName() {
        return name;
    }

    public String getNumber() {
        return number;
    }

    public String getPinyin() {
        return pinyin;
    }
}

