package com.alterdekim.hearthhack.component.processor;

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

@Slf4j
public class FriendsProcessor extends Processor {

    public FriendsProcessor() {
        this.setProcessorId(6);
    }

    private void processSubscribeToFriends(BattleNetPacket packet, TcpConnection conn) throws Exception {
        //Protocol.SubscribeToFriendsRequest subscribeToFriendsRequest = Protocol.SubscribeToFriendsRequest.parseFrom(packet.getBody());
        Protocol.SubscribeToFriendsResponse subscribeToFriendsResponse = Protocol.SubscribeToFriendsResponse.newBuilder()
                .setMaxFriends(200)
                .setMaxReceivedInvitations(200)
                .setMaxSentInvitations(200)
                .addRole(Protocol.Role.newBuilder()
                        .setId(1)
                        .setName("battle_tag_friend")
                        .build())
                .addRole(Protocol.Role.newBuilder()
                        .setId(2)
                        .setName("real_id_friend")
                        .build())
                .build();
        Protocol.Header header = Processor.generateResponse(subscribeToFriendsResponse.getSerializedSize(), packet.getHeader().getToken(), 0, 0);
        conn.send(new BattleNetPacket(header, subscribeToFriendsResponse.toByteArray()));
    }

    @Override
    public void process(BattleNetPacket packet, TcpConnection conn) throws Exception {
        if (packet.getHeader().getMethodId() == 1) {
            processSubscribeToFriends(packet, conn);
            return;
        }
        log.error("Can't process weird FriendsProcessor method: {}", packet.getHeader().getMethodId());
    }

    @Override
    public String getExportName() {
        return "bnet.protocol.friends.FriendsNotify";
    }
}