package com.ruoyi.sensitive.service;

import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.ruoyi.sensitive.cache.SensitiveWordConfigCache;
import com.ruoyi.sensitive.dto.SensitiveConfig;
import com.ruoyi.sensitive.dto.WordTestResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SensitiveWordManager {

    @Autowired
    private SensitiveWordBs sensitiveWordBs;
    @Autowired
    private SensitiveWordConfigCache sensitiveWordConfigCache;

    public WordTestResp test(String text) {
        String results = sensitiveWordBs.replace(text);
        WordTestResp wordTestResp = new WordTestResp();
        wordTestResp.setSourceText(text);
        wordTestResp.setTargetText(results);
        return wordTestResp;
    }

    public void updateSensitiveConfig(SensitiveConfig sensitiveConfig) {
        sensitiveWordBs
                .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();
        sensitiveWordConfigCache.setConfig(sensitiveConfig);
    }

    public SensitiveConfig getSensitiveConfig(){
        return sensitiveWordConfigCache.getConfig();
    }

}
