package com.lantone.qc.nlp.participle.cfg;

import com.lantone.qc.nlp.participle.word.Segment;
import com.lantone.qc.nlp.util.EncrypDES;
import com.lantone.qc.pub.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DefaultConfig implements Configuration {
    Logger logger = LoggerFactory.getLogger(DefaultConfig.class);
    private static final String DEFAULT_PATH = "tc.dict";
    private List<Character[]> contents = new ArrayList<Character[]>();

    private Map<Integer, Character> chars = new HashMap<Integer, Character>(100, 0.75f);
    private Integer charsKey;

    Segment segment;

    private String path_prefix = "";

    public DefaultConfig() {
    }

    public Segment loadMainDict(String path) {
        List<String> fileContents = readFileContents(path);
        logger.info("读取文件" + path + "结果：" + fileContents.size() + "行");
        return loadMainDict(fileContents);
    }

    public Segment loadMainDict(List<String> dicts) {
        int i = 0;
        int length = dicts.size();

        segment = new Segment(Character.valueOf('0'));
        try {
            String[] line_string;
            char[] chars;
            for (i = 0; i < length; i++) {
                String s = dicts.get(i);
                line_string = org.apache.commons.lang3.StringUtils.split(dicts.get(i), "\\|");
                chars = line_string[0].toCharArray();
                if (line_string.length == 4) {
                    segment.fill(segment, chars, 0, chars.length, Float.parseFloat(line_string[1]), line_string[2], line_string[3]);
                } else if (line_string.length == 1) {
                    segment.fill(segment, chars, 0, chars.length);
                } else {
                    continue;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return segment;
    }

    public Map<String, String> loadMapDict(String path) {
        List<String> fileContents = readFileContents(path);
        String[] line_string;
        Map<String, String> map = new HashMap<>(10, 0.8f);
        try {
            for (int i = 0; i < fileContents.size(); i++) {
                line_string = org.apache.commons.lang3.StringUtils.split(fileContents.get(i), "\\|");
                if (line_string.length == 2) {
                    if (map.get(line_string[0]) != null) {
                        map.put(line_string[0], map.get(line_string[0]) + "," + line_string[1]);
                    } else {
                        map.put(line_string[0], line_string[1]);
                    }
                } else {
                    continue;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public List<String> readFileContents(String path) {
        if (!StringUtils.isEmpty(path_prefix)) {
            path = path_prefix + path;
        }
        return readDirectFileContents(path);
    }

    public List<String> readDirectFileContents(String path) {
        InputStream is = null;
        List<String> fileContents = new ArrayList<String>(10);
        try {
            if (StringUtils.isEmpty(path)) {
                path = DEFAULT_PATH;
            }
            //            is = this.getClass().getClassLoader().getResourceAsStream(path);
            File file = new File(path);
            //            File file = new File(this.getClass().getResource(path).getFile());
            is = new FileInputStream(file);
            if (is == null) {
                throw new RuntimeException(path + ".......文件未找到!!");
            }

            addFileContents(is, fileContents);
        } catch (Exception ioe) {
            System.err.println("读取文件" + path + "出错.......................");
            ioe.printStackTrace();
        } finally {
            this.close(is);
        }
        return fileContents;
    }

    public List<String> readTargetFileContents(String path) {
        InputStream is = null;
        List<String> fileContents = new ArrayList<String>(10);
        try {
            if (StringUtils.isEmpty(path)) {
                path = DEFAULT_PATH;
            }
            is = this.getClass().getClassLoader().getResourceAsStream(path);
            //            File file = new File(path);
            //            File file = new File(this.getClass().getResource(path).getFile());
            //            is = new FileInputStream(file);
            if (is == null) {
                throw new RuntimeException(path + ".......文件未找到!!");
            }

            addFileContents(is, fileContents);
        } catch (Exception ioe) {
            System.err.println("读取文件" + path + "出错.......................");
            ioe.printStackTrace();
        } finally {
            this.close(is);
        }
        return fileContents;
    }

    public void addFileContents(InputStream is, List<String> fileContents) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"), 512);

        EncrypDES encrypDES = new EncrypDES();
        String theWord = null;
        int i = 0;
        do {
            theWord = br.readLine();
            if (theWord != null && !"".equals(theWord.trim())) {
                fileContents.add(encrypDES.decryptor(theWord.trim()));
            }
            i++;
        } while (theWord != null);
    }


    public void writeFileContents(List<String> contents, String path) {
        this.writeFileContents(contents, path, "");
    }

    public void writeFileContents(List<String> contents, String path, String separator) {
        if (!StringUtils.isEmpty(path_prefix)) {
            path = path_prefix + path;
        }
        try {
            FileWriter fw = new FileWriter(path);
            for (String content : contents) {
                fw.write(content);
                if (!StringUtils.isEmpty(separator)) {
                    fw.write(separator);
                }
            }
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void close(InputStream is) {
        try {
            if (is != null) {
                is.close();
                is = null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
