package com.saki.plugin.dictionary;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import java.util.zip.CRC32;

import com.saki.QVMProtect;
import com.saki.d.n;
import com.setqq.script.Msg;

import com.saki.encode.Canvart;
import com.saki.encode.Code;
import com.saki.log.SQLog;
import com.saki.tools.FileUtil;
@QVMProtect
public class DictionaryV8 {
    public static final File dicFile = new File(FileUtil.BASE_PATH
            + "DictionaryV8/dic.txt");

    public static String decode(String text) throws IOException {
        return n.b(Canvart.Hex2Byte(text));
    }

    public static String encode(String text) {
        return n.a(text.getBytes());
    }

    private BufferedWriter writer;
    private boolean isCache = false;
    private String key;
    private HashMap<String, String[]> cache = new HashMap<String, String[]>();

    // 检查dic文件
    public boolean checkDictionaryFile() {
        if (!dicFile.exists()) {
            if (!dicFile.getParentFile().exists())
                dicFile.getParentFile().mkdirs();
            try {
                return dicFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return false;
        }
        return true;
    }

    // 查找key
    public BufferedReader findKey(String key, int type, String charset) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    new FileInputStream(dicFile), charset));
            if (reader != null) {
                String line = null;
                while ((line = reader.readLine()) != null) {
                    if (line.equals("") || line.indexOf("##") == 0) {
                        continue;
                    }
                    if (line.indexOf("[友]") == 0
                            && type != Msg.TYPE_BUDDY_MSG) {
                        findValue(reader, false);
                        continue;
                    }
                    if (line.indexOf("[群]") == 0
                            && type != Msg.TYPE_GROUP_MSG) {
                        findValue(reader, false);
                        continue;
                    }
                    if (!key.matches(line.replace("[友]", "").replace("[群]", ""))) {
                        findValue(reader, false);
                        continue;
                    }
                    this.key = line;
                    return reader;
                }
            }
        } catch (IOException e) {
            SQLog.e("无法打开并读取dic:" + e.toString());
        }
        return null;
    }

    // 查找内容
    public String[] findValue(BufferedReader reader, boolean decode) {
        ArrayList<String> value = new ArrayList<String>();
        String line = null;
        try {
            while ((line = reader.readLine()) != null && !line.equals("")) {
                if (line.indexOf("##") == 0 || line.indexOf("//") == 0)
                    continue;
                if (line.matches("[0-9a-fA-F ]+")) {
                    String[] mvalue = null;
                    if (decode)
                        mvalue = decode(line).split("\n");
                    return mvalue;
                } else {
                    value.add(line);
                }
            }
            String[] mvalue = value.toArray(new String[value.size()]);
            if (isCache) {
                if (this.key != null)
                    cache.put(this.key, mvalue);
            }
            return mvalue;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String[]{};
    }

    public String[] findValueOfCache(String key) {
        Iterator<String> it = cache.keySet().iterator();
        while (it.hasNext()) {
            String k = it.next();
            if (k != null) {
                if (key.matches(k))
                    return cache.get(k);
            }
        }
        return null;
    }

    public boolean isCacheMode() {
        return isCache;
    }

    // 打开dic
    public boolean openDictionary() {
        try {
            writer = new BufferedWriter(new FileWriter(dicFile));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void setCache(boolean checked) {
        isCache = checked;
    }
}
