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 TestUtil {

    /**
     * 对于长度超过一页的字符串，计算行数，进行数据分割，返回分割后的字符串
     *
     * @param str
     * @return
     */
    public static String getCharLength(String str, Integer total, Integer line) throws Exception {
        int strLength = 0;
        String chinese = "[\u4e00-\u9fa5]";
        String tmpAgo = "";
        String tmpLatter = "";
        int lineTotal = 0; //行数
        StringBuilder sb = new StringBuilder(str);
        String resultString = "";
        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 (temp.charAt(0) >= 'A' && temp.charAt(0) <= 'Z') {
                        if (isChinesePunctuation(tmpAgo.charAt(0))) {
                            strLength += 12;
                        } else if (tmpAgo.matches(chinese)) {
                            strLength += 16;
                        } else {
                            strLength += 8;
                        }
                    } else {
                        if (isChinesePunctuation(tmpAgo.charAt(0))) {
                            strLength += 8;
                        } else if (tmpAgo.matches(chinese)) {
                            strLength += 16;
                        } else {
                            strLength += 8;
                        }
                    }
                }
            }
            if (strLength >= total) {
                //行数 + 1
                strLength = 0;
                lineTotal += 1;
                if (lineTotal >= line) {
                    sb.insert(i, "`");
                    lineTotal = 0;
                }
            }
        }
        resultString = sb.toString();
        return resultString;
    }

    /**
     * @return
     */
    public static int getLineTotal(String str, int total) {
        int strLength = 0;
        String chinese = "[\u4e00-\u9fa5]";
        String tmpAgo = "";
        String tmpLatter = "";
        int lineTotal = 0; //行数
        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 (!tmpLatter.equals("") && 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 (temp.charAt(0) >= 'A' && temp.charAt(0) <= 'Z') {
                        if (isChinesePunctuation(tmpAgo.charAt(0))) {
                            strLength += 12;
                        } else if (tmpAgo.matches(chinese)) {
                            strLength += 16;
                        } else {
                            strLength += 8;
                        }
                    } else {
                        if (isChinesePunctuation(tmpAgo.charAt(0))) {
                            strLength += 8;
                        } else if (tmpAgo.matches(chinese)) {
                            strLength += 16;
                        } else {
                            strLength += 8;
                        }
                    }
                }
            }
            if (strLength >= total) {
                lineTotal += 1;
                strLength = 0;
            }
            if (i + 1 == str.length()) {
                if (strLength != 0) {
                    lineTotal += 1;
                }
            }
        }
        return lineTotal;
    }

    /**
     * 判断是否是中文标点符号
     *
     * @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;
        }
    }

    /**
     * 查询一个字符串中 "`" 出现的次数
     */
    public static int findChar(String str) {
        int count = 0;
        //存放每个字符的数组
        String[] strs = new String[str.length()];
        for (int i = 0; i < strs.length; i++) {
            strs[i] = str.substring(i, i + 1);
        }
        //挨个字符进行查找，查找到之后count++
        for (int i = 0; i < strs.length; i++) {
            if (strs[i].equals("`")) {
                count++;
            }
        }
        return count;
    }

    public static List<List<String>> dataCut(List<String> list, int pageOneLineTotal, int pageTwoLineTotal) throws Exception {
        List<List<String>> stringList = new ArrayList<>();
        int countTotal = 0;
        int flag = 0;    //记录当前页的行数
        for (int i = 0; i < list.size(); i++) {
            String str = list.get(i);
            if (i == 0) {
                countTotal = TestUtil.getLineTotal(str, 544);
                if (countTotal > pageOneLineTotal) {
                    String charLength = TestUtil.getCharLength(str, 544, pageOneLineTotal);    //遍历length，在13行结束的地方插入 "`"
                    int index = charLength.indexOf("`");
                    List<String> temp = new ArrayList<>();
                    temp.add(charLength.substring(0, index));
                    stringList.add(temp);
                    List<String> remain = new ArrayList<>();
                    String substring = charLength.substring(index + 1, charLength.length());
                    flag = TestUtil.getLineTotal(substring, 544);
                    remain.add(substring);
                    stringList.add(remain);
                } else if (countTotal == pageOneLineTotal) {
                    List<String> temp = new ArrayList<>();
                    temp.add(str);
                    stringList.add(temp);
                } else {
                    flag = TestUtil.getLineTotal(str, 544);
                    List<String> temp = new ArrayList<>();
                    temp.add(str);
                    stringList.add(temp);
                }
            } else {   //读完第一条，但是不够塞满第一页
                if (flag > 0) {
                    countTotal = TestUtil.getLineTotal(str, 544);
                    int line = countTotal + flag;
                    //获取当前list中的总行数
                    int nowLine = 0;
                    for (int j = 0; j < stringList.get(0).size(); j++) {
                        String s = stringList.get(0).get(j);
                        nowLine += TestUtil.getLineTotal(s,544);
                    }
                    if (nowLine < pageOneLineTotal) {
                        flag += TestUtil.getLineTotal(str, 544);
                        if (flag > pageOneLineTotal){
                            String charLength = TestUtil.getCharLength(str, 544, pageOneLineTotal - nowLine);
                            String substring = charLength.substring(0, charLength.indexOf("`"));
                            stringList.get(stringList.size() - 1).add(substring);
                            String s = charLength.substring(charLength.indexOf("`") + 1, charLength.length());
                            flag = TestUtil.getLineTotal(s, 544);
                            List<String> tmp = new ArrayList<>();
                            tmp.add(s);
                            stringList.add(tmp);
                        }else if (flag == pageOneLineTotal){
                            stringList.get(stringList.size() - 1).add(str);
                            flag = 0;
                        }else {
                            stringList.get(stringList.size() - 1).add(str);
                        }
                    } else if (line > pageTwoLineTotal) {
                        String charLength = TestUtil.getCharLength(str, 544, pageTwoLineTotal - flag);
                        String substring = charLength.substring(0, charLength.indexOf("`"));
                        stringList.get(stringList.size() - 1).add(substring);
                        String s = charLength.substring(charLength.indexOf("`") + 1, charLength.length());
                        flag = TestUtil.getLineTotal(s, 544);
                        List<String> tmp = new ArrayList<>();
                        tmp.add(s);
                        stringList.add(tmp);
                    } else if (line == pageTwoLineTotal) {
                        stringList.get(stringList.size() - 1).add(str);
                        flag = 0;
                    } else {
                        flag += TestUtil.getLineTotal(str, 544);
                        stringList.get(stringList.size() - 1).add(str);
                    }
                } else {
                    int lineTotal = TestUtil.getLineTotal(str, 544);
                    if (lineTotal > pageTwoLineTotal) {
                        String charLength = TestUtil.getCharLength(str, 544, pageTwoLineTotal);
                        String substring = charLength.substring(0, charLength.indexOf("`"));
                        List<String> temp = new ArrayList<>();
                        temp.add(substring);
                        stringList.add(temp);
                        String s = charLength.substring(charLength.indexOf("`") + 1, charLength.length() - 1);
                        flag = TestUtil.getLineTotal(s, 544);
                        List<String> tmp = new ArrayList<>();
                        tmp.add(s);
                        stringList.add(tmp);
                    } else if (lineTotal == pageTwoLineTotal) {
                        List<String> temp = new ArrayList<>();
                        temp.add(str);
                        stringList.add(temp);
                    } else {
                        flag = lineTotal;
                        List<String> temp = new ArrayList<>();
                        temp.add(str);
                        stringList.add(temp);
                    }
                }
            }
        }
        return stringList;
    }
}
