package com.saki.service;

import java.util.ArrayList;
import java.util.HashMap;

import com.saki.QVMProtect;
import com.saki.client.Client;
import com.saki.client.ServiceNotification;
import com.saki.log.Log;
import com.saki.qq.global.ScriptManager;
import com.saki.qq.wtlogin.pack.send.FriendList;
import com.saki.qq.wtlogin.pack.send.OidbSvc;
import com.saki.qq.wtlogin.pack.send.ProfileService;
import com.saki.qq.wtlogin.pack.send.VisitorSvc;
import com.saki.qq.wtlogin.pack.send.MessageSvc.PbSendMsg;
import com.saki.qq.wtlogin.pack.send.data.MsgData;
import com.saki.ui.i.IQP;
import com.saki.ui.i.IUI;
import com.setqq.script.ScriptLoader;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
@QVMProtect
public class NewService extends Service implements IQP {
    public class NewBinder extends Binder {
        public NewService getService() {
            return NewService.this;
        }
    }

    private Client client;
    private KeepTimeCountThread keep;
    private IUI ui;
    private ServiceNotification n;

    @Override
    public void agreeJoin(long group, long uin, long reqid, boolean agree) {
        client.send(new ProfileService.Pb.ReqSystemMsgAction.Group(client
                .getRequstionID(), group, uin, reqid, agree).toByteArray());
    }

    @Override
    public void deleteGroupMember(long group, long uin) {
        client.send(new OidbSvc.Ox8a0_0(client.getRequstionID(), group, uin)
                .toByteArray());
    }

    @Override
    public void getFriendList() {
        client.send(new FriendList.GetFriendGroupList(client.getRequstionID())
                .toByteArray());
    }

    @Override
    public void getGroupList() {
        client.send(new FriendList.GetTroopListReqV2(client.getRequstionID())
                .toByteArray());
    }

    @Override
    public void getGroupMember(long group) {
        client.send(new FriendList.GetTroopMemberList(client.getRequstionID(),
                group).toByteArray());
    }

    public void loadPlugin() {
        ScriptManager.getInstance().clear();
        ScriptManager.getInstance().loadDictionary(this);
        ScriptManager.getInstance().load();
    }

    @Override
    public void login(final String id,final String password,final int version) {
        client.login(id,password,version);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new NewBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        ScriptLoader.initLoader(this);//初始化脚本加载器
        n = new ServiceNotification(this);
        n.startForeground();
        client = new Client(this);//创建客户端
        keep = new KeepTimeCountThread(n);
        keep.start();
    }

    @Override
    public void onDestroy() {
        keep.interrupt();
        n.stopForeground();
        client.releseSocket();
        super.onDestroy();
        System.exit(0);
    }

    @Override
    public void putFavorite(long uin) {
        client.send(new VisitorSvc.ReqFavorite(client.getRequstionID(), uin).toByteArray());
    }

    @Override
    public void sendMsg(final long id, final long group, final long uin, final HashMap<String, ArrayList<String>> data) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (data != null) {
                    PbSendMsg m = new PbSendMsg(client.getRequstionID(), id,
                            group, uin);
                    ArrayList<String> index = data.get("index");
                    if (index != null)// 序号消息
                    {
                        for (int i = 0; i < index.size(); i++) {
                            String key = index.get(i);
                            ArrayList<String> msg = data.get(key);
                            if (msg != null && msg.size() > 0)
                                MsgData.pushMsgData(client, m,index.size(), i, key, msg.remove(0));
                        }
                    }
                }
            }
        }).start();
    }

    @Override
    public void setMemberCard(long group, long uin, String card) {
        client.send(new FriendList.ModifyGroupCardReq(client.getRequstionID(),
                group, uin, card).toByteArray());
    }

    @Override
    public void setShutup(long group, boolean open) {
        client.send(new OidbSvc.Ox89a_0(client.getRequstionID(), group, open)
                .toByteArray());
    }

    @Override
    public void setShutup(long group, long uin, int time) {
        client.send(new OidbSvc.Ox570_8(client.getRequstionID(), group, uin,
                time).toByteArray());
    }

    public void setUpdataListener(IUI ui) {
        this.ui = ui;
    }

    public void updataUI(int cmd, Object data) {
        if (ui != null)
            ui.updateUI(cmd, data);
    }

    @Override
    public void verifyCode(String value) {
        client.verifyCode(value);
    }
}
