package me.km.networking;

import me.km.KajetansMod;
import me.km.inventory.ModInventory;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.PacketDistributor;
import net.minecraftforge.fml.network.simple.SimpleChannel;

public class ModPacketHandler {
    private static final String PROTOCOL_VERSION = "1";

    private static final SimpleChannel INSTANCE = NetworkRegistry.ChannelBuilder
            .named(new ResourceLocation(KajetansMod.MODID, "main"))
            .clientAcceptedVersions(PROTOCOL_VERSION::equals)
            .serverAcceptedVersions(PROTOCOL_VERSION::equals)
            .networkProtocolVersion(() -> PROTOCOL_VERSION)
            .simpleChannel();

    public static void init() {
        int id = 0;
        INSTANCE.registerMessage(id++, PlayerDisplay.class, PlayerDisplay::writeBytes, PlayerDisplay::fromBytes, PlayerDisplay::handle);
        INSTANCE.registerMessage(id++, FunctionKey.class, FunctionKey::writeBytes, FunctionKey::fromBytes, FunctionKey::handle);
        INSTANCE.registerMessage(id++, CustomInventory.class, CustomInventory::writeBytes, CustomInventory::fromBytes, CustomInventory::handle);
        INSTANCE.registerMessage(id++, StatusDisplay.class, StatusDisplay::writeBytes, StatusDisplay::fromBytes, StatusDisplay::handle);
        INSTANCE.registerMessage(id++, PlayerHead.class, PlayerHead::writeBytes, PlayerHead::fromBytes, PlayerHead::handle);
        INSTANCE.registerMessage(id++, ItemStackDisplay.class, ItemStackDisplay::writeBytes, ItemStackDisplay::fromBytes, ItemStackDisplay::handle);
        INSTANCE.registerMessage(id++, PlayerLeftClickEmpty.class, PlayerLeftClickEmpty::writeBytes, PlayerLeftClickEmpty::fromBytes, PlayerLeftClickEmpty::handle);
    }

    private static void sendToPlayer(ServerPlayerEntity p, Object msg) {
        INSTANCE.send(PacketDistributor.PLAYER.with(() -> p), msg);
    }

    private static void sendToPlayers(Object msg) {
        INSTANCE.send(PacketDistributor.ALL.noArg(), msg);
    }

    private static void sendToServer(Object msg) {
        INSTANCE.sendToServer(msg);
    }

    public static void sendToDisplay(ServerPlayerEntity p, byte action, byte index, String text) {
        sendToPlayer(p, new PlayerDisplay(action, index, text));
    }

    public static void sendFunctionKey(int key) {
        sendToServer(new FunctionKey(key));
    }

    public static void sendCustomInventory(ServerPlayerEntity p, int id, String title, ModInventory inv) {
        sendToPlayer(p, new CustomInventory(id, title, inv));
    }

    public static void addStatus(ServerPlayerEntity p, byte index, String text) {
        sendToPlayer(p, new StatusDisplay((byte) 1, index, text));
    }

    public static void addTimedStatus(ServerPlayerEntity p, byte index, String text, int time) {
        sendToPlayer(p, new StatusDisplay((byte) 1, index, time, text));
    }

    public static void removeStatus(ServerPlayerEntity p, byte index) {
        sendToPlayer(p, new StatusDisplay((byte) 2, index, ""));
    }

    public static void clearStatus(ServerPlayerEntity p) {
        sendToPlayer(p, new StatusDisplay((byte) 3, (byte) 0, ""));
    }

    public static void sendToHead(ServerPlayerEntity p, byte action, byte index, String name, int x, int y, byte scale) {
        sendToPlayer(p, new PlayerHead(action, index, name, x, y, scale));
    }

    public static void sendLeftClickEmpty() {
        sendToServer(new PlayerLeftClickEmpty());
    }

    //--------------------------------------------------------------------------
    // item stack display
    //--------------------------------------------------------------------------
    public static void setItemStackIndex(ServerPlayerEntity p, byte index, int iconIndex, int count) {
        sendToPlayer(p, new ItemStackDisplay(index, iconIndex, count));
    }

    public static void setItemStackActive(ServerPlayerEntity p, boolean active) {
        sendToPlayer(p, new ItemStackDisplay(active ? (byte) -1 : (byte) -2, -1, -1));
    }

    public static void clearItemStacks(ServerPlayerEntity p) {
        sendToPlayer(p, new ItemStackDisplay((byte) -3, -1, -1));
    }
}
