package com.shanebeestudios.skbee.elements.itemcomponent.expressions;

import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
import com.github.shanebeee.skr.Registration;
import com.shanebeestudios.skbee.api.util.ItemComponentUtils;
import com.shanebeestudios.skbee.api.util.ItemUtils;
import com.shanebeestudios.skbee.config.SkBeeMetrics;
import io.papermc.paper.datacomponent.DataComponentTypes;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@SuppressWarnings("UnstableApiUsage")
public class ExprMaxStackSizeComponent extends SimplePropertyExpression<Object, Number> {

    public static void register(Registration reg) {
        reg.newPropertyExpression(ExprMaxStackSizeComponent.class, Number.class,
                "max stack size component", "itemstacks/itemtypes/slots")
            .name("ItemComponent - Max Stack Size")
            .description("Represents the max stack size of an item.",
                "See [**MaxStackSize**](https://minecraft.wiki/w/Data_component_format#max_stack_size) on McWiki for more details.",
                "",
                "**Changers**:",
                "- `set` = Set the max stack size, must be an integer between 1 and 99.",
                "- `delete` = Will remove the component from the item.",
                "- `reset` = Resets back to default stack size.")
            .examples("set max stack size component of player's tool to 1",
                "reset max stack size component of player's tool")
            .since("3.6.0")
            .register();
    }

    @Override
    public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
        SkBeeMetrics.Features.ITEM_COMPONENTS.used();
        return super.init(expressions, matchedPattern, isDelayed, parseResult);
    }

    @Override
    public @Nullable Number convert(Object itemType) {
        ItemStack itemStack = ItemUtils.getItemStackFromObjects(itemType);
        if (itemStack != null && itemStack.hasData(DataComponentTypes.MAX_STACK_SIZE)) {
            return itemStack.getData(DataComponentTypes.MAX_STACK_SIZE);
        }
        return null;
    }

    @Override
    public Class<?> @Nullable [] acceptChange(ChangeMode mode) {
        if (mode == ChangeMode.RESET || mode == ChangeMode.DELETE) return CollectionUtils.array();
        else if (mode == ChangeMode.SET) return CollectionUtils.array(Number.class);
        return null;
    }

    @Override
    public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
        Integer maxStackSize = delta != null && delta[0] instanceof Number num ? Math.clamp(num.intValue(), 1, 99) : null;

        ItemComponentUtils.modifyComponent(getExpr().getArray(event), mode, DataComponentTypes.MAX_STACK_SIZE, maxStackSize);
    }

    @Override
    protected @NotNull String getPropertyName() {
        return "max stack size component";
    }

    @Override
    public @NotNull Class<? extends Number> getReturnType() {
        return Number.class;
    }

}
