package top.leisuretimedock.dglabgame.core.network;

import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.simple.SimpleChannel;
import top.leisuretimedock.dglabgame.DGLabGame;
import top.leisuretimedock.dglabgame.core.network.toServer.AcceptUserAgreementPacket;
import top.leisuretimedock.dglabgame.core.network.toServer.UpdateServerConfigPacket;
import top.leisuretimedock.dglabgame.core.network.toServer.UpdateStrengthConfigPacket;

import java.util.concurrent.atomic.AtomicInteger;

public class DLGNetworkHandler {
    public static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel(
            DGLabGame.rl("main"),
            () -> DGLabGame.ModInfo.VERSION,
            DGLabGame.ModInfo.VERSION::equals,
            DGLabGame.ModInfo.VERSION::equals
    );
    static AtomicInteger cid = new AtomicInteger(0);
    public static void register() {
        CHANNEL.messageBuilder(AcceptUserAgreementPacket.class, getIndex(), NetworkDirection.PLAY_TO_SERVER)
                .encoder(AcceptUserAgreementPacket::encode)
                .decoder(AcceptUserAgreementPacket::decode)
                .consumerMainThread(AcceptUserAgreementPacket::handle)
                .add();
        CHANNEL.messageBuilder(UpdateServerConfigPacket.class, getIndex(), NetworkDirection.PLAY_TO_SERVER)
                .encoder(UpdateServerConfigPacket::encode)
                .decoder(UpdateServerConfigPacket::decode)
                .consumerMainThread(UpdateServerConfigPacket::handle)
                .add();
        CHANNEL.messageBuilder(UpdateStrengthConfigPacket.class, getIndex(), NetworkDirection.PLAY_TO_SERVER)
                .encoder(UpdateStrengthConfigPacket::encode)
                .decoder(UpdateStrengthConfigPacket::decode)
                .consumerMainThread(UpdateStrengthConfigPacket::handle)
                .add();

    }
    public static int getIndex() {
        return cid.getAndIncrement();
    }
}
