package me.km.networking;

import me.km.KajetansMod;
import me.km.entities.EntityHuman;
import me.km.inventory.InventoryBase;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import net.minecraftforge.fml.relauncher.Side;

public class ModPacketHandler 
{
    public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(KajetansMod.MODID);
    
    private static int id = 0;
    
    public static void init()
    {
        INSTANCE.registerMessage(PlayerDisplay.Handler.class, PlayerDisplay.class, id++, Side.CLIENT);
        INSTANCE.registerMessage(FunctionKey.Handler.class, FunctionKey.class, id++, Side.SERVER);
        INSTANCE.registerMessage(HumanUpdate.Handler.class, HumanUpdate.class, id++, Side.CLIENT);
        INSTANCE.registerMessage(HumanScaleUpdate.Handler.class, HumanScaleUpdate.class, id++, Side.CLIENT);
        INSTANCE.registerMessage(CustomInventory.Handler.class, CustomInventory.class, id++, Side.CLIENT);
        INSTANCE.registerMessage(CampFireInventory.Handler.class, CampFireInventory.class, id++, Side.CLIENT);
        INSTANCE.registerMessage(SeasonUpdate.Handler.class, SeasonUpdate.class, id++, Side.CLIENT);
        INSTANCE.registerMessage(StatusDisplay.Handler.class, StatusDisplay.class, id++, Side.CLIENT);
    }
    
    public static void sendToDisplay(EntityPlayerMP p, byte action, byte index, String text)
    {
        INSTANCE.sendTo(new PlayerDisplay(action, index, text), p);
    }
    
    public static void sendFunctionKey(int key)
    {
        INSTANCE.sendToServer(new FunctionKey(key));
    }
    
    public static void sendHumanUpdate(EntityPlayerMP p, EntityHuman human)
    {
        INSTANCE.sendTo(new HumanUpdate(human), p);
    }
    
    public static void sendHumanUpdate(EntityHuman human)
    {
        HumanUpdate update = new HumanUpdate(human);
        KajetansMod.server.getPlayerList().getPlayers().forEach(p -> INSTANCE.sendTo(update, p));
    }
    
    public static void sendHumanScaleUpdate(EntityHuman human)
    {
        HumanScaleUpdate update = new HumanScaleUpdate(human);
        KajetansMod.server.getPlayerList().getPlayers().forEach(p -> INSTANCE.sendTo(update, p));
    }
    
    public static void sendCustomInventory(EntityPlayerMP p, int id, InventoryBase inv)
    {
        INSTANCE.sendTo(new CustomInventory(id, inv), p);
    }
    
    public static void sendCampFireInventory(EntityPlayerMP p, int id, String name)
    {
        INSTANCE.sendTo(new CampFireInventory(id, name), p);
    }
    
    public static void sendSeasonUpdate(EntityPlayerMP p, byte season)
    {
        INSTANCE.sendTo(new SeasonUpdate(season), p);
    }
    
    public static void addStatus(EntityPlayerMP p, byte index, String text)
    {
        INSTANCE.sendTo(new StatusDisplay((byte) 1, index, text), p);
    }
    
    public static void addTimedStatus(EntityPlayerMP p, byte index, String text, int time)
    {
        INSTANCE.sendTo(new StatusDisplay((byte) 1, index, time, text), p);
    }
    
    public static void removeStatus(EntityPlayerMP p, byte index)
    {
        INSTANCE.sendTo(new StatusDisplay((byte) 2, index, ""), p);
    }
    
    public static void clearStatus(EntityPlayerMP p)
    {
        INSTANCE.sendTo(new StatusDisplay((byte) 3, (byte) 0, ""), p);
    }
}