package de.viper.survivalplus.commands;

import de.viper.survivalplus.SurvivalPlus;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class TeleportCommands implements CommandExecutor {

    private final SurvivalPlus plugin;
    private final Map<UUID, UUID> teleportRequests = new HashMap<>();

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

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(plugin.getMessage("only-players"));
            return true;
        }

        Player player = (Player) sender;
        String cmd = label.toLowerCase();

        switch (cmd) {
            case "tp":
                return handleTp(player, args);
            case "tphere":
                return handleTphere(player, args);
            case "tpa":
                return handleTpa(player, args);
            case "tpaccept":
                return handleTpAccept(player);
            case "tpdeny":
                return handleTpDeny(player);
            default:
                return false;
        }
    }

    private boolean handleTp(Player sender, String[] args) {
        if (!sender.hasPermission("survivalplus.tp")) {
            sender.sendMessage(plugin.getMessage("no-permission"));
            return true;
        }
        if (args.length != 1) {
            sender.sendMessage(plugin.getMessage("teleport-usage"));
            return true;
        }

        Player target = Bukkit.getPlayer(args[0]);
        if (target == null) {
            sender.sendMessage(plugin.getMessage("player-not-found").replace("%player%", args[0]));
            return true;
        }

        sender.teleport(target);
        sender.sendMessage(plugin.getMessage("teleport-success").replace("%player%", target.getName()));
        return true;
    }

    private boolean handleTphere(Player sender, String[] args) {
        if (!sender.hasPermission("survivalplus.tphere")) {
            sender.sendMessage(plugin.getMessage("no-permission"));
            return true;
        }
        if (args.length != 1) {
            sender.sendMessage(plugin.getMessage("tphere-usage"));
            return true;
        }

        Player target = Bukkit.getPlayer(args[0]);
        if (target == null) {
            sender.sendMessage(plugin.getMessage("player-not-found").replace("%player%", args[0]));
            return true;
        }

        target.teleport(sender);
        sender.sendMessage(plugin.getMessage("tphere-success").replace("%player%", target.getName()));
        return true;
    }

    private boolean handleTpa(Player sender, String[] args) {
        if (!sender.hasPermission("survivalplus.tpa")) {
            sender.sendMessage(plugin.getMessage("no-permission"));
            return true;
        }
        if (args.length != 1) {
            sender.sendMessage(plugin.getMessage("tpa-usage"));
            return true;
        }

        Player target = Bukkit.getPlayer(args[0]);
        if (target == null) {
            sender.sendMessage(plugin.getMessage("player-not-found").replace("%player%", args[0]));
            return true;
        }

        teleportRequests.put(target.getUniqueId(), sender.getUniqueId());
        sender.sendMessage(plugin.getMessage("tpa-sent").replace("%player%", target.getName()));
        target.sendMessage(plugin.getMessage("tpa-received").replace("%player%", sender.getName()));
        return true;
    }

    private boolean handleTpAccept(Player target) {
        if (!target.hasPermission("survivalplus.tpaccept")) {
            target.sendMessage(plugin.getMessage("no-permission"));
            return true;
        }

        UUID senderId = teleportRequests.remove(target.getUniqueId());
        if (senderId == null) {
            target.sendMessage(plugin.getMessage("no-tpa-request"));
            return true;
        }

        Player sender = Bukkit.getPlayer(senderId);
        if (sender == null) {
            target.sendMessage(plugin.getMessage("player-not-found"));
            return true;
        }

        sender.teleport(target);
        sender.sendMessage(plugin.getMessage("tpa-accepted").replace("%player%", target.getName()));
        target.sendMessage(plugin.getMessage("tpaccept-success").replace("%player%", sender.getName()));
        return true;
    }

    private boolean handleTpDeny(Player target) {
        if (!target.hasPermission("survivalplus.tpdeny")) {
            target.sendMessage(plugin.getMessage("no-permission"));
            return true;
        }

        UUID senderId = teleportRequests.remove(target.getUniqueId());
        if (senderId == null) {
            target.sendMessage(plugin.getMessage("no-tpa-request"));
            return true;
        }

        Player sender = Bukkit.getPlayer(senderId);
        if (sender != null) {
            sender.sendMessage(plugin.getMessage("tpa-denied").replace("%player%", target.getName()));
        }

        target.sendMessage(plugin.getMessage("tpdeny-success"));
        return true;
    }
}
