package com.ema.common.tools.sensitive.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.ema.common.tools.sensitive.SensitiveWordService;
import com.ema.common.tools.sensitive.config.SensitiveWordConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class SensitiveWordServiceImpl implements SensitiveWordService {

    @Resource
    private SensitiveWordConfig sensitiveWordConfig;

    private static SensitiveWordBs sensitiveWordBs;


    //是否开启日志打印输出
    private boolean enabled;

    @Override
    public SensitiveWordBs getSensitiveWordBs() {
        if (sensitiveWordBs != null) {
            return sensitiveWordBs;
        }
        sensitiveWordBs = SensitiveWordBs.newInstance()
                // 忽略大小写
                .ignoreCase(sensitiveWordConfig.isIgnoreCase())
                // 忽略半角圆角
                .ignoreWidth(sensitiveWordConfig.isIgnoreWidth())
                // 忽略数字的写法
                .ignoreNumStyle(sensitiveWordConfig.isIgnoreNumStyle())
                // 忽略中文的书写格式：简繁体
                .ignoreChineseStyle(sensitiveWordConfig.isIgnoreChineseStyle())
                // 忽略英文的书写格式
                .ignoreEnglishStyle(sensitiveWordConfig.isIgnoreEnglishStyle())
                // 忽略重复词
                .ignoreRepeat(sensitiveWordConfig.isIgnoreRepeat())
                // 是否启用邮箱检测
                .enableEmailCheck(sensitiveWordConfig.isEnableEmailCheck())
                // 是否启用链接检测
                .enableUrlCheck(sensitiveWordConfig.isEnableUrlCheck())
                // 是否启用数字检测
                .enableNumCheck(sensitiveWordConfig.isEnableNumCheck())
                // 数字检测，自定义指定长度
                .numCheckLen(sensitiveWordConfig.getNumCheckLen())
                // 配置自定义敏感词
                .wordDeny(() -> {
                    if (StrUtil.isNotBlank(sensitiveWordConfig.getDenyPath())
                            && FileUtil.exist(sensitiveWordConfig.getDenyPath())) {
                        List<String> strings = FileUtil.readUtf8Lines(sensitiveWordConfig.getDenyPath());
                        return StrUtil.split(CollUtil.join(strings, ","), ",");
                    }
                    return null;
                })
                // 配置非敏感词库
                .wordAllow(() -> {
                    if (StrUtil.isNotBlank(sensitiveWordConfig.getAllowPath())
                            && FileUtil.exist(sensitiveWordConfig.getAllowPath())) {
                        List<String> strings = FileUtil.readUtf8Lines(sensitiveWordConfig.getAllowPath());
                        return StrUtil.split(CollUtil.join(strings, ","), ",");
                    }
                    return null;
                })
                .init();
        return sensitiveWordBs;
    }

    /**
     * 刷新敏感词库与非敏感词库缓存
     */
    @Override
    public void refresh() {
        sensitiveWordBs = null;
        getSensitiveWordBs();
    }

    @Override
    public void add(List<String> words) {
        getSensitiveWordBs().addWord(words);
    }

    /**
     * 判断是否含有敏感词
     */
    @Override
    public boolean contains(String text) {
        return getSensitiveWordBs().contains(text);
    }

    /**
     * 使用默认替换符 * 进行替换敏感词
     */
    @Override
    public String replace(String text) {
        return getSensitiveWordBs().replace(text);
    }

    /**
     * 返回所有敏感词
     */
    @Override
    public List<String> findAll(String text) {
        return getSensitiveWordBs().findAll(text);
    }
}
