package com.leisuretimedock.blasttravelreborn.network;

import com.leisuretimedock.blasttravelreborn.BlastTravelReborn;
import com.leisuretimedock.blasttravelreborn.network.toClient.FireCannonPayload;
import com.leisuretimedock.blasttravelreborn.network.toClient.StopCannonFlightClientPayload;
import com.leisuretimedock.blasttravelreborn.network.toClient.SyncFlightStatePayload;
import com.leisuretimedock.blasttravelreborn.network.toServer.RequestFirePayload;
import com.leisuretimedock.blasttravelreborn.network.toServer.StopCannonFlightServerPayload;
import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.simple.SimpleChannel;

public class BTRNetwork {
    private static final String PROTOCOL_VERSION = "1";
    private static int ID = -1;
    public static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel(
            BlastTravelReborn.id("network"),
            () -> PROTOCOL_VERSION,
            PROTOCOL_VERSION::equals,
            PROTOCOL_VERSION::equals
    );
    public static void register() {
        CHANNEL
                .messageBuilder(FireCannonPayload.class, getIDAndIncrease(), NetworkDirection.PLAY_TO_CLIENT)
                .encoder(FireCannonPayload::write)
                .decoder(FireCannonPayload::read)
                .consumerNetworkThread(FireCannonPayload::handler)
                .add();
        CHANNEL
                .messageBuilder(StopCannonFlightServerPayload.class, getIDAndIncrease(), NetworkDirection.PLAY_TO_SERVER)
                .encoder(StopCannonFlightServerPayload::write)
                .decoder(StopCannonFlightServerPayload::read)
                .consumerNetworkThread(StopCannonFlightServerPayload::handler)
                .add();
        CHANNEL
                .messageBuilder(StopCannonFlightClientPayload.class, getIDAndIncrease(), NetworkDirection.PLAY_TO_CLIENT)
                .encoder(StopCannonFlightClientPayload::write)
                .decoder(StopCannonFlightClientPayload::read)
                .consumerNetworkThread(StopCannonFlightClientPayload::handle)
                .add();
        CHANNEL
                .messageBuilder(RequestFirePayload.class, getIDAndIncrease(), NetworkDirection.PLAY_TO_SERVER)
                .encoder(RequestFirePayload::write)
                .decoder(RequestFirePayload::read)
                .consumerNetworkThread(RequestFirePayload::handle)
                .add();
        CHANNEL
                .messageBuilder(SyncFlightStatePayload.class, getIDAndIncrease(), NetworkDirection.PLAY_TO_CLIENT)
                .encoder(SyncFlightStatePayload::write)
                .decoder(SyncFlightStatePayload::read)
                .consumerNetworkThread(SyncFlightStatePayload::handle)
                .add();

    }
    private static int getIDAndIncrease() {
        return ++ID;
    }
}
