package de.teleportsuite.managers;

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

public class SpawnManager {
    private final TeleportSuite plugin;
    public SpawnManager(TeleportSuite plugin) { this.plugin = plugin; }

    public void setSpawn(Player player) {
        TeleportLocation loc = new TeleportLocation(player.getLocation(), plugin.getConfigManager().getServerName());
        plugin.getDatabaseManager().saveSpawn("spawn", loc);
        player.sendMessage(plugin.getConfigManager().getMessage("spawn-set"));
    }

    public void setFirstSpawn(Player player) {
        TeleportLocation loc = new TeleportLocation(player.getLocation(), plugin.getConfigManager().getServerName());
        plugin.getDatabaseManager().saveSpawn("firstspawn", loc);
        player.sendMessage(plugin.getConfigManager().getMessage("firstspawn-set"));
    }

    public void teleportToSpawn(Player player) {
        TeleportLocation spawn = plugin.getDatabaseManager().getSpawn("spawn");
        if (spawn == null) { player.sendMessage("§cKein Spawn gesetzt!"); return; }
        plugin.getTeleportManager().teleport(player, spawn);
    }

    public void teleportToFirstSpawn(Player player) {
        TeleportLocation firstSpawn = plugin.getDatabaseManager().getSpawn("firstspawn");
        if (firstSpawn == null) {
            teleportToSpawn(player);
            return;
        }
        plugin.getTeleportManager().teleport(player, firstSpawn);
    }

    public TeleportLocation getRespawnLocation() {
        return plugin.getDatabaseManager().getSpawn("spawn");
    }
}
