package com.uas.search.analyzer;

import org.wltea.analyzer.cfg.Configuration;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * 替代 {@link org.wltea.analyzer.cfg.DefaultConfig} , 增加 loadExtDic 参数
 *
 * @author sunyj
 * @since 2017/11/21 17:00
 */
public class DefaultConfig implements Configuration {
    private static final String PATH_DIC_MAIN = "org/wltea/analyzer/dic/main2012.dic";
    private static final String PATH_DIC_QUANTIFIER = "org/wltea/analyzer/dic/quantifier.dic";
    private static final String FILE_NAME = "IKAnalyzer.cfg.xml";
    private static final String EXT_DICT = "ext_dict";
    private static final String EXT_STOP = "ext_stopwords";
    private Properties props = new Properties();
    private boolean useSmart;
    private boolean loadExtDic;

    public DefaultConfig(boolean useSmart, boolean loadExtDic) {
        this.useSmart = useSmart;
        this.loadExtDic = loadExtDic;
        // 如果指定需要加载自定义词典，再进行加载
        if (loadExtDic) {
            InputStream input = this.getClass().getClassLoader().getResourceAsStream("IKAnalyzer.cfg.xml");
            if (input != null) {
                try {
                    this.props.loadFromXML(input);
                } catch (InvalidPropertiesFormatException var3) {
                    var3.printStackTrace();
                } catch (IOException var4) {
                    var4.printStackTrace();
                }
            }
        }
    }

    public boolean useSmart() {
        return this.useSmart;
    }

    public void setUseSmart(boolean useSmart) {
        this.useSmart = useSmart;
    }

    public boolean loadExtDic() {
        return loadExtDic;
    }

    public String getMainDictionary() {
        return "org/wltea/analyzer/dic/main2012.dic";
    }

    public String getQuantifierDicionary() {
        return "org/wltea/analyzer/dic/quantifier.dic";
    }

    public List<String> getExtDictionarys() {
        ArrayList extDictFiles = new ArrayList(2);
        String extDictCfg = this.props.getProperty("ext_dict");
        if (extDictCfg != null) {
            String[] filePaths = extDictCfg.split(";");
            if (filePaths != null) {
                String[] var7 = filePaths;
                int var6 = filePaths.length;

                for (int var5 = 0; var5 < var6; ++var5) {
                    String filePath = var7[var5];
                    if (filePath != null && !"".equals(filePath.trim())) {
                        extDictFiles.add(filePath.trim());
                    }
                }
            }
        }

        return extDictFiles;
    }

    public List<String> getExtStopWordDictionarys() {
        ArrayList extStopWordDictFiles = new ArrayList(2);
        String extStopWordDictCfg = this.props.getProperty("ext_stopwords");
        if (extStopWordDictCfg != null) {
            String[] filePaths = extStopWordDictCfg.split(";");
            if (filePaths != null) {
                String[] var7 = filePaths;
                int var6 = filePaths.length;

                for (int var5 = 0; var5 < var6; ++var5) {
                    String filePath = var7[var5];
                    if (filePath != null && !"".equals(filePath.trim())) {
                        extStopWordDictFiles.add(filePath.trim());
                    }
                }
            }
        }

        return extStopWordDictFiles;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass() || !(obj instanceof DefaultConfig)) {
            return false;
        }
        DefaultConfig defaultConfig = (DefaultConfig) obj;
        return Objects.equals(useSmart, defaultConfig.useSmart()) && Objects.equals(loadExtDic, defaultConfig.loadExtDic());
    }
}
