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);
            }
            String name = in[2].getString(sc);
            doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.setItemStackIndex((ServerPlayerEntity) p, index, name));
        });
        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, ""));
        });
        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));
        });
    }
}
