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

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

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

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

@Slf4j
public class DeckList extends GenericParser {

    @Override
    public void parseGenericRequest(int token, TcpConnection conn) throws Exception {
        log.info("GetDeckList has been triggered!");
        List<Protocol.DeckInfo> l = conn.getUserService().getDecksForUser(conn.getUserId())
                .stream()
                .map(d -> {
                    log.info("DeckId: {}", d.getId());
                    return Protocol.DeckInfo.newBuilder()
                        .setId(d.getId())
                        .setName(d.getName())
                        .setCardBack(conn.getUserService().getCardBackById(d.getCardBack()).getBackId())
                        .setHero(d.getHero())
                        .setDeckType(Protocol.DeckType.forNumber(d.getDeckType()))
                        .setValidity(767L)
                        .setHeroPremium(d.getHeroPremium() ? 1 : 0)
                        .setCardBackOverride(d.getCardBackOverride())
                        .setHeroOverride(d.getHeroOverride())
                        .setLastModified(d.getLastModified())
                        .setSortOrder(d.getSortOrder())
                        .setSourceType(Protocol.DeckSourceType.forNumber(d.getSourceType()))
                        .build(); })
                .collect(Collectors.toList());
        l.add(Protocol.DeckInfo.newBuilder()
                .setId(171593L)
                .setName("precon")
                .setCardBack(0)
                .setHero(637)
                .setDeckType(Protocol.DeckType.PRECON_DECK)
                .setValidity(31L)
                .setHeroPremium(0)
                .setCardBackOverride(false)
                .setHeroOverride(false)
                .setSortOrder(0L)
                .setSourceType(Protocol.DeckSourceType.DECK_SOURCE_TYPE_UNKNOWN).build());
        l.add(Protocol.DeckInfo.newBuilder()
                .setId(172325L)
                .setName("precon")
                .setCardBack(0)
                .setHero(31)
                .setDeckType(Protocol.DeckType.PRECON_DECK)
                .setValidity(31L)
                .setHeroPremium(0)
                .setCardBackOverride(false)
                .setHeroOverride(false)
                .setSortOrder(0L)
                .setSourceType(Protocol.DeckSourceType.DECK_SOURCE_TYPE_UNKNOWN).build());

        Protocol.DeckList deckList = Protocol.DeckList.newBuilder().addAllDecks(l).build();
        Protocol.Notification n = generateNotification(202, deckList.toByteString(), deckList.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()));
    }

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