/*
 *   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.misslewars.listener;

import de.steamwar.linkage.Linked;
import de.steamwar.misslewars.FightState;
import de.steamwar.misslewars.MWTeam;
import de.steamwar.misslewars.MissileWars;
import de.steamwar.misslewars.SpawnPlatformCreator;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.util.Vector;

import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;

@Linked
public class DeathListener extends BasicListener {

    private static final Vector ZERO = new Vector(0, 0, 0);
    private final Set<Player> respawnedPlayers = new HashSet<>();

    public DeathListener() {
        super(EnumSet.allOf(FightState.class));
    }

    @EventHandler
    public void onDeath(PlayerDeathEvent e) {
        e.setDeathMessage(null);
        if (Bukkit.getOnlinePlayers().size() > 10) {
            for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
                onlinePlayer.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(e.getEntity().getDisplayName() + " §cstarb"));
            }
        } else {
            Bukkit.broadcastMessage(e.getEntity().getDisplayName() + " §cstarb");
        }
    }

    @EventHandler
    public void onRespawn(PlayerRespawnEvent e) {
        Player p = e.getPlayer();
        p.setFireTicks(0);
        MWTeam team = MissileWars.getTeam(p);
        if (team == null) return;

        e.setRespawnLocation(team.getSpawn());
        e.getPlayer().setVelocity(ZERO);
        respawnedPlayers.add(p);
    }

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
        if (respawnedPlayers.isEmpty()) return;
        Player player = event.getPlayer();
        if (!respawnedPlayers.contains(player)) return;
        respawnedPlayers.remove(player);
        new SpawnPlatformCreator(player);
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        respawnedPlayers.remove(event.getPlayer());
    }

}
