package com.saki.plugin.dictionary;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;

import com.saki.QVMProtect;
import com.saki.tools.FileUtil;
@QVMProtect
public class Dictionary {
    public static File dicFile = new File(FileUtil.BASE_PATH
            + "DictionaryV8/dic.txt");

    LinkedHashMap<String, String[]> dic = new LinkedHashMap<String, String[]>();

    public boolean addValue(String key, String[] value) {
        if (dic.containsKey(key))
            return false;
        dic.put(key, value);
        return true;
    }

    public void dicEncode() {
        LinkedHashMap<String, String[]> m = new LinkedHashMap<String, String[]>();
        Iterator<String> it = dic.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            String[] value = dic.get(key);
            String s = "";
            for (int i = 0; i < value.length; i++) {
                s += value[i] + (i == value.length - 1 ? "" : "\n");
                // value[i] = DictionaryV8.encode(value[i]);
            }
            m.put(key, new String[]{DictionaryV8.encode(s).replace(" ", "")});
        }
        dic = m;
    }

    public HashMap<String, String[]> getDictionary() {
        return dic;
    }

    public int getLength() {
        return dic.size();
    }

    public void loadDictionary2Memory(File path, String charset)
            throws IOException {
        loadDictionary2Memory(new FileInputStream(path), charset);
    }

    public void loadDictionary2Memory(InputStream is, String charset)
            throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(is,
                charset));
        String line = null;
        while ((line = br.readLine()) != null) {
            if (line.indexOf("##") == 0 || line.equals(""))
                continue;
            String key = line;
            ArrayList<String> arr = new ArrayList<String>();
            while ((line = br.readLine()) != null && !line.equals("")) {
                arr.add(line);
            }
            dic.put(key, arr.toArray(new String[]{}));
        }
        br.close();
    }

    public void loadDictionary2Memory(String charset) throws IOException {
        loadDictionary2Memory(dicFile, charset);
    }

    public boolean removeKey(String key) {
        Iterator<String> it = dic.keySet().iterator();
        while (it.hasNext()) {
            if (it.next().equals(key)) {
                it.remove();
                return true;
            }
        }
        return false;
    }

    public void saveDictionary2File(File path, String charset)
            throws IOException {
        if (dic.size() == 0)
            return;
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(path), charset));
        bw.write("");
        Iterator<String> it = dic.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            bw.append(key);
            bw.newLine();
            String[] ss = dic.get(key);
            for (String s : ss) {
                bw.append(s);
                bw.newLine();
            }
            bw.newLine();
        }
        bw.flush();
        bw.close();
    }
}
