package com.cmvr.test.util;

import cn.hutool.core.util.StrUtil;

import java.util.ArrayList;
import java.util.List;

public class TextSplitUtil {

    private static final int DEFAULT_MAX_LENGTH = 20;

    public static List<String> splitText(String text) {
        return splitText(text, DEFAULT_MAX_LENGTH);
    }

    public static List<String> splitText(String text, int maxLength) {
        List<String> result = new ArrayList<>();
        if (StrUtil.isEmpty(text)) {
            return result;
        }

        // 0. 清洗
        text = cleanText(text);

        // 1. 先按句号/问号/感叹号切
        String[] sentences = text.split("(?<=[。！？])");
        for (int i = 0; i < sentences.length; i++) {
            String sentence = sentences[i].trim();
            if (StrUtil.isEmpty(sentence)) {
                continue;
            }

            // 特殊规则：第一句单独处理
            if (i == 0 && sentence.length() > 15) {
                int cut = Math.min(sentence.length(), 15); // 最多15
                if (cut < 10) cut = 10; // 保底至少10
                result.add(sentence.substring(0, cut));

                // 剩余部分继续走正常分割
                String remain = sentence.substring(cut).trim();
                if (StrUtil.isNotEmpty(remain)) {
                    if (remain.length() > maxLength) {
                        result.addAll(splitByComma(remain, maxLength));
                    } else {
                        result.add(remain);
                    }
                }
                continue;
            }

            // 2. 如果太长，按逗号/分号切
            if (sentence.length() > maxLength) {
                result.addAll(splitByComma(sentence, maxLength));
            } else {
                result.add(sentence);
            }
        }

        return result;
    }

    /**
     * 先按逗号/分号切，再兜底按长度切
     */
    private static List<String> splitByComma(String text, int maxLength) {
        List<String> result = new ArrayList<>();
        String[] parts = text.split("(?<=[，；])"); // 保留逗号/分号
        for (String part : parts) {
            part = part.trim();
            if (StrUtil.isEmpty(part)) {
                continue;
            }
            if (part.length() > maxLength) {
                result.addAll(splitByLength(part, maxLength));
            } else {
                result.add(part);
            }
        }
        return result;
    }

    /**
     * 长句按长度强制切分（兜底）
     */
    private static List<String> splitByLength(String text, int maxLength) {
        List<String> result = new ArrayList<>();
        for (int i = 0; i < text.length(); i += maxLength) {
            int end = Math.min(text.length(), i + maxLength);
            result.add(text.substring(i, end).trim());
        }
        return result;
    }

    private static String cleanText(String text) {
        if (StrUtil.isEmpty(text)) {
            return text;
        }
        String cleaned = text;
        cleaned = cleaned.replaceAll("[\\r\\n\\t]+", " ");   // 换行制表
        cleaned = cleaned.replaceAll(" +", " ");             // 多余空格
        cleaned = cleaned.replaceAll("[-=*]{3,}", " ");      // 分隔符
        return cleaned.trim();
    }
}
