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

import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.util.Timespan;
import ch.njol.util.Kleenean;
import com.github.shanebeee.skr.Registration;
import com.shanebeestudios.skbee.api.util.Util;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.event.Event;
import org.bukkit.inventory.CookingRecipe;
import org.bukkit.inventory.Recipe;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class ExprRecipeCookTime extends SimpleExpression<Timespan> {

    public static void register(Registration reg) {
        reg.newSimpleExpression(ExprRecipeCookTime.class, Timespan.class,
                "cook[ ]time of recipe[s] [with id[s]] %strings%")
            .name("Recipe - CookTime")
            .description("Get the cooktime of a cooking recipes.")
            .examples("set {_time} to cooktime of recipe with id \"minecraft:cooked_chicken\"")
            .since("2.18.0")
            .register();
    }

    private Expression<String> key;

    @SuppressWarnings("unchecked")
    @Override
    public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
        this.key = (Expression<String>) exprs[0];
        return true;
    }

    @Override
    protected @Nullable Timespan[] get(Event event) {
        List<Timespan> cookTimes = new ArrayList<>();
        for (String key : this.key.getArray(event)) {
            NamespacedKey namespacedKey = Util.getNamespacedKey(key, false);
            if (namespacedKey == null) continue;

            Recipe recipe = Bukkit.getRecipe(namespacedKey);
            if (recipe instanceof CookingRecipe<?> cookingRecipe)
                cookTimes.add(new Timespan(Timespan.TimePeriod.TICK, cookingRecipe.getCookingTime()));

        }
        return cookTimes.toArray(new Timespan[0]);
    }

    @Override
    public boolean isSingle() {
        return this.key.isSingle();
    }

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

    @Override
    public @NotNull String toString(@Nullable Event e, boolean d) {
        return "cook time of recipe " + this.key.toString(e, d);
    }

}
