package de.teleportsuite.commands;

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

public class TpCommand implements CommandExecutor {
    private final TeleportSuite plugin;
    public TpCommand(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.tp")) { p.sendMessage(plugin.getConfigManager().getMessage("no-permission")); return true; }
        if (args.length < 1) { p.sendMessage("§cVerwendung: /tp <spieler> [spieler2]"); return true; }

        if (args.length == 1) {
            Player target = Bukkit.getPlayer(args[0]);
            if (target == null) { p.sendMessage(plugin.getConfigManager().getMessage("player-not-found","player",args[0])); return true; }
            plugin.getTeleportManager().teleport(p, new TeleportLocation(target.getLocation(), plugin.getConfigManager().getServerName()));
        } else {
            Player from = Bukkit.getPlayer(args[0]);
            Player to   = Bukkit.getPlayer(args[1]);
            if (from == null || to == null) { p.sendMessage("§cEin Spieler nicht gefunden."); return true; }
            if (!p.hasPermission("teleportsuite.admin")) { p.sendMessage(plugin.getConfigManager().getMessage("no-permission")); return true; }
            plugin.getTeleportManager().teleport(from, new TeleportLocation(to.getLocation(), plugin.getConfigManager().getServerName()));
        }
        return true;
    }
}
