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;

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

@Slf4j
public class BuySellCard extends ClientRequestParser {
    @Override
    public void parse(BattleNetPacket packet, ClientRequestBody body, TcpConnection conn) throws Exception {
        Protocol.BuySellCard request = Protocol.BuySellCard.parseFrom(body.getBody());
        int count = request.hasCount() ? request.getCount() : 1;
        if( request.getBuying() ) {
            if( conn.getUserService().getDustForUserId(conn.getUserId()) < request.getUnitBuyPrice()*count ) {
                sendResponse(Protocol.BoughtSoldCard.newBuilder()
                        .setAmount(0)
                        .setResult(Protocol.BoughtSoldCard.Result.WRONG_BUY_PRICE)
                        .setDef(request.getDef())
                        .build(), conn);
                return;
            }
            IntStream.range(0, count).forEach(i -> conn.getUserService().addCardToCollection(conn.getUserId(), (long) request.getDef().getAsset(), request.getDef().getPremium() != 0, true));
            conn.getUserService().setDustForUserId(conn.getUserId(), conn.getUserService().getDustForUserId(conn.getUserId())-(request.getUnitBuyPrice()*count));
            sendResponse(Protocol.BoughtSoldCard.newBuilder()
                    .setAmount(request.getUnitBuyPrice()*count)
                    .setCount(count)
                    .setResult(Protocol.BoughtSoldCard.Result.BOUGHT)
                    .setDef(request.getDef())
                    .build(), conn);
            return;
        }
        conn.getUserService().removeCardToCollection(conn.getUserId(), (long) request.getDef().getAsset(), request.getDef().getPremium() != 0);
        conn.getUserService().setDustForUserId(conn.getUserId(), conn.getUserService().getDustForUserId(conn.getUserId())+(request.getUnitSellPrice()*count));
        sendResponse(Protocol.BoughtSoldCard.newBuilder()
                .setAmount(request.getUnitSellPrice()*count)
                .setCount(count)
                .setResult(Protocol.BoughtSoldCard.Result.SOLD)
                .setDef(request.getDef())
                .build(), conn);
    }

    private void sendResponse(Protocol.BoughtSoldCard response, TcpConnection conn) throws Exception {
        Protocol.Notification n = generateNotification(258, response.toByteString(), response.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 257;
    }
}
