package com.izouma.nineth.utils;

import com.anji.captcha.util.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.util.*;

public class SensitiveFilterUtil {


    /**
     * 敏感词集合
     */
    private static HashMap sensitiveWordMap;

    /**
     * 初始化敏感词库,构建DFA算法模型
     */
    private static synchronized void initContext() {
        if (sensitiveWordMap != null) {
            return;
        }
        HashSet<String> set = new HashSet<String>();
        try {
            //获取敏感词文件
            //获取容器资源解析器
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            Resource[] resources = resolver.getResources("/xls/cfg_sensitive_word.xlsx");
            Workbook workbook = new XSSFWorkbook(resources[0].getInputStream());
            // 获取第一个张表
            Sheet sheet = workbook.getSheetAt(0);
            // 获取每行中的字段
            for (int j = 6; j <= sheet.getLastRowNum(); j++) {
                Row row = sheet.getRow(j); // 获取行
                if (row != null) {// 略过空行
                    Cell cell = row.getCell(1);
                    if (row.getCell(1) != null) {
                        cell.setCellType(CellType.STRING);
                        set.add(cell.getStringCellValue());
                    }
                }
            }
            initSensitiveWordMap(set);
        } catch (Exception e) {
            System.out.println("<<<<<<解析敏感词文件报错！");
            e.printStackTrace();
        }
    }

    /**
     * 初始化敏感词库,构建DFA算法模型
     *
     * @param sensitiveWordSet 敏感词库
     */
    private static void initSensitiveWordMap(Set<String> sensitiveWordSet) {
        //初始化敏感词容器,减少扩容操作
        sensitiveWordMap = new HashMap<String, String>(sensitiveWordSet.size());
        Map<Object, Object> temp;
        Map<Object, Object> newWorMap;
        //遍历sensitiveWordSet
        for (String key : sensitiveWordSet) {
            temp = sensitiveWordMap;
            for (int i = 0; i < key.length(); i++) {
                //转换成char型
                char keyChar = key.charAt(i);
                //库中获取关键字
                Object wordMap = temp.get(keyChar);
                //如果存在该key,直接赋值,用于下一个循环获取
                if (wordMap != null) {
                    temp = (Map) wordMap;
                } else {
                    //不存在则,则构建一个map,同时将isEnd设置为0,因为他不是最后一个
                    newWorMap = new HashMap<>();
                    //不是最后一个
                    newWorMap.put("isEnd", "0");
                    temp.put(keyChar, newWorMap);
                    temp = newWorMap;
                }
                //最后一个
                if (i == key.length() - 1) temp.put("isEnd", "1");
            }
        }
    }

    /**
     * 检查文字中是否包含敏感字符,检查规则如下:
     *
     * @param txt
     * @param beginIndex
     * @return 如果存在, 则返回敏感词字符的长度, 不存在返回0
     */
    private static int checkSensitiveWord(String txt, int beginIndex) {
        //敏感词结束标识位:用于敏感词只有1位的情况
        boolean flag = false;
        //匹配标识数默认为0
        int matchFlag = 0;
        char word;
        Map nowMap = sensitiveWordMap;
        for (int i = beginIndex; i < txt.length(); i++) {
            word = txt.charAt(i);
            //获取指定key
            nowMap = (Map) nowMap.get(word);
            if (nowMap != null) {//存在,则判断是否为最后一个
                //找到相应key,匹配标识+1
                matchFlag++;
                //如果为最后一个匹配规则,结束循环,返回匹配标识数
                if ("1".equals(nowMap.get("isEnd"))) {
                    //结束标志位为true
                    flag = true;
                }
            } else {//不存在,直接返回
                break;
            }
        }
        if (matchFlag < 2 || !flag) {//长度必须大于等于1,为词
            matchFlag = 0;
        }
        return matchFlag;
    }


    /**
     * 判断文字是否包含敏感字符
     * <p>
     * 文本
     * <p>
     * 若包含返回true,否则返回false
     */
    public static boolean contains(String txt) {
        if (sensitiveWordMap == null) {
            initContext();
        }
        boolean flag = false;
        for (int i = 0; i < txt.length(); i++) {
            int matchFlag = checkSensitiveWord(txt, i); //判断是否包含敏感字符
            if (matchFlag > 0) {//大于0存在,返回true
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 获取文字中的敏感词
     * <p>
     * txt文字
     */
    public static List<String> getSensitiveWord(String txt) {
        if (sensitiveWordMap == null) {
            initContext();
        }
        List<String> sensitiveWordList = new ArrayList<>();
        for (int i = 0; i < txt.length(); i++) {
            //判断是否包含敏感字符
            int length = checkSensitiveWord(txt, i);
            if (length > 0) {//存在,加入list中
                sensitiveWordList.add(txt.substring(i, i + length));
                i = i + length - 1;//减1的原因,是因为for会自增
            }
        }
        return sensitiveWordList;
    }

    /**
     * 把敏感词替换成*
     */
    public static String sensitiveReplace(String txt) {
        if (StringUtils.isEmpty(txt)) return "";
        List<String> list = getSensitiveWord(txt);
        for (String s : list) {
            txt = txt.replace(s, "*");
        }
        return txt;
    }

//    public static void main(String[] args) {
//        String str = "徐凯卖淫嫖娼杀人犯法共产党国民党法轮大法T.M.Dsswww.315ts.net";
//        System.err.println(SensitiveFilterUtil.sensitiveReplace(str));
//
//    }
}