package com.alterdekim.hearthhack.component.processor;


import com.alterdekim.Protocol;
import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.util.BattleNetPacket;
import com.alterdekim.hearthhack.util.Util;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.Set;

@Slf4j
public class ConnectionProcessor extends Processor {

    public ConnectionProcessor() {
        this.setProcessorId(0);
    }

    /*int GameUtilityProcessor = Compute32.Hash("bnet.protocol.game_utilities.GameUtilities");
    int GameMasterProcessor = Compute32.Hash("bnet.protocol.game_master.GameMaster");
    int NotificationProcessor = Compute32.Hash("bnet.protocol.notification.NotificationProcessor");

    int ChannelOwnerProcessor = Compute32.Hash("bnet.protocol.channel.ChannelOwner");

    int ChannelInvitationProcessor = Compute32.Hash("bnet.protocol.channel_invitation.ChannelInvitationProcessor");

    int ChallengeProcessor = Compute32.Hash("bnet.protocol.challenge.ChallengeProcessor");
    int ResourcesProcessor = Compute32.Hash("bnet.protocol.resources.Resources"); */

    private void processConnect( BattleNetPacket packet, TcpConnection conn ) throws Exception {
        Protocol.ConnectRequest cr = Protocol.ConnectRequest.parseFrom(packet.getBody());
        Protocol.BindResponse.Builder b = Protocol.BindResponse.newBuilder();
        List<Integer> hashes = cr.getBindRequest().getImportedServiceHashList();
        for( Integer hash : hashes ) {
           b.addImportedServiceId(hash);
        }
        List<Protocol.BoundService> bs = cr.getBindRequest().getExportedServiceList();
        for( Protocol.BoundService s : bs ) {
            Set<Integer> ks = conn.getProcessors().keySet();
            for( Integer c : ks ) {
                Processor ss = conn.getProcessors().get(c);
                if( ss != null && ss.getProcessorHash() == s.getHash() ) {
                    ss.setProcessorId(s.getId());
                    break;
                }
            }
        }

        conn.sendRaw(Util.hexStringToByteArray("000c08fe011800200028950130000a0c08f4c9ccf30d10c687bcb805120a0889ff5c1092e5a1b9051800220e0a0c0109080a04030507060211102a2e0a2c0d55450000157a72746d1a206ff4fdd5fa5f6d62a278a04403e075d69d734cd4880732dce8edc6a3f528089230a08a95f4cebdcc02422e0a2c0d55450000157a72746d1a20b4bd0f0096a7648de1d19042fb7a79b96c0df48eea3488f498b08ad3d38fb2cc"));
    }

    @Override
    public void process(BattleNetPacket packet, TcpConnection conn) throws Exception {
        switch (packet.getHeader().getMethodId()) {
            case 1:
                processConnect(packet, conn);
                break;
            case 5:
                // FUCK NODATA
                break;
            default:
                log.error("Can't process weird ConnectionProcessor method: " + packet.getHeader().getMethodId());
                break;
        }
    }

    @Override
    public String getExportName() {
        return super.getExportName();
    }
}
