package moe.oko.opennaw.command;

import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.BlockVector3;
import moe.oko.opennaw.OpenNAW;
import moe.oko.opennaw.model.Gate;
import moe.oko.opennaw.util.CommandHelper;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.block.data.type.Sign;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class GateCommand implements TabExecutor {
    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(CommandHelper.NOT_PLAYER);
            return true;
        }

        if (args.length < 1) { return false; }
        var player = (Player) sender;

        switch (args[0]) {
            case "create" -> {
                if(args.length < 2) {
                    player.sendMessage(ChatColor.RED + "Specify a name for the new gate.");
                    return true;
                }
                var newGate = new Gate(args[1]);
                OpenNAW.getInstance().getGateHandler().getGates().put(newGate.getUuid(), newGate);
                player.sendMessage(ChatColor.GREEN + "Successfully created a new gate with the name " + args[1]);
                return true;
            }
            case "delete" -> {
                if(args.length < 2) {
                    player.sendMessage(ChatColor.RED + "Specify a name for the gate.");
                    return true;
                }
                var uid = OpenNAW.getInstance().getGateHandler().getGateUidByName(args[1]);
                if(uid != null) {
                    OpenNAW.getInstance().getGateHandler().getGates().remove(uid);
                } else {
                    player.sendMessage(ChatColor.RED + "That gate is invalid.");
                }
                return true;
            }
            case "modify" -> {
                if(args.length < 2) {
                    player.sendMessage(ChatColor.RED + "Specify a name for the gate.");
                    return true;
                }
                var gate = OpenNAW.getInstance().getGateHandler().getGateByName(args[1]);
                if(gate == null) {
                    player.sendMessage(ChatColor.RED + "That gate is invalid.");
                    return true;
                }
                try {
                    var sel = WorldEdit.getInstance().getSessionManager().get(BukkitAdapter.adapt(player)).getSelection();
                    //clear old blocks.
                    gate.getGateBlocks().clear();
                    int i = 0;
                    for(BlockVector3 vec : sel) {
                        gate.registerGateBlock(new Location(BukkitAdapter.adapt(sel.getWorld()), vec.getBlockX(), vec.getBlockY(), vec.getBlockZ()).getBlock());
                        i++;
                    }
                    player.sendMessage(ChatColor.GREEN + "Successfully set the new gate area to the " + i + " blocks selected.");
                    return true;
                } catch (Exception e) {
                    player.sendMessage(ChatColor.RED + "Make sure you have selected a region before defining the gate.");
                    return true;
                }
            }
            case "open" -> {
                if(args.length < 2) {
                    player.sendMessage(ChatColor.RED + "Specify a name for the gate.");
                    return true;
                }
                var gate = OpenNAW.getInstance().getGateHandler().getGateByName(args[1]);
                if(gate == null) {
                    player.sendMessage(ChatColor.RED + "That gate is invalid.");
                    return true;
                }
                gate.open();
                player.sendMessage(ChatColor.GREEN + "Done.");
                return true;
            }
            case "close" -> {
                if(args.length < 2) {
                    player.sendMessage(ChatColor.RED + "Specify a name for the gate.");
                    return true;
                }
                var gate = OpenNAW.getInstance().getGateHandler().getGateByName(args[1]);
                if(gate == null) {
                    player.sendMessage(ChatColor.RED + "That gate is invalid.");
                    return true;
                }
                gate.close();
                player.sendMessage(ChatColor.GREEN + "Done.");
                return true;
            }
            case "addsign" -> {
                if(args.length < 2) {
                    player.sendMessage(ChatColor.RED + "Specify a name for the gate.");
                    return true;
                }
                var gate = OpenNAW.getInstance().getGateHandler().getGateByName(args[1]);
                if(gate == null) {
                    player.sendMessage(ChatColor.RED + "That gate is invalid.");
                    return true;
                }
                var block = player.getTargetBlockExact(25);
                if(!(block.getType().name().contains("_SIGN"))) {
                    player.sendMessage(ChatColor.RED + "You aren't looking at a sign block...");
                    return true;
                }
                gate.registerSign(block);
                player.sendMessage(ChatColor.GREEN + "Successfully registered " + block.getLocation() + " as a sign block for gate " + gate.getName());
                return true;
            }
            case "delsign" -> {
                if(args.length < 2) {
                    player.sendMessage(ChatColor.RED + "Specify a name for the gate.");
                    return true;
                }
                var gate = OpenNAW.getInstance().getGateHandler().getGateByName(args[1]);
                if(gate == null) {
                    player.sendMessage(ChatColor.RED + "That gate is invalid.");
                    return true;
                }
                var block = player.getTargetBlockExact(25);
                if(!(block.getBlockData() instanceof Sign)) {
                    player.sendMessage(ChatColor.RED + "You aren't looking at a sign block...");
                    return true;
                }
                if(gate.isGateSign(block.getLocation())) {
                    gate.removeSignBlock(block);
                    player.sendMessage(ChatColor.GREEN + "Successfully removed " + block.getLocation() + " as a sign block for gate " + gate.getName());
                } else {
                    player.sendMessage(ChatColor.RED + "That sign is not a registered sign block...?");
                }
                return true;
            }
        }
        return true;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        return switch (args.length) {
            case 1 -> List.of("create", "delete", "modify", "open", "close", "addsign", "delsign");
            case 2 -> OpenNAW.getInstance().getGateHandler().getGateNames();
            default -> null;
        };
    }
}
