package de.viper.survivalplus.commands;

import de.viper.survivalplus.SurvivalPlus;
import de.viper.survivalplus.listeners.SitListener;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.configuration.file.FileConfiguration;

public class SitCommand implements CommandExecutor {
    private final SurvivalPlus plugin;
    private final SitListener sitListener;

    public SitCommand(SurvivalPlus plugin, SitListener sitListener) {
        this.plugin = plugin;
        this.sitListener = sitListener;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(plugin.getLangConfig().getString("player-only", "§cDieser Befehl ist nur für Spieler!"));
            return true;
        }

        Player player = (Player) sender;
        FileConfiguration lang = plugin.getLangConfig();

        if (!player.hasPermission("survivalplus.sit")) {
            player.sendMessage(lang.getString("no-permission", "§cDu hast keine Berechtigung für diesen Befehl!"));
            return true;
        }

        if (args.length != 0) {
            player.sendMessage(lang.getString("sit.usage", "§cVerwendung: /sit"));
            return true;
        }

        // --- ÄNDERUNG: Wenn man schon sitzt, tue nichts ---
        if (sitListener.isSitting(player)) {
            player.sendMessage(ChatColor.RED + "Du sitzt bereits!");
            return true;
        }
        // --------------------------------------------------

        if (player.isInsideVehicle()) {
            player.sendMessage(lang.getString("sit.already-in-vehicle", "§cDu kannst dich nicht setzen, während du in einem Fahrzeug bist!"));
            return true;
        }

        // Prüfe den Boden unter dem Spieler
        Location loc = player.getLocation();
        Block blockBelow = loc.clone().subtract(0, 1, 0).getBlock();
        
        if (blockBelow.getType().isAir() || !blockBelow.getType().isSolid()) {
            player.sendMessage(plugin.getLangConfig().getString("sit.no-solid-ground", "§cDu kannst dich hier nicht hinsetzen (kein fester Boden)."));
            return true;
        }

        // Position vorbereiten
        Location location = player.getLocation();
        location.setX(location.getBlockX() + 0.5);
        location.setZ(location.getBlockZ() + 0.5);

        // Höhe dynamisch berechnen
        double y = location.getBlockY();
        
        if (isStair(blockBelow.getType()) || isSlab(blockBelow.getType())) {
            y += 0.5;
        } 
        
        location.setY(y);

        sitListener.sitPlayer(player, location);
        return true;
    }

    private boolean isStair(Material material) {
        return material.name().endsWith("_STAIRS");
    }

    private boolean isSlab(Material material) {
        return material.name().endsWith("_SLAB") || material.name().endsWith("_STEP") || material.name().equals("PRISMARINE_SLAB");
    }
}