package com.saki.qq.wtlogin.unpack;

import com.saki.encode.Canvart;
import com.saki.encode.Code;
import com.saki.encode.UnPack;
import com.saki.log.Log;
import com.saki.qq.global.Global;
import com.saki.qq.global.Key;

public class QQUnPacket {

    private static RecviePack cmdUnpacket(String cmd, byte[] bin) {
        try {
            if (cmd.equals("wtlogin.login")) {
                return new ReServer(bin);
            }
            if (cmd.equals("GrayUinPro.Check")) {
                return new ReGrayUinPro(bin);
            }
            if (cmd.equals("OidbSvc.0x59f")) {
                return new ReOidSvc.Ox59f(bin);
            }
            if (cmd.equals("OidbSvc.0x496")) {
                return new ReOidSvc.Ox496(bin);
            }
            if (cmd.equals("OidbSvc.0x7c4_0")) {
                return new ReOidSvc.Ox7c4_0(bin);
            }
            if (cmd.equals("OidbSvc.0x7a2_0")) {
                return new ReOidSvc.Ox7a2_0(bin);
            }
            if (cmd.equals("StatSvc.SimpleGet")) {
                return new ReStatSvc.SimpleGet(bin);
            }
            if (cmd.equals("StatSvc.register")) {
                return new ReStatSvc.Register(bin);
            }
            if (cmd.equals("ConfigPushSvc.PushReq")) {
                return new ReConfigPushSvc.PushReq(bin);
            }
            if (cmd.equals("ConfigPushSvc.PushDomain")) {
                return new ReConfigPushSvc.PushDomain(bin);
            }
            if (cmd.equals("friendlist.GetTroopListReqV2")) {
                return new ReFriendList.GetTroopListReqV2(bin);
            }
            if (cmd.equals("friendlist.getFriendGroupList")) {
                return new ReFriendList.GetFriendGroupList(bin);
            }
            if (cmd.equals("friendlist.getTroopMemberList")) {
                return new ReFriendList.GetTroopMemberList(bin);
            }
            // if (cmd.equals("PhSigLcId.Check")) {
            // return new RePhSigLcId.Check(bin);
            // }
            if (cmd.equals("OnlinePush.PbPushTransMsg")) {
                return new ReOnlinePush.PbPushTransMsg(bin);
            }
            if (cmd.equals("OnlinePush.PbPushGroupMsg")) {
                return new ReOnlinePush.PbPushGroupMsg(bin);
            }
            if (cmd.equals("OnlinePush.PbPushDisMsg")) {
                return new ReOnlinePush.PbPushDisMsg(bin);
            }
            if (cmd.equals("OnlinePush.ReqPush")) {
                return new ReOnlinePush.ReqPush(bin);
            }
            if (cmd.equals("MessageSvc.PushNotify")) {
                return new ReMessageSvc.PbshNotify(bin);
            }
            if (cmd.equals("MessageSvc.PushForceOffline")) {
                return new ReMessageSvc.PushForceOffline(bin);
            }
            if (cmd.equals("MessageSvc.PbGetMsg")) {
                return new ReMessageSvc.PbGetMsg(bin);
            }
            if (cmd.equals("ProfileService.Pb.ReqSystemMsgNew.Group")) {
                return new ReProfileService.Pb.ReqSystemMsgNew.Group(bin);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ReUnHandled(bin);
    }

    public static RecviePack unPacket(byte[] data)
            throws UnknownPacketException {
        UnPack un = new UnPack(data);
        Info info = null;
        if ((info = unPacketInfo(un, Key.inUseKey())) == null)
            throw new UnknownPacketException();
        Log.i(info, "---------------" + info.cmd + "---------------");
        Log.i(info, Canvart.Bytes2Hex(un.getAll()));
        Log.i(info, "---------------------end--------------------");
        return cmdUnpacket(info.cmd, un.getAll()).setInfo(info).setData(data);
    }

    public static Info unPacketInfo(UnPack un, byte[] key) {
        String qq = String.valueOf(Global.qq);
        byte[] find = qq.getBytes();
        int index = -1;
        while ((index = un.findBytes(find)) == -1 && find.length != 3) {
            find = new byte[]{0x00, 0x05, 0x30};
        }
        if (index != -1) {
            un.getBytes(index + find.length);
            byte[] body = un.getAll();
            byte[] decode = Code.QQTEAdecrypt(body, key);
            un.setBin(decode);
            //Log.i("", Canvart.Bytes2Hex(decode));
            int len = un.getInt();
            if (len < 0 || len > un.length())
                return null;
            Info info = new Info(un.getBytes(len - 4));
            return info;
        }
        return null;
    }
}
