package de.viper.survivalplus.commands;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.Openable;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import de.viper.survivalplus.SurvivalPlus;

public class CloseDoorsCommand implements CommandExecutor {

    private final SurvivalPlus plugin;

    public CloseDoorsCommand(SurvivalPlus plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player player)) {
            sender.sendMessage(plugin.getLangConfig().getString("commands.closedoors.noplayer", "Nur Spieler können diesen Befehl nutzen!"));
            return true;
        }

        if (!player.hasPermission("survivalplus.closedoors")) {
            player.sendMessage(plugin.getLangConfig().getString("commands.closedoors.nopermission", "Dafür hast du keine Rechte!"));
            return true;
        }

        if (args.length != 1) {
            player.sendMessage(plugin.getLangConfig().getString("commands.closedoors.usage", "Benutzung: /closedoors <radius>"));
            return true;
        }

        int radius;
        try {
            radius = Integer.parseInt(args[0]);
            if (radius <= 0) {
                player.sendMessage(plugin.getLangConfig().getString("commands.closedoors.invalidradius", "Der Radius muss größer als 0 sein!"));
                return true;
            }
        } catch (NumberFormatException e) {
            player.sendMessage(plugin.getLangConfig().getString("commands.closedoors.invalidradius", "Der Radius muss eine Zahl sein!"));
            return true;
        }

        int closedCount = 0;
        var center = player.getLocation().getBlock();

        for (int x = -radius; x <= radius; x++) {
            for (int y = -radius; y <= radius; y++) {
                for (int z = -radius; z <= radius; z++) {
                    Block block = center.getRelative(x, y, z);
                    Material type = block.getType();

                    if (type == Material.OAK_DOOR ||
                        type == Material.SPRUCE_DOOR ||
                        type == Material.BIRCH_DOOR ||
                        type == Material.JUNGLE_DOOR ||
                        type == Material.ACACIA_DOOR ||
                        type == Material.DARK_OAK_DOOR ||
                        type == Material.CRIMSON_DOOR ||
                        type == Material.WARPED_DOOR ||
                        type == Material.IRON_DOOR) {

                        var blockData = block.getBlockData();
                        if (blockData instanceof Openable openable) {
                            if (openable.isOpen()) {
                                openable.setOpen(false);
                                block.setBlockData(openable);
                                closedCount++;
                            }
                        }
                    }
                }
            }
        }

        player.sendMessage(plugin.getLangConfig()
            .getString("commands.closedoors.success", "Es wurden %count% Türen geschlossen.")
            .replace("%count%", String.valueOf(closedCount)));

        return true;
    }
}
