package com.ruoyi.sensitive.config;

import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.support.allow.WordAllows;
import com.github.houbb.sensitive.word.support.deny.WordDenys;
import com.ruoyi.sensitive.cache.SensitiveWordConfigCache;
import com.ruoyi.sensitive.dto.SensitiveConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author binbin.hou
 * @since 1.1.0
 */
@Configuration
public class SensitiveWordConfig {

    @Autowired
    private MyDdWordAllow myDdWordAllow;

    @Autowired
    private MyDdWordDeny myDdWordDeny;
    @Autowired
    private SensitiveWordConfigCache sensitiveWordConfigCache;

    /**
     * 初始化引导类
     * @return 初始化引导类
     * @since 1.0.0
     */
    @Bean
    public SensitiveWordBs sensitiveWordBs() {
        SensitiveConfig sensitiveConfig = sensitiveWordConfigCache.getConfig();
        if(sensitiveConfig == null){
            sensitiveConfig = new SensitiveConfig();
        }
        return SensitiveWordBs.newInstance()
                .wordAllow(WordAllows.chains(WordAllows.defaults(), myDdWordAllow))
                .wordDeny(WordDenys.chains(WordDenys.defaults(), myDdWordDeny))
                // 各种其他配置
                .ignoreCase(sensitiveConfig.isIgnoreCase())
                .ignoreWidth(sensitiveConfig.isIgnoreWidth())
                .ignoreNumStyle(sensitiveConfig.isIgnoreNumStyle())
                .ignoreChineseStyle(sensitiveConfig.isIgnoreChineseStyle())
                .ignoreEnglishStyle(sensitiveConfig.isIgnoreEnglishStyle())
                .ignoreRepeat(sensitiveConfig.isIgnoreRepeat())
                .enableNumCheck(sensitiveConfig.isEnableNumCheck())
                .enableEmailCheck(sensitiveConfig.isEnableEmailCheck())
                .enableUrlCheck(sensitiveConfig.isEnableUrlCheck())
                .enableWordCheck(sensitiveConfig.isEnableWordCheck())
                .numCheckLen(sensitiveConfig.getNumCheckLen())
                .init();
    }

}
