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 java.util.List;
import java.util.stream.Collectors;

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

public class Boosters extends GenericParser {

    @Override
    public void parseGenericRequest(int token, TcpConnection conn) throws Exception {
        List<Protocol.BoosterInfo> binfo = conn.getUserService().getBoostersByUserId(conn.getUserId()).stream().map(b -> Protocol.BoosterInfo.newBuilder()
                .setCount(b.getCount())
                .setType(b.getType())
                .build()
        ).collect(Collectors.toList());

        Protocol.BoosterList boosterList = Protocol.BoosterList.newBuilder()
                .addAllList(binfo)
                .build();

        Protocol.Notification n = generateNotification(224, boosterList.toByteString(), boosterList.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 BOOSTERS.getValue();
    }
}
