package de.teleportsuite.managers;

import de.teleportsuite.TeleportSuite;
import de.teleportsuite.models.Portal;
import de.teleportsuite.models.TeleportLocation;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import java.util.*;

public class PortalManager {
    private final TeleportSuite plugin;
    private List<Portal> portals = new ArrayList<>();
    // Selection for portal creation: player -> pos1, pos2
    private final Map<UUID, Location> pos1 = new HashMap<>();
    private final Map<UUID, Location> pos2 = new HashMap<>();
    // Cooldown to prevent multiple triggers
    private final Map<UUID, Long> cooldown = new HashMap<>();

    public PortalManager(TeleportSuite plugin) {
        this.plugin = plugin;
        loadPortals();
    }

    public void loadPortals() {
        portals = plugin.getDatabaseManager().getAllPortals();
    }

    public void setPos1(Player player, Location loc) {
        pos1.put(player.getUniqueId(), loc);
        player.sendMessage("§aPos1 gesetzt: §f" + formatLoc(loc));
    }

    public void setPos2(Player player, Location loc) {
        pos2.put(player.getUniqueId(), loc);
        player.sendMessage("§aPos2 gesetzt: §f" + formatLoc(loc));
    }

    public boolean createPortal(Player player, String name, String targetServer, String targetWorld, double tx, double ty, double tz) {
        Location l1 = pos1.get(player.getUniqueId());
        Location l2 = pos2.get(player.getUniqueId());
        if (l1 == null || l2 == null) {
            player.sendMessage("§cBitte setze erst Pos1 und Pos2 mit §e/setportal pos1 §cund §e/setportal pos2");
            return false;
        }
        TeleportLocation dest = new TeleportLocation(targetWorld, tx, ty, tz, 0, 0, targetServer);
        Portal portal = new Portal(name, l1.getWorld().getName(),
                l1.getBlockX(), l1.getBlockY(), l1.getBlockZ(),
                l2.getBlockX(), l2.getBlockY(), l2.getBlockZ(),
                targetServer, dest);
        plugin.getDatabaseManager().savePortal(portal);
        portals.add(portal);
        pos1.remove(player.getUniqueId());
        pos2.remove(player.getUniqueId());
        player.sendMessage(plugin.getConfigManager().getMessage("portal-created", "name", name));
        return true;
    }

    public boolean deletePortal(String name) {
        portals.removeIf(p -> p.getName().equalsIgnoreCase(name));
        return plugin.getDatabaseManager().deletePortal(name);
    }

    public void checkPortal(Player player, Location loc) {
        long now = System.currentTimeMillis();
        if (now - cooldown.getOrDefault(player.getUniqueId(), 0L) < 2000) return;

        for (Portal portal : portals) {
            if (portal.contains(loc)) {
                cooldown.put(player.getUniqueId(), now);
                plugin.getTeleportManager().teleport(player, portal.getDestination(), true);
                break;
            }
        }
    }

    public List<Portal> getPortals() { return portals; }

    private String formatLoc(Location loc) {
        return loc.getBlockX() + ", " + loc.getBlockY() + ", " + loc.getBlockZ() + " in " + loc.getWorld().getName();
    }
}
