package com.alterdekim.hearthhack.component.processor.client.request;

import com.alterdekim.Protocol;
import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.component.processor.Processor;
import com.alterdekim.hearthhack.util.BattleNetPacket;
import com.alterdekim.hearthhack.util.ClientRequestBody;
import com.alterdekim.hearthhack.util.Util;
import com.google.protobuf.ByteString;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

import static com.alterdekim.hearthhack.util.GameUtilities.generateNotification;

@Slf4j
public class GetDeck extends ClientRequestParser {
    @Override
    public void parse(BattleNetPacket packet, ClientRequestBody body, TcpConnection conn) throws Exception {
        Protocol.GetDeckContents request = Protocol.GetDeckContents.parseFrom(body.getBody());

        List<Protocol.DeckCardData> ddl = new ArrayList<>();
        for( int i = 0; i < 10; i++ ) {
            ddl.add(Protocol.DeckCardData.newBuilder()
                    .setDef(Protocol.CardDef.newBuilder().setAsset(242)).setHandle(0).setQty(2).setPrev(0).build());
        }
        for( int i = 0; i < 10; i++ ) {
            ddl.add(Protocol.DeckCardData.newBuilder()
                    .setDef(Protocol.CardDef.newBuilder().setAsset(670)).setHandle(0).setQty(2).setPrev(0).build());
        }
        for( int i = 0; i < 5; i++ ) {
            ddl.add(Protocol.DeckCardData.newBuilder()
                    .setDef(Protocol.CardDef.newBuilder().setAsset(555)).setHandle(0).setQty(2).setPrev(0).build());
        }
        for( int i = 0; i < 5; i++ ) {
            ddl.add(Protocol.DeckCardData.newBuilder()
                    .setDef(Protocol.CardDef.newBuilder().setAsset(289)).setHandle(0).setQty(2).setPrev(0).build());
        }
        Protocol.GetDeckContentsResponse.Builder deckContentsResponse = Protocol.GetDeckContentsResponse.newBuilder();

        for( long id : request.getDeckIdList() ) {
            deckContentsResponse.addDecks(
                    Protocol.DeckContents.newBuilder()
                    .setSuccess(true)
                    .setDeckId(id)
                    .addAllCards(ddl)
            );
        }

      //  Protocol.GetDeckContentsResponse dd = deckContentsResponse.build();

      //  Protocol.Notification n = generateNotification(215,
       //         dd.toByteString(),
       //         dd.getSerializedSize());

     /*   Protocol.Header header = Protocol.Header.newBuilder()
                .setServiceId(4)
                .setMethodId(1)
                .setToken(conn.nextToken())
                .setObjectId(0)
                .setSize(n.getSerializedSize())
                .setStatus(0)
                .build();*/

     //   conn.send(new BattleNetPacket(header, n.toByteArray()));


        /////////////////////

        byte[] bb = Util.hexStringToByteArray("08CFED81FB02120D0A05089E051000100018022800120D0A0508AB041000100018022800120C0A04084D1000100018022800120D0A0508FB041000100018022800120D0A0508A1021000100018022800120D0A0508BB021000100018022800120D0A05089C021000100018022800120D0A0508BF011000100018022800120D0A0508DA0A1000100018022800120D0A0508BF031000100018022800120D0A0508960D1000100018022800120D0A0508F90A1000100018022800120D0A0508D8011000100018022800120D0A0508D90A1000100018022800120D0A0508B40410001000180228001AE801080110CFED81FB021A0D0A05089E0510001000180228001A0D0A0508AB0410001000180228001A0C0A04084D10001000180228001A0D0A0508FB0410001000180228001A0D0A0508A10210001000180228001A0D0A0508BB0210001000180228001A0D0A05089C0210001000180228001A0D0A0508BF0110001000180228001A0D0A0508DA0A10001000180228001A0D0A0508BF0310001000180228001A0D0A0508960D10001000180228001A0D0A0508F90A10001000180228001A0D0A0508D80110001000180228001A0D0A0508D90A10001000180228001A0D0A0508B40410001000180228001AE801080110969787FB021A0D0A0508EB0710001000180228001A0D0A0508DE0410001000180228001A0D0A0508970810001000180228001A0C0A04084310001000180228001A0D0A0508970D10001000180228001A0D0A0508D30210001000180228001A0D0A0508880510001000180228001A0D0A05089D0310001000180228001A0D0A0508810210001000180228001A0D0A0508DA0A10001000180228001A0D0A0508F90A10001000180228001A0D0A0508A40210001000180228001A0D0A0508D80110001000180228001A0D0A0508ED0610001000180228001A0D0A0508D90A1000100018022800");


        Protocol.ClientResponse clResponse = Protocol.ClientResponse.newBuilder()
                .addAttribute(Protocol.Attribute.newBuilder()
                        .setName("id")
                        .setValue(Protocol.Variant.newBuilder()
                                .setIntValue(215)
                        ))
                .addAttribute(Protocol.Attribute.newBuilder()
                        .setName("proto")
                        .setValue(Protocol.Variant.newBuilder()
                                .setBlobValue(ByteString.copyFrom(bb))
                        ))
                .build();

        Protocol.Header header = Processor.generateResponse(clResponse.getSerializedSize(), packet.getHeader().getToken(), 0, 0);
        conn.send(new BattleNetPacket(header, clResponse.toByteArray()));
    }

    @Override
    public int getId() {
        return 214;
    }
}
