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

import com.alterdekim.Protocol;
import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.config.DBFConfig;
import com.alterdekim.hearthhack.config.ServerConfig;
import com.alterdekim.hearthhack.service.IService;
import com.alterdekim.hearthhack.service.UserService;
import com.alterdekim.hearthhack.util.BattleNetPacket;
import com.alterdekim.hearthhack.util.GetAccountInfoRequest;

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

public class GoldBalance extends GenericParser {

    private DBFConfig config;
    private UserService userService;

    @Override
    public void setResources(IService service, DBFConfig config) {
        this.config = config;
        this.userService = (UserService) service;
    }

    @Override
    public void parseGenericRequest(int token, TcpConnection conn) throws Exception {
        Protocol.GoldBalance gb = Protocol.GoldBalance.newBuilder()
                .setCap(999999)
                .setBonusBalance(0)
                .setCappedBalance(userService.getGoldForUserId(conn.getUserId()))
                .setCapWarning(2000)
                .build();

        byte[] b = generateNotification(278, gb.toByteString(), gb.getSerializedSize()).toByteArray();

        Protocol.Header header = Protocol.Header.newBuilder()
                .setServiceId(4)
                .setMethodId(1)
                .setToken(conn.nextToken())
                .setObjectId(0)
                .setSize(b.length)
                .setStatus(0)
                .build();

        conn.send(new BattleNetPacket(header, b));
    }

    @Override
    public int getId() {
        return GOLD_BALANCE.getValue();
    }
}
