/*
 *   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.linkage.Linked;
import de.steamwar.lobby.LobbySystem;
import de.steamwar.lobby.boatrace.BoatRace;
import de.steamwar.lobby.special.advent.AdventsCalendar;
import de.steamwar.lobby.util.ItemBuilder;
import de.steamwar.network.NetworkSender;
import de.steamwar.network.packets.client.ImALobbyPacket;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;

@Linked
public class PlayerSpawn implements Listener {

    public static final int FIREWORK_SLOT = 2;
    public static final ItemStack FIREWORK = new ItemBuilder(Material.FIREWORK_ROCKET, 1).setDisplayName("§5Rakete").build();
    public static final int PARTICLE_SLOT = 6;
    public static final int NETHER_STAR_SLOT = 4;
    public static final ItemStack PARTICLE = new ItemBuilder(Material.NAME_TAG).setDisplayName("§6Partikel").setUnbreakable(true).removeAllAttributes().build();
    private static final ItemStack ELYTRA = new ItemBuilder(Material.ELYTRA).setDisplayName("§5Elytra").setUnbreakable(true).removeAllAttributes().build();
    public static final ItemStack NETHER_STAR = new ItemBuilder(Material.NETHER_STAR).setDisplayName("§5Teleporter").setUnbreakable(true).removeAllAttributes().build();

    @EventHandler(priority = EventPriority.HIGH)
    public void onJoin(PlayerJoinEvent e) {
        Player player = e.getPlayer();
        e.setJoinMessage(null);

        player.setGameMode(GameMode.ADVENTURE);
        player.setWalkSpeed(0.5f);
        player.teleport(Bukkit.getWorlds().get(0).getSpawnLocation().clone().add(0.5, 0, 0.5));
        player.setHealth(20);
        player.setFoodLevel(20);
        giveItems(player);

        LobbySystem.getEntityServer(false).addPlayer(player);
        BoatRace.boatNpcServer.addPlayer(player);

        Bukkit.getScheduler().runTaskLater(LobbySystem.getInstance(), () -> NetworkSender.send(new ImALobbyPacket(), player), 20);
    }

    public static void giveItems(Player player) {
        PlayerInventory inventory = player.getInventory();

        inventory.clear();

        inventory.setItem(EquipmentSlot.CHEST, ELYTRA);

        int offset = 0;
        if (AdventsCalendar.active()) {
            offset = -1;
        }

        player.getInventory().setItem(FIREWORK_SLOT + offset, FIREWORK);

        player.getInventory().setItem(PARTICLE_SLOT + offset, PARTICLE);

        player.getInventory().setItem(NETHER_STAR_SLOT + offset, NETHER_STAR);
    }

    @EventHandler(priority = EventPriority.HIGH)
    public void handlePlayerQuit(PlayerQuitEvent event) {
        event.setQuitMessage(null);
    }
}
