package org.diagbot.nlp.participle.cfg;

import org.diagbot.nlp.participle.word.Segment;
import org.diagbot.pub.utils.security.EncrypDES;
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 {
    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;

    public Segment loadMainDict(String path) {
        List<String> fileContents = readFileContents(path);
        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;
            EncrypDES encrypDES = new EncrypDES();
            for (i = 0; i < length; i++) {
                String s = dicts.get(i);
                line_string = org.apache.commons.lang3.StringUtils.split(new String(encrypDES.decryptor(dicts.get(i))), "\\|");
                chars = line_string[0].toCharArray();
                if (line_string.length == 3) {
                    segment.fill(segment, chars, 0, chars.length, Float.parseFloat(line_string[1]), line_string[2]);
                } 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 {
            EncrypDES encrypDES = new EncrypDES();
            for (int i = 0; i < fileContents.size(); i++) {
                line_string = org.apache.commons.lang3.StringUtils.split(new String(encrypDES.decryptor(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) {
        InputStream is = null;
        List<String> fileContents = new ArrayList<String>(10);
        try {
            if (StringUtils.isEmpty(path)) path = DEFAULT_PATH;
            is = this.getClass().getClassLoader().getResourceAsStream(path);
            if (is == null) {
                throw new RuntimeException(path + ".......文件未找到!!");
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"), 512);

            String theWord = null;
            do {
                theWord = br.readLine();
                if (theWord != null && !"".equals(theWord.trim())) {
                    fileContents.add(theWord.trim());
                }
            } while (theWord != null);
        } catch (IOException ioe) {
            System.err.println("读取文件" + path + "出错.......................");
            ioe.printStackTrace();
        } finally {
            this.close(is);
        }
        return fileContents;
    }

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

    public void writeFileContents(List<String> contents, String path, String separator) {
        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();
        }
    }
}
