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

import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import ch.njol.util.coll.CollectionUtils;
import com.github.shanebeee.skr.Registration;
import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ExprNoClip extends SimplePropertyExpression<Entity, Boolean> {

    public static void register(Registration reg) {
        reg.newPropertyExpression(ExprNoClip.class, Boolean.class,
            "no[( |-)](clip|physics) (state|mode)", "entities")
            .name("Entity NoClip")
            .description("Set or get the noClip status of an entity (This will not work on players)")
            .examples("spawn a zombie at player",
                "set no clip state of last spawned zombie to true",
                "set {_var} to no clip state of last spawned sheep",
                "loop all entities in radius 5 around player:",
                "\tset no clip state of loop-entity to true",
                "\tpush loop-entity up with speed 5")
            .since("1.0.2")
            .register();
    }

    @Override
    public @Nullable Boolean convert(Entity entity) {
        return entity.hasNoPhysics();
    }

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

    @Override
    public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
        Boolean changeValue = (Boolean) delta[0];
        if (changeValue == null) return;

        if (mode == ChangeMode.SET) {
            for (Entity entity : getExpr().getArray(event)) {
                entity.setNoPhysics(changeValue);
            }
        }
    }

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

    @Override
    protected @NotNull String getPropertyName() {
        return "no clip state";
    }

}
