package com.saki.qq.global;

import java.util.ArrayList;
import com.saki.log.Log;
import com.saki.qq.wtlogin.unpack.ReMessageSvc.PbGetMsg;
import com.saki.qq.wtlogin.unpack.ReOnlinePush.PbPushDisMsg;
import com.saki.qq.wtlogin.unpack.ReOnlinePush.PbPushGroupMsg;
import com.saki.qq.wtlogin.unpack.ReOnlinePush.PbPushTransMsg;
import com.saki.qq.wtlogin.unpack.ReProfileService.Pb.ReqSystemMsgNew.Group.SysMsg;
import com.saki.service.NewService;
import com.saki.tools.FileUtil;
import com.saki.tools.ScriptUtil;
import com.saki.ui.listener.IMessageListener;
import com.saki.ui.listener.IPluginListener;
import android.annotation.SuppressLint;

import com.setqq.script.ScriptLoader;
import com.setqq.script.Msg;

import com.saki.sqv9.R;
import com.setqq.script.Script;
import com.saki.plugin.dictionary.AppDictionaryV8;


@SuppressLint("NewApi")
public class ScriptManager implements IMessageListener {
    public static ScriptManager getInstance() {
        return mana;
    }

    private boolean buddy_repley = true;
    private boolean diccuse_replay = true;
    private static final ScriptManager mana = new ScriptManager();

    public void load() {
        ScriptUtil.checkScriptPublicLibraryFile();
        ScriptLoader.load(FileUtil.SCRIPT_PATH,null);
    }

    private AppDictionaryV8 dic;
    private ArrayList<Script> scripts = new ArrayList<Script>();
    private IPluginListener l;

    public Script findScript(String packname){
        if(packname != null) {
            for (Script p : scripts) {
                if (packname.equals(p.packageName))
                    return p;
            }
        }
        return null;
    }
    public void addScript(Script p) {
        scripts.remove(p);
        if (l != null)
            l.onPluginAdd(p);
        scripts.add(p);
    }
    public void flushScript(){
        if(l != null)
            l.onFlush();
    }
    public void clear() {
        for (Script p : scripts) {
            p.stop();
        }
        scripts.clear();
        if (l != null) {
            l.onPluginClear();
        }
    }

    public AppDictionaryV8 getDictionary() {
        return dic;
    }

    public void loadDictionary(NewService context) {
        dic = new AppDictionaryV8();
        dic.onLoad(context);
        new Script(context, "", "高级词库", "小社", "1.0", "使用dic前请打开此插件", false,
                context.getResources().getDrawable(R.drawable.ic_launcher), dic);
    }

    @Override
    public void onDisMsg(PbPushDisMsg msg) {
        if (!diccuse_replay || msg.fromUin == 1000000)
            return;
        for (Script p : scripts) {
            if (!p.opration || msg.fromUin == Global.qq)
                continue;
            Msg m = new Msg();
            m.type = Msg.TYPE_DIS_MSG;
            m.groupid = msg.groupId;
            m.groupName = msg.groupName;
            m.uin = msg.fromUin;
            m.uinName = msg.fromName;
            m.setData(msg.msg);
            m.time = msg.sendTime;
            m.title = msg.sendTitle;
            p.handlerMsg(m);
        }
    }

    @Override
    public void onGroupMsg(PbPushGroupMsg msg) {
        if (msg.fromUin == 1000000)
            return;
        long code = TroopManager.getInstance().id2code(msg.groupId);
        for (Script p : scripts) {
            if (!p.opration || !TroopManager.getInstance().isOPen(msg.groupId)
                    || msg.fromUin == Global.qq)
                continue;
            Msg m = new Msg();
            m.type = Msg.TYPE_GROUP_MSG;
            m.code = code;
            m.groupid = msg.groupId;
            m.groupName = msg.groupName;
            m.uin = msg.fromUin;
            m.uinName = msg.fromName;
            m.setData(msg.msg);
            m.time = msg.sendTime;
            m.title = msg.sendTitle;
            p.handlerMsg(m);
        }
    }

    @Override
    public void onOtherMsg(PbGetMsg msg) {
        if (msg.msgList.size() > 0) {
            // 只取用最后一条消息
            PbGetMsg.Msg ms = msg.msgList.get(msg.msgList.size() - 1);
            //Log.i(this,"[其他消息]"+ms.type+" "+ms.groupid+" "+ms.fromUin+" "+ms.applyName+" "+ms.code+" "+ms.sendTime+" "+ms.msg);
            // 对好友消息进行处理
            if (buddy_repley && ms.fromUin != Global.qq
                    && ms.type == PbGetMsg.T_MSG) {
                Log.i(this, ms.toString());
                for (Script p : scripts) {
                    if (!p.opration)
                        continue;
                    Msg m = new Msg();
                    m.type = Msg.TYPE_BUDDY_MSG;
                    m.groupid = ms.groupid;
                    m.uin = ms.fromUin;
                    m.code = ms.code;
                    m.uinName = ms.applyName;
                    m.addMsg("msg", ms.msg);
                    m.time = ms.sendTime;
                    p.handlerMsg(m);
                }
            }
        }
    }

    @Override
    public void onSystemMsg(SysMsg msg) {
        Log.i(this, "系统消息:" + msg.group + ":" + msg.msg);
        for (Script p : scripts) {
            if (!p.opration)
                continue;
            Msg m = new Msg();
            m.type = Msg.TYPE_SYS_MSG;
            m.groupid = msg.group;
            m.uin = msg.uin;
            m.groupName = msg.groupName;
            m.time = msg.time;
            m.uinName = msg.uinName;
            m.code = msg.requstId;
            m.addMsg("msg", msg.msg);
            if (msg.inviter != 0 && msg.inviterName != null) {
                m.addMsg("inviter", msg.inviter + "@" + msg.inviterName);
            }
            p.handlerMsg(m);
        }
    }

    @Override
    public void onTransMsg(PbPushTransMsg msg) {
        Log.i(this, "变换消息:" + msg.groupId + " " + msg.uin + " " + msg.type
                + "  " + msg.b);
        for (Script p : scripts) {
            if (!p.opration || !TroopManager.getInstance().isOPen(msg.groupId))
                continue;
            Msg m = new Msg();
            if (msg.type == PbPushTransMsg.T_DELETE)
                m.type = Msg.TYPE_DELETE_MEMBER;
            else if (m.type == PbPushTransMsg.T_ADMIN)
                m.type = Msg.TYPE_ADMIN_CHANGE;
            else
                return;
            m.code = msg.code;
            m.groupid = msg.groupId;
            m.value = msg.b ? 0 : -1;
            m.uin = msg.uin;
            m.time = msg.time;
            p.handlerMsg(m);
        }
    }

    public void setBuddyReplay(boolean b) {
        this.buddy_repley = b;
    }

    public void setDicuseReplay(boolean b) {
        this.diccuse_replay = b;
    }

    public void setListener(IPluginListener l) {
        this.l = l;
    }
}
