package me.km.networking;

import java.util.function.Supplier;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent;

public class ItemStackDisplay {
    private byte index;
    private int iconIndex;
    private int count;

    public ItemStackDisplay() {
        index = -1;
        iconIndex = -1;
        count = -1;
    }

    public ItemStackDisplay(byte index, int iconIndex, int count) {
        this.index = index;
        this.iconIndex = iconIndex;
        this.count = count;
    }

    public static void writeBytes(ItemStackDisplay isd, PacketBuffer buf) {
        buf.writeByte(isd.index);
        buf.writeInt(isd.iconIndex);
        buf.writeInt(isd.count);
    }

    public static ItemStackDisplay fromBytes(PacketBuffer buf) {
        ItemStackDisplay isd = new ItemStackDisplay();
        isd.index = buf.readByte();
        isd.iconIndex = buf.readInt();
        isd.count = buf.readInt();
        return isd;
    }

    public static void handle(ItemStackDisplay isd, Supplier<NetworkEvent.Context> context) {
        context.get().enqueueWork(() -> {
            switch(isd.index) {
                case -1:
                    ItemStackDisplayGui.INSTANCE.setActive(true);
                    break;
                case -2:
                    ItemStackDisplayGui.INSTANCE.setActive(false);
                    break;
                case -3:
                    ItemStackDisplayGui.INSTANCE.clear();
                    break;
                default:
                    ItemStackDisplayGui.INSTANCE.setIcon(isd.index, isd.iconIndex, isd.count);
                    break;
            }
        });
        context.get().setPacketHandled(true);
    }
}
