/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.lobby.listener;

import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import org.bukkit.*;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerInteractEvent;

@Linked
public class TeleporterListener implements Listener {

    private static final World world = Bukkit.getWorlds().get(0);

    private static final Location spawn = world.getSpawnLocation().clone().add(0.5, 0, 0.5);
    private static final Location team = new Location(world, 2719.5, 59, 1311.5, -90, -5);
    private static final Location map = new Location(world, 2336.5, 38, 1423.5);
    private static final Location bau = new Location(world, 1951, 66.1, 1337, -38, -5);
    private static final Location arenen = new Location(world, 2255.5, 21, 1450.5);

    @EventHandler
    public void handlePlayerInteract(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        if (!PlayerSpawn.NETHER_STAR.equals(event.getItem())) return;

        SWInventory swInventory = new SWInventory(player, () -> {
            return Bukkit.createInventory(null, InventoryType.HOPPER, "§6Teleporter");
        });
        swInventory.setItem(0, new SWItem(Material.WOODEN_AXE, "§eBaubereich").getItemStack(), clickType -> {
            teleport(bau, player);
        });
        swInventory.setItem(1, new SWItem(Material.MAGMA_CREAM, "§eSpawn").getItemStack(), clickType -> {
            teleport(spawn, player);
        });
        swInventory.setItem(2, new SWItem(Material.BEACON, "§eTeamhalle").getItemStack(), clickType -> {
            teleport(team, player);
        });
        swInventory.setItem(3, new SWItem(Material.MAP, "§eMap").getItemStack(), clickType -> {
            teleport(map, player);
        });
        swInventory.setItem(4, new SWItem(Material.CLOCK, "§eArenen").getItemStack(), clickType -> {
            teleport(arenen, player);
        });
        swInventory.open();
    }

    private void teleport(Location location, Player player) {
        player.teleport(location);
        player.playSound(location, Sound.BLOCK_ENCHANTMENT_TABLE_USE, 0.25f, 1.0f);
        Portals.getStack(player).clear();
    }
}
