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

import com.alterdekim.Protocol;
import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.dbf.CardsDBF;
import com.alterdekim.hearthhack.dbf.DBFField;
import com.alterdekim.hearthhack.util.BattleNetPacket;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

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

@Slf4j
public class CardValues extends GenericParser {

    @Override
    public void parseGenericRequest(int token, TcpConnection conn) throws Exception {
         CardsDBF cards = conn.getDbfConfig().getCards();
         Protocol.CardValues.Builder cardVals = Protocol.CardValues.newBuilder();

         cards.getRecords().forEach(c -> {
             List<DBFField> fields = c.getFields();
             if( !fields.stream().filter(p -> p.getColumn().equals("IS_COLLECTIBLE")).findFirst().get().getVal().equals("True") ) return;
             cardVals.addCards(Protocol.CardValue.newBuilder()
                     .setCard(
                             Protocol.CardDef.newBuilder()
                                     .setAsset(
                                             Integer.parseInt(
                                                     fields.stream()
                                                             .filter(p -> p.getColumn().equals("ID"))
                                                             .findFirst()
                                                             .get()
                                                             .getVal()
                                            )
                                     )
                                     .setPremium(1)
                                     .build()
                     )
                     .setBuy(400)
                     .setSell(100)
                     .setNerfed(false));
             cardVals.addCards(Protocol.CardValue.newBuilder()
                     .setCard(
                             Protocol.CardDef.newBuilder()
                                     .setAsset(
                                             Integer.parseInt(
                                                     fields.stream()
                                                             .filter(p -> p.getColumn().equals("ID"))
                                                             .findFirst()
                                                             .get()
                                                             .getVal()
                                             )
                                     )
                                     .setPremium(0)
                                     .build()
                     )
                     .setBuy(400)
                     .setSell(100)
                     .setNerfed(false));
         });

         Protocol.CardValues vals = cardVals.setCardNerfIndex(5).build();

        Protocol.Notification n = generateNotification(260, vals.toByteString(), vals.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[] b = Util.hexStringToByteArray("[encoded data, 45058 characters]");

     /*   Protocol.Header 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));
    }

    @Override
    public int getId() {
        return CARD_VALUES.getValue();
    }
}
