package me.km.snuviscript.commands;

import me.hammerle.snuviscript.code.ScriptManager;
import me.km.networking.ModPacketHandler;
import me.km.permissions.Permissions;
import me.km.snuviscript.Scripts;
import static me.km.snuviscript.commands.CommandUtils.doForGroup;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.server.MinecraftServer;

public class ItemStackDisplayCommands {
    public static void registerFunctions(ScriptManager sm, Scripts scripts, Permissions perms, MinecraftServer server) {
        sm.registerConsumer("stacks.set", (sc, in) -> {
            byte index = in[1].getByte(sc);
            if(index < 0 || index >= 9) {
                throw new IllegalArgumentException("index must be beetween 0 and 8, given: " + index);
            }
            int iconIndex = in[2].getInt(sc);
            int count = in[3].getInt(sc);
            doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.setItemStackIndex((ServerPlayerEntity) p, index, iconIndex, count));
        });
        sm.registerConsumer("stacks.clearindex", (sc, in) -> {
            byte index = in[1].getByte(sc);
            if(index < 0 || index >= 9) {
                throw new IllegalArgumentException("index must be beetween 0 and 8, given: " + index);
            }
            doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.setItemStackIndex((ServerPlayerEntity) p, index, -1, 0));
        });
        sm.registerConsumer("stacks.clear", (sc, in) -> {
            doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.clearItemStacks((ServerPlayerEntity) p));
        });
        sm.registerConsumer("stacks.setactive", (sc, in) -> {
            boolean active = in[1].getBoolean(sc);
            doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.setItemStackActive((ServerPlayerEntity) p, active));
        });
    }
}
