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

import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.util.SkriptColor;
import ch.njol.util.Kleenean;
import com.github.shanebeee.skr.Registration;
import com.shanebeestudios.skbee.api.wrapper.ComponentWrapper;
import org.bukkit.DyeColor;
import org.bukkit.block.Block;
import org.bukkit.block.sign.Side;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class EffSendSignChange extends Effect {

    public static void register(Registration reg) {
        reg.newEffect(EffSendSignChange.class,
                "make %players% see [front|:back] sign lines (at|of) %blocks% as %textcomps% [with color %-color%] [glowing:and glowing]")
            .name("TextComponent - Sign Change")
            .description("Sends a sign change to the player. You CAN send a block change first.",
                "Side = Whether to show the front/back change (defaults to front).",
                "Color = the color of the text on the sign.",
                "Glowing = make the text glow.")
            .examples("set {_m::1} to mini message from \"<rainbow>OOOOOOOO\"",
                "set {_m::2} to text component from \"Le-Text\"",
                "make player see sign lines of target block as {_m::*} with color blue")
            .since("2.6.0")
            .register();
    }

    private Expression<Player> players;
    private Expression<Block> blocks;
    private Expression<ComponentWrapper> components;
    private Expression<SkriptColor> color;
    private boolean glowing;
    private Side signSide;

    @SuppressWarnings({"unchecked"})
    @Override
    public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
        this.players = (Expression<Player>) exprs[0];
        this.blocks = (Expression<Block>) exprs[1];
        this.components = (Expression<ComponentWrapper>) exprs[2];
        this.color = (Expression<SkriptColor>) exprs[3];
        this.glowing = parseResult.hasTag("glowing");
        this.signSide = parseResult.hasTag("back") ? Side.BACK : Side.FRONT;
        return true;
    }

    @Override
    protected void execute(Event event) {
        ComponentWrapper[] components = this.components.getArray(event);
        DyeColor dyeColor = null;
        if (this.color != null) {
            SkriptColor skriptColor = this.color.getSingle(event);
            if (skriptColor != null) {
                dyeColor = skriptColor.asDyeColor();
            }
        }

        for (Block block : this.blocks.getArray(event)) {
            for (Player player : this.players.getArray(event)) {
                ComponentWrapper.sendSignChange(player, block.getLocation(), components, this.signSide, dyeColor, this.glowing);
            }
        }
    }

    @Override
    public @NotNull String toString(@Nullable Event e, boolean d) {
        String color = this.color != null ? this.color.toString(e, d) : "";
        String glow = this.glowing ? " and glowing" : "";
        return "make " + this.players.toString(e, d) + " see sign lines of " + this.blocks.toString(e, d)
            + " as " + this.components.toString(e, d) + color + glow;
    }

}
