package de.teleportsuite.commands;

import de.teleportsuite.TeleportSuite;
import org.bukkit.Bukkit;
import org.bukkit.command.*;
import org.bukkit.entity.Player;

public class TpaCommand implements CommandExecutor {
    private final TeleportSuite plugin;
    public TpaCommand(TeleportSuite p) { this.plugin = p; }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (!(sender instanceof Player)) { sender.sendMessage("§cNur für Spieler!"); return true; }
        Player p = (Player) sender;
        if (!p.hasPermission("teleportsuite.tpa")) { p.sendMessage(plugin.getConfigManager().getMessage("no-permission")); return true; }
        if (args.length < 1) { p.sendMessage("§cVerwendung: /tpa <spieler>"); return true; }
        if (p.getName().equalsIgnoreCase(args[0])) { p.sendMessage("§cDu kannst dir nicht selbst eine Anfrage senden."); return true; }
        Player target = Bukkit.getPlayer(args[0]);
        if (target == null) {
            if (plugin.getConfigManager().isBungeeEnabled() && plugin.getBungeeMessenger() != null) {
                plugin.getTeleportManager().sendTpaRequest(p, args[0]);
                return true;
            }
            p.sendMessage(plugin.getConfigManager().getMessage("player-not-found","player",args[0]));
            return true;
        }
        plugin.getTeleportManager().sendTpaRequest(p, target);
        return true;
    }
}
