package de.viper.survivalplus.commands;

import de.viper.survivalplus.SurvivalPlus;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.GameMode;
import org.bukkit.configuration.file.FileConfiguration;

public class GamemodeCommand implements CommandExecutor {
    private final SurvivalPlus plugin;

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

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        FileConfiguration lang = plugin.getLangConfig();
        
        if (!sender.hasPermission("survivalplus.gamemode")) {
            sender.sendMessage(lang.getString("no-permission", "§cDu hast keine Berechtigung für diesen Befehl!"));
            return true;
        }

        if (args.length == 0) {
            sender.sendMessage(lang.getString("gamemode.usage", "§cVerwendung: /gm <0|1|2|3> [spieler]"));
            return true;
        }

        Player target = (args.length > 1) ? plugin.getServer().getPlayer(args[1]) : (sender instanceof Player ? (Player) sender : null);
        
        if (args.length > 1 && !sender.hasPermission("survivalplus.gamemode.others")) {
            sender.sendMessage(lang.getString("no-permission-others", "§cDu hast keine Berechtigung, den Spielmodus anderer zu ändern!"));
            return true;
        }

        if (target == null) {
            sender.sendMessage(lang.getString("player-not-found", "§cSpieler nicht gefunden!"));
            return true;
        }

        GameMode gameMode;
        String modeName;
        switch (args[0].toLowerCase()) {
            case "0":
            case "survival":
                gameMode = GameMode.SURVIVAL;
                modeName = lang.getString("gamemode.survival", "Überleben");
                break;
            case "1":
            case "creative":
                gameMode = GameMode.CREATIVE;
                modeName = lang.getString("gamemode.creative", "Kreativ");
                break;
            case "2":
            case "adventure":
                gameMode = GameMode.ADVENTURE;
                modeName = lang.getString("gamemode.adventure", "Abenteuer");
                break;
            case "3":
            case "spectator":
                gameMode = GameMode.SPECTATOR;
                modeName = lang.getString("gamemode.spectator", "Zuschauer");
                break;
            default:
                sender.sendMessage(lang.getString("invalid-gamemode", "§cUngültiger Spielmodus! Verwende 0, 1, 2 oder 3"));
                return true;
        }

        target.setGameMode(gameMode);
        String message = sender == target ?
            lang.getString("gamemode.changed-self", "§aDein Spielmodus wurde zu %mode% geändert!")
                .replace("%mode%", modeName) :
            lang.getString("gamemode.changed-other", "§aSpielmodus von %player% zu %mode% geändert!")
                .replace("%player%", target.getName())
                .replace("%mode%", modeName);
        
        sender.sendMessage(message);
        return true;
    }
}