package com.alterdekim.hearthhack.component.processor;


import com.alterdekim.Protocol;
import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.util.BattleNetPacket;
import com.alterdekim.hearthhack.util.Util;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;

@Slf4j
public class PresenceProcessor extends Processor {

    public PresenceProcessor() {
        this.setProcessorId(4);
    }

    @Override
    public void process(BattleNetPacket packet, TcpConnection conn) throws Exception {
        switch (packet.getHeader().getMethodId()) {
            case 1:
                Protocol.SubscribeRequest subscribeRequest = Protocol.SubscribeRequest.parseFrom(packet.getBody());
                Protocol.Header header = Processor.generateResponse(0, packet.getHeader().getToken(), 0, 0);
                conn.send(new BattleNetPacket(header, new byte[0]));

                if( subscribeRequest.getObjectId() == 1 ) {
                    String ni = conn.getUserService().findById(conn.getUserId()).getDisplayName()+"#66666";
                    byte[] nickname = ni.getBytes(StandardCharsets.US_ASCII);
                   // byte[] bb = Util.hexStringToByteArray("1A"+Util.intToHex(98+nickname.length)+"AA06"+Util.intToHex(95+nickname.length)+"0A120900000000000000011193710E1A0000000012190A170A0A08CE84011001180620001209189AB1D8B499BCCC02121A0A180A0A08CE8401100118012000120A2A086A6F686E20626F6E12210A1F0A0A08CE840110011804200012102A"+Util.intToHex(nickname.length)+Util.bytesToHex(nickname));
                    byte[] bb = Util.hexStringToByteArray("1A71AA066E0A120900000000000000011193710E1A0000000012190A170A0A08CE84011001180620001209189AB1D8B499BCCC02121A0A180A0A08CE8401100118012000120A2A086A6F686E20626F6E12210A1F0A0A08CE840110011804200012"+Util.intToHex(nickname.length+2)+"2A"+Util.intToHex(nickname.length)+Util.bytesToHex(nickname));
                    bb[1] = (byte) (98 + nickname.length);
                    bb[4] = (byte) (95 + nickname.length);
                    bb[81] = (byte) (18 + nickname.length);
                    header = Protocol.Header.newBuilder(packet.getHeader())
                            .setServiceId(5)
                            .setMethodId(1)
                            .setSize(bb.length)
                            .setObjectId(1)
                            .setToken(conn.nextToken())
                            .setStatus(0)
                            .build();
                    // FIRST
                    Protocol.AddNotification addNotification = Protocol.AddNotification.parseFrom(Util.hexStringToByteArray("1A71AA066E0A120900000000000000011193710E1A0000000012190A170A0A08CE84011001180620001209189AB1D8B499BCCC02121A0A180A0A08CE8401100118012000120A2A086A6F686E20626F6E12210A1F0A0A08CE840110011804200012112A0F517569726B794F7263233239363838"));
                    //log.info("AddNotification: {}", addNotification);

                    //System.out.println(Util.bytesToHex(bb));

                    conn.send(new BattleNetPacket(header, bb));


                } else if( subscribeRequest.getObjectId() == 2 ) {
                    String ni = conn.getUserService().findById(conn.getUserId()).getDisplayName()+"#66666";
                    byte[] nickname = ni.getBytes(StandardCharsets.US_ASCII);
                    byte[] bb = Util.hexStringToByteArray("1ADB01AA06D7010A12094743545702000002116739AB040000000012210A1F0A0A08CE840110021805200012"+Util.intToHex(nickname.length+2)+"2A"+Util.intToHex(nickname.length)+Util.bytesToHex(nickname)+"12240A220A0A08CE8401100218072000121452120900000000000000011193710E1A0000000012190A170A0A08CE84011002180420001209189AB1D8B499BCCC0212120A100A0A08CE84011002180C20001202100012120A100A0A08CE84011002180120001202100012160A140A0A08CE84011002180320001206420457544347121D0A1B0A0A08CE8401100218062000120D2A0B3433373135343139352331");
                    bb[1] = (byte) (204 + nickname.length);
                    bb[5] = (byte) (200 + nickname.length);
                    bb[28] = (byte) (18 + nickname.length);
                    bb[30] = (byte) (16 + nickname.length);
                    header = Protocol.Header.newBuilder(packet.getHeader())
                            .setServiceId(5)
                            .setMethodId(1)
                            .setSize(bb.length)
                            .setObjectId(2)
                            .setToken(conn.nextToken())
                            .setStatus(0)
                            .build();
                    // SECOND
                    /*Protocol.AddNotification addNotification = Protocol.AddNotification.parseFrom(Util.hexStringToByteArray("1ADB01AA06D7010A12094743545702000002116739AB040000000012210A1F0A0A08CE840110021805200012112A0F517569726B794F726323323936383812240A220A0A08CE8401100218072000121452120900000000000000011193710E1A0000000012190A170A0A08CE84011002180420001209189AB1D8B499BCCC0212120A100A0A08CE84011002180C20001202100012120A100A0A08CE84011002180120001202100012160A140A0A08CE84011002180320001206420457544347121D0A1B0A0A08CE8401100218062000120D2A0B3433373135343139352331"));
                    System.out.println(addNotification);*/

                    conn.send(new BattleNetPacket(header, bb));

                    header = Protocol.Header.newBuilder(packet.getHeader())
                            .setServiceId(5)
                            .setMethodId(6)
                            .setSize(68)
                            .setObjectId(2)
                            .setToken(conn.nextToken())
                            .setStatus(0)
                            .build();
                    conn.send(new BattleNetPacket(header, Util.hexStringToByteArray("1242AA063F0A12094743545702000002116739AB040000000012170A150A0808CE840110021804120918F182EAF5CEBDCC0212100A0E0A0808CE84011002180112021001")));

                }
                break;
            case 3:
                header = Processor.generateResponse(0, packet.getHeader().getToken(), 0, 0);
                conn.send(new BattleNetPacket(header, new byte[0]));
                if( packet.getHeader().getToken() == 12 ) {
                  /*  Protocol.Header header1 = Processor.generateResponse(0, packet.getHeader().getToken(), 0, 0);
                    conn.send(new BattleNetPacket(header1, new byte[0]));*/
                    conn.sendRaw(Util.hexStringToByteArray("000c08fe011800200028950130000a0c08f4c9ccf30d10c687bcb805120a0889ff5c1092e5a1b9051800220e0a0c0109080a04030507060211102a2e0a2c0d55450000157a72746d1a206ff4fdd5fa5f6d62a278a04403e075d69d734cd4880732dce8edc6a3f528089230a08a95f4cebdcc02422e0a2c0d55450000157a72746d1a20b4bd0f0096a7648de1d19042fb7a79b96c0df48eea3488f498b08ad3d38fb2cc"));
                } else if( packet.getHeader().getToken() == 13 ) {
                   /* Protocol.Header header1 = Processor.generateResponse(0, packet.getHeader().getToken(), 0, 0);
                    conn.send(new BattleNetPacket(header1, new byte[0]));*/

                    Protocol.Header header1 = Protocol.Header.newBuilder()
                            .setServiceId(5)
                            .setMethodId(6)
                            .setToken(conn.nextToken())
                            .setObjectId(2)
                            .setSize(Util.hexStringToByteArray("1233AA06300A12094743545702000002116739AB0400000000121A0A180A0A08C786D1BA0510021813120A2A083833383836303830").length)
                            .setStatus(0)
                            .build();

                    conn.send(new BattleNetPacket(header1, Util.hexStringToByteArray("1233AA06300A12094743545702000002116739AB0400000000121A0A180A0A08C786D1BA0510021813120A2A083833383836303830")));
                } else if( packet.getHeader().getToken() == 18 ) {
                   /* Protocol.Header header1 = Processor.generateResponse(0, packet.getHeader().getToken(), 0, 0);
                    conn.send(new BattleNetPacket(header1, new byte[0]));*/

                    byte[] b = Util.hexStringToByteArray("122BAA06280A12094743545702000002116739AB040000000012120A100A0A08C786D1BA051002180112021000");

                    Protocol.Header header1 = Protocol.Header.newBuilder()
                            .setServiceId(5)
                            .setMethodId(6)
                            .setToken(conn.nextToken())
                            .setObjectId(2)
                            .setSize(b.length)
                            .setStatus(0)
                            .build();

                    conn.send(new BattleNetPacket(header1, b));

                   /* b = Util.hexStringToByteArray("0A120900000000000000021100000000000000001212094743545702000002116739AB04000000001A1C575443472E5574696C4E6F74696669636174696F6E4D65737361676522130A0C6D6573736167655F74797065120318CA0222140A0C667261676D656E745F30303012043202080122120A0C6D6573736167655F73697A65120218022A1209000000000000000111000000000000000032120900000000000000011193710E1A000000003A004A0A0889FF5C1092E5A1B90552150A050D93710E1A120C0D6739AB0415474354571802");

                    header = Protocol.Header.newBuilder()
                            .setServiceId(4)
                            .setMethodId(1)
                            .setToken(conn.nextToken())
                            .setObjectId(0)
                            .setSize(b.length)
                            .setStatus(0)
                            .build();

                    conn.send(new BattleNetPacket(header, b));*/

                } else if( packet.getHeader().getToken() == 26 ) {
                   /* Protocol.Header header1 = Processor.generateResponse(0, packet.getHeader().getToken(), 0, 0);
                    conn.send(new BattleNetPacket(header1, new byte[0]));*/

                    byte[] b = Util.hexStringToByteArray("1231AA062E0A12094743545702000002116739AB040000000012180A160A0A08C786D1BA051002181212083206190000190000");

                    header = Protocol.Header.newBuilder()
                            .setServiceId(5)
                            .setMethodId(6)
                            .setToken(conn.nextToken())
                            .setObjectId(2)
                            .setSize(b.length)
                            .setStatus(0)
                            .build();

                    conn.send(new BattleNetPacket(header, b));
                } else if( packet.getHeader().getToken() == 32 ) {
                    byte[] b = Util.hexStringToByteArray("0D5545000015737368001A20BEC5292231D7686AF00CE64E0C58CC6E360EA950AAFFAC6A114F03A958E275F3");

                    Protocol.Header header1 = Processor.generateResponse(b.length, packet.getHeader().getToken(), 0, 0);
                    //conn.send(new BattleNetPacket(header1, b));

                    b = Util.hexStringToByteArray("1230AA062D0A12094743545702000002116739AB040000000012170A150A0A08C786D1BA0510021811120732050103000000");

                    header = Protocol.Header.newBuilder()
                            .setServiceId(5)
                            .setMethodId(6)
                            .setToken(conn.nextToken())
                            .setObjectId(2)
                            .setSize(b.length)
                            .setStatus(0)
                            .build();
                    conn.send(new BattleNetPacket(header, b));
                } else if( packet.getHeader().getToken() == 33 ) {
                    byte[] b = Util.hexStringToByteArray("0D5545000015737368001A20BEC5292231D7686AF00CE64E0C58CC6E360EA950AAFFAC6A114F03A958E275F3");

                    Protocol.Header header1 = Processor.generateResponse(b.length, packet.getHeader().getToken(), 0, 0);
                   // conn.send(new BattleNetPacket(header1, b));
                } else if( packet.getHeader().getToken() == 36 ) {
                    byte[] b = Util.hexStringToByteArray("0A090A026964120318C6020A120A0570726F746F12093207080110C9011806");

                    Protocol.Header header1 = Processor.generateResponse(b.length, packet.getHeader().getToken(), 0, 0);
                   // conn.send(new BattleNetPacket(header1, b));

                /*    b = Util.hexStringToByteArray("0A120900000000000000021100000000000000001212094743545702000002116739AB04000000001A1C575443472E5574696C4E6F74696669636174696F6E4D65737361676522130A0C6D6573736167655F74797065120318E00122120A0C6D6573736167655F73697A65120218002A1209000000000000000111000000000000000032120900000000000000011193710E1A000000003A004A0A0889FF5C1092E5A1B90552150A050D93710E1A120C0D6739AB0415474354571802");

                    header = Protocol.Header.newBuilder()
                            .setServiceId(4)
                            .setMethodId(1)
                            .setToken(conn.nextToken())
                            .setObjectId(0)
                            .setSize(b.length)
                            .setStatus(0)
                            .build();
                    conn.send(new BattleNetPacket(header, b));*/
                }
                break;
            default:
                log.error("Can't process weird PresenceProcessor method: " + packet.getHeader().getMethodId());
                break;
        }
    }

    @Override
    public String getExportName() {
        return "bnet.protocol.presence.PresenceProcessor";
    }
}
