package com.saki.qq.wtlogin.pack.send;

import java.util.ArrayList;
import java.util.Random;

import com.saki.encode.Canvart;
import com.saki.qq.global.Global;
import com.saki.qq.wtlogin.pack.Pack;
import com.saki.qq.wtlogin.pack.protobuff.ProtoBuff;
import com.saki.qq.wtlogin.pack.send.data.MsgData;

public class MessageSvc {
    public static class PbGetMsg extends T0B01 {
        public static final String INFO = "model:HTC Evo - 4.1.1 - SendInterface 16 - 720x1280;os:16;version:v2man:Genymotionsys:vbox86p-userdebug 4.1.1 JRO03S eng.buildbot.20151117.133415 test-keys";
        public static byte[] bin = new byte[0];

        public PbGetMsg(int seq) {
            super("MessageSvc.PbGetMsg");
            setSeq(seq);
        }

        @Override
        protected byte[] getContent() {
            Pack p = new Pack();
            p.appendBytes(ProtoBuff.writeVarint(1, 0));
            p.appendBytes(ProtoBuff.writeLengthDelimt(2, INFO.getBytes()));
            p.appendBytes(ProtoBuff.writeVarint(3, System.currentTimeMillis()));
            p.appendBytes(ProtoBuff.writeVarint(4, System.currentTimeMillis()));
            p.appendBytes(ProtoBuff.writeVarint(5, System.currentTimeMillis()));
            p.appendBytes(ProtoBuff.writeVarint(6, System.currentTimeMillis()));
            p.addInHead(Canvart.Int2Bytes(p.length() + 4));
            p.addInHead(msgCookie);
            p.addInHead(Canvart.Int2Bytes(8));
            p.addInHead(command.getBytes());
            p.addInHead(Canvart.Int2Bytes(command.length() + 4));
            return p.getAll();
            // return Canvart
            // .Hex2Byte("08 00 12 26 08 98 C8 C7 A5 05 10 98 C8 C7 A5 05 28 8A 8A 96 A9 06 30 B5 8B 01 38 8A 8A A6 E1 0E 40 92 8F 01 48 F3 E7 BE 80 0E 18 00 20 14 28 03 30 01 38 01");
        }

        @Override
        protected void packInfo() {
            packContent();
        }

        @Override
        public byte[] toByteArray() {
            packInfo();
            Pack p = new Pack();
            p.appendBytes(ProtoBuff.writeVarint(1, 0));
            p.appendBytes(ProtoBuff.writeLengthDelimt(2, Global.MSGCOOKIE));
            p.appendBytes(ProtoBuff.writeVarint(3, 0));
            p.appendBytes(ProtoBuff.writeVarint(4, 20));
            p.appendBytes(ProtoBuff.writeVarint(5, 3));
            p.appendBytes(ProtoBuff.writeVarint(6, 1));
            p.appendBytes(ProtoBuff.writeVarint(7, 1));
            p.appendBytes(ProtoBuff.writeVarint(9, 0));
            p.addInHead(Canvart.Int2Bytes(p.length() + 4));
            pack.appendBytes(p.getAll());
            packHead();
            return pack.getAll();
        }
    }

    public static class PbSendMsg extends T0B01 {
        public static final int FRIENDMSG = 1;
        public static final int GROUPMSG = 2;
        public static final int SESSMSG = 3;
        public static final int DISMSG = 4;
        private static int msgSeq = 10000;

        public static final int getMsgSeq() {
            return msgSeq++;
        }

        public long id = -1;
        public long uin = -1;
        public long code = -1;
        public int msgType = 1;

        public ArrayList<MsgData> msgs = new ArrayList<MsgData>();

        public PbSendMsg(int packseq, long id, long code, long uin) {
            super("MessageSvc.PbSendMsg");
            setSeq(packseq);
            this.id = id;
            this.code = code;
            this.uin = uin;
            if (uin != -1) {
                if (code != -1)
                    msgType = SESSMSG;
                else
                    msgType = FRIENDMSG;
            } else if (id == -1)
                msgType = GROUPMSG;
            else
                msgType = DISMSG;
        }

        public void addMsg(int index, MsgData data) {
            if (data != null && index >= 0) {
                msgs.add(index, data);
            }
        }

        public void addMsg(MsgData data) {
            if (data != null)
                msgs.add(data);
        }

        @Override
        protected byte[] getContent() {
            Pack p = new Pack();
            p.appendBytes(getTarget());
            p.appendBytes(getStyle());
            Pack msg = new Pack();
            for (MsgData data : msgs) {
                msg.appendBytes(data.getBytes());
            }
            byte[] bin = ProtoBuff.writeLengthDelimt(1, msg.getAll());
            bin = ProtoBuff.writeLengthDelimt(3, bin);
            p.appendBytes(bin);
            p.appendBytes(ProtoBuff.writeVarint(4, getMsgSeq()));
            p.appendBytes(ProtoBuff.writeVarint(5,
                    Math.abs(new Random().nextInt())));
            if (msgType == SESSMSG || msgType == FRIENDMSG) {
                p.appendBytes(ProtoBuff.writeLengthDelimt(6, Global.MSGCOOKIE));
            }
            int i = 3 - msgType;
            if (i < 0) {
                i = 0;
            }
            p.appendBytes(ProtoBuff.writeVarint(8, i));
            return p.getAll();
        }

        private byte[] getStyle() {
            Pack p = new Pack();
            int i = msgType == FRIENDMSG ? 0 : 1;
            p.appendBytes(ProtoBuff.writeVarint(1, i));
            p.appendBytes(ProtoBuff.writeVarint(2, 0));
            p.appendBytes(ProtoBuff.writeVarint(3, 0));
            return ProtoBuff.writeLengthDelimt(2, p.getAll());
        }

        private byte[] getTarget() {
            if (msgType == SESSMSG) {
                Pack p = new Pack();
                p.appendBytes(ProtoBuff.writeVarint(1, code));
                p.appendBytes(ProtoBuff.writeVarint(2, uin));
                return ProtoBuff.writeLengthDelimt(1,
                        ProtoBuff.writeLengthDelimt(SESSMSG, p.getAll()));
            } else if (msgType == FRIENDMSG) {
                return ProtoBuff.writeLengthDelimt(
                        1,
                        ProtoBuff.writeLengthDelimt(FRIENDMSG,
                                ProtoBuff.writeVarint(1, uin)));
            } else if (msgType == GROUPMSG) {
                return ProtoBuff.writeLengthDelimt(
                        1,
                        ProtoBuff.writeLengthDelimt(GROUPMSG,
                                ProtoBuff.writeVarint(1, code)));
            } else {
                return ProtoBuff.writeLengthDelimt(
                        1,
                        ProtoBuff.writeLengthDelimt(DISMSG,
                                ProtoBuff.writeVarint(1, id)));
            }
        }
    }
}
