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

import com.alterdekim.Protocol;
import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.entity.CardBack;
import com.alterdekim.hearthhack.util.BattleNetPacket;

import java.util.stream.Collectors;

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

public class CardBacks extends GenericParser {

    @Override
    public void parseGenericRequest(int token, TcpConnection conn) throws Exception {
        Protocol.CardBacks cardBacks = Protocol.CardBacks.newBuilder()
                .setDefaultCardBack(conn.getUserService().getDefaultCardBackForUserId(conn.getUserId()))
                .addAllCardBacks(conn.getUserService().getCardBacksForUserId(conn.getUserId()).stream()
                        .map(CardBack::getBackId).collect(Collectors.toList()))
                .build();

        Protocol.Notification n = generateNotification(236, cardBacks.toByteString(), cardBacks.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 CARD_BACKS.getValue();
    }
}
