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.COLLECTION;


@Slf4j
public class Collection extends GenericParser {

    private final Protocol.Date d = Protocol.Date.newBuilder()
            .setYear(2023)
            .setMonth(1)
            .setDay(3)
            .setHours(0)
            .setMin(0)
            .setSec(0)
            .build();

    @Override
    public void parseGenericRequest(int token, TcpConnection conn) throws Exception {
        List<Protocol.CardStack> l = conn.getUserService().getCollectionOfUser(conn.getUserId())
                    .stream()
                    .map(p -> Protocol.CardStack.newBuilder()
                            .setCount(p.getCount())
                            .setLatestInsertDate(d)
                            .setNumSeen(p.getHasSeen() ? 1 : 0)
                            .setCardDef(Protocol.CardDef.newBuilder()
                                    .setPremium(p.getIsPremium() ? 1 : 0)
                                    .setAsset(p.getAssetId().intValue()))
                            .build())
                    .collect(Collectors.toList());

         Protocol.Collection collection = Protocol.Collection.newBuilder()
                 .addAllStacks(l)
                 .build();

        Protocol.Notification n = generateNotification(207, collection.toByteString(), collection.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 COLLECTION.getValue();
    }
}
