package com.nantian.cloud.jljc.tools;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

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

@Component
public class CutStringUtil {

    /**
     * 获取字符串的长度，如果有中文，则每个中文字符计为2位
     * @param str
     * @param total 每行所占的字符数
     * @return
     */
    public static List<String> getCharLength(String str, int total) throws Exception {
        int strLength = 0;
        String chinese = "[\u0391-\uFFE5]";
        List<String> resultList = new ArrayList<>();
        Integer count = 0;
        String tmpAgo = "";
        String tmpLatter = "";
        int size = str.length();
        for (int i = 0; i < str.length(); i++) {
            //截取一个字符
            String temp = str.substring(i, i + 1);
            //截取前一个字符
            if (i > 0){
                tmpAgo = str.substring(i - 1, i);
            }
            //截取后一个字符
            if (i + 2 < str.length()){
                tmpLatter = str.substring(i+1,i+2);
            }
            //判断temp是否是中文标点符号
            if (isChinesePunctuation(temp.charAt(0))){
                //word中标点符号不会出现在首位
                if (isChinesePunctuation(tmpLatter.charAt(0))){
                    strLength += 8;
                }else {
                    strLength += 16;
                }
                //判断temp是否是中文字符
            }else if (temp.matches(chinese)){
                strLength += 16;
                //非中文字符
            }else{
                if (i > 0 && i + 2 < str.length()){
                    if (temp.equals(" ")){
                        strLength += 8;
                    }else if (isChinesePunctuation(tmpLatter.charAt(0)) || isChinesePunctuation(tmpAgo.charAt(0))){
                        strLength += 8;
                    }else if(tmpAgo.matches(chinese) || tmpLatter.matches(chinese)){
                        strLength += 12;
                    } else {
                        strLength += 8;
                    }
                }else if (i == 0){
                    if (isChinesePunctuation(tmpLatter.charAt(0))){
                        strLength += 12;
                    }else if(tmpLatter.matches(chinese)){
                        strLength += 16;
                    } else {
                        strLength += 8;
                    }
                }else {
                    if (temp.equals(" ")){
                        strLength += 8;
                    }else if (isChinesePunctuation(tmpAgo.charAt(0))){
                        strLength += 12;
                    }else if(tmpAgo.matches(chinese)){
                        strLength += 16;
                    } else {
                        strLength += 8;
                    }
                }
            }
            if (i + 1 == str.length()){
                resultList.add(str.substring(count, size));
            }else{
                if (strLength == total){
                    strLength = 0;
                    if (isChinesePunctuation(tmpLatter.charAt(0))){
                        resultList.add(str.substring(count,i + 2));
                        count = i + 2;
                    }else {
                        String substring = str.substring(count, i + 1);
                        boolean containChiesePunctuation = isContainChiesePunctuation(substring);
                        if (containChiesePunctuation){
                            resultList.add(substring);
                            count = i + 1;
                        }else {
                            resultList.add(str.substring(count,i));
                            count = i;
                            i = i - 1;
                        }
                    }
                }else if (strLength > total){
                    strLength = 0;
                    if (isChinesePunctuation(tmpLatter.charAt(0))){
                        resultList.add(str.substring(count,i + 2));
                        count = i + 2;
                    }else if (temp.matches(chinese)){
                        resultList.add(str.substring(count,i));
                        count = i;
                        i = i - 1;
                    }else {
                        resultList.add(str.substring(count, i + 1));
                        count = i;
                    }
                }
            }
        }
        return resultList;
    }

    /**
     * 判断是否是中文标点符号
     * @param c
     * @return
     */
    private static boolean isChinesePunctuation(char c){
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS
                || ub == Character.UnicodeBlock.VERTICAL_FORMS){
            return true;
        }else {
            return false;
        }
    }

    private static boolean isContainChiesePunctuation(String str) throws Exception {
        if (StringUtils.isEmpty(str)){
            throw new Exception("sms context is empty!");
        }
        Pattern p = Pattern.compile("[\\！|\\，|\\。|\\（|\\）|\\《|\\》|\\“|\\”|\\？|\\：|\\；|\\【|\\】|\\〔|\\〕]");
        Matcher matcher = p.matcher(str);
        if (matcher.find()){
            return true;
        }else {
            return false;
        }
    }
}
