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