package com.shanebeestudios.skbee.elements.damagesource.effects;

import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import com.github.shanebeee.skr.Registration;
import org.bukkit.damage.DamageSource;
import org.bukkit.entity.Damageable;
import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;

public class EffEntityDamageSource extends Effect {

    public static void register(Registration reg) {
        reg.newEffect(EffEntityDamageSource.class,
                "[skbee|le] damage %entities% by %number% (with|using) %damagesource%")
            .name("DamageSource - Damage Entity")
            .description("Damage entities using a damage source.",
                "This has the same functionality as Minecraft's `/damage` command.",
                "Requires MC 1.20.4+")
            .examples("set {_source} to damage source from arrow directly by (random element of all entities)",
                "set {_source} to damage source of dragon breath",
                "set {_source} to damage source of magic",
                "set {_source} to damage source of mob_attack_no_aggro caused by target entity of player",
                "damage player by 100 with {_source}")
            .since("3.3.0")
            .register();
    }

    private Expression<Entity> entities;
    private Expression<Number> amount;
    private Expression<DamageSource> damageSource;

    @SuppressWarnings("unchecked")
    @Override
    public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
        this.entities = (Expression<Entity>) exprs[0];
        this.amount = (Expression<Number>) exprs[1];
        this.damageSource = (Expression<DamageSource>) exprs[2];
        return true;
    }

    @Override
    protected void execute(Event event) {
        DamageSource damageSource = this.damageSource.getSingle(event);
        Number amountNum = this.amount.getSingle(event);
        if (damageSource == null || amountNum == null) {
            return;
        }

        double amount = amountNum.doubleValue();
        for (Entity entity : this.entities.getArray(event)) {
            if (entity instanceof Damageable damageable) {
                damageable.damage(amount, damageSource);
            }
        }

    }

    @Override
    public @NotNull String toString(Event e, boolean d) {
        return "skbee damage " + this.entities.toString(e, d) + " by " + this.amount.toString(e, d) +
            " with " + this.damageSource.toString(e, d);
    }

}
