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

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

import java.util.stream.IntStream;

@Slf4j
public class DeckUpdate extends ClientRequestParser {
    @Override
    public void parse(BattleNetPacket packet, ClientRequestBody body, TcpConnection conn) throws Exception {
        // 222 DeckSetData
        Protocol.DeckSetData data = Protocol.DeckSetData.parseFrom(body.getBody());

        data.getCardsList().forEach(c -> {
            IntStream.range(0, c.getQty()).forEach(i -> conn.getUserService().addCardToDeck(conn.getUserId(), data.getDeck(), (long) c.getDef().getAsset(), c.getDef().hasPremium() && c.getDef().getPremium() != 0));
        });
        if( data.hasCardBack() ) {
            conn.getUserService().updateDeckCardBack(conn.getUserId(), data.getDeck(), data.getCardBack());
        }
        if( data.hasHero() ) {
            conn.getUserService().updateDeckHero(conn.getUserId(), data.getDeck(), data.getHero().getAsset(), data.getHero().getPremium() != 0);
        }
    }

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