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

import com.alterdekim.Protocol;
import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.dbf.DBFCard;
import com.alterdekim.hearthhack.game.GameTag;
import com.alterdekim.hearthhack.util.BattleNetPacket;
import com.alterdekim.hearthhack.util.ClientRequestBody;
import com.alterdekim.hearthhack.util.Util;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

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

@Slf4j
public class OpenBooster extends ClientRequestParser {
    @Override
    public void parse(BattleNetPacket packet, ClientRequestBody body, TcpConnection conn) throws Exception {
        // 226 BoosterContent
        log.info("BoosterContent: got");
        Protocol.Date d = Protocol.Date.newBuilder()
                .setYear(2023)
                .setMonth(1)
                .setDay(3)
                .setHours(0)
                .setMin(0)
                .setSec(0)
                .build();

        // TODO: optimize this
        List<Protocol.BoosterCard> l = Util.getRandomItems(conn.getDbfConfig().getCardsXML().findEntitiesByTagVal(GameTag.CARD_SET, 3).stream().filter(p -> {
                    Optional<DBFCard> c = conn.getDbfConfig().getCards().findWithMiniGuid(p.getCardId());
                    return c.isPresent() && c.get().findField("IS_COLLECTIBLE").isPresent() && c.get().findField("IS_COLLECTIBLE").get().getVal().equals("True");
                }).collect(Collectors.toList()), 5)
                .stream()
                .map(e -> conn.getDbfConfig().getCards().findWithMiniGuid(e.getCardId()))
                .filter(Optional::isPresent)
                .map(Optional::get)
                .map(c -> Protocol.BoosterCard.newBuilder()
                        .setCardDef(Protocol.CardDef.newBuilder()
                                .setAsset(Integer.parseInt(
                                        c.getFields().stream().filter(p -> p.getColumn().equals("ID")).findFirst().get().getVal()
                                    )
                                )
                                .setPremium(1))
                        .setInsertDate(d).build())
                .collect(Collectors.toList());
        Protocol.BoosterContent response = Protocol.BoosterContent.newBuilder()
                .addAllList(l)
                .build();

      /*  Protocol.BoosterContent response = Protocol.BoosterContent.newBuilder()
                .addList(Protocol.BoosterCard.newBuilder()
                        .setCardDef(Protocol.CardDef.newBuilder()
                                .setAsset(459)
                                .setPremium(1))
                        .setInsertDate(d))
                .addList(Protocol.BoosterCard.newBuilder()
                        .setCardDef(Protocol.CardDef.newBuilder()
                                .setAsset(457)
                                .setPremium(1))
                        .setInsertDate(d))
                .addList(Protocol.BoosterCard.newBuilder()
                        .setCardDef(Protocol.CardDef.newBuilder()
                                .setAsset(777)
                                .setPremium(1))
                        .setInsertDate(d))
                .addList(Protocol.BoosterCard.newBuilder()
                        .setCardDef(Protocol.CardDef.newBuilder()
                                .setAsset(459)
                                .setPremium(1))
                        .setInsertDate(d))
                .addList(Protocol.BoosterCard.newBuilder()
                        .setCardDef(Protocol.CardDef.newBuilder()
                                .setAsset(459)
                                .setPremium(0))
                        .setInsertDate(d))
                .build();*/

        Protocol.Notification n = generateNotification(226, response.toByteString(), response.getSerializedSize());

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

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

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