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

import de.steamwar.linkage.Linked;
import de.steamwar.towerrun.TowerRun;
import de.steamwar.towerrun.config.WorldConfig;
import de.steamwar.towerrun.game.TowerRunGame;
import de.steamwar.towerrun.game.TowerRunPlayer;
import de.steamwar.towerrun.state.GameState;
import de.steamwar.towerrun.state.GameStateBukkitListener;
import de.steamwar.towerrun.state.GameStates;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;

import java.util.EnumSet;

@Linked
public class GlobalListener extends GameStateBukkitListener implements Listener {
    public GlobalListener() {
        super(EnumSet.allOf(GameStates.class));
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        if (TowerRunGame.isAlive(TowerRunPlayer.get(event.getPlayer()))) {
            TowerRunGame.remove(TowerRunPlayer.get(event.getPlayer()));
        }
        TowerRunPlayer.remove(event.getPlayer());

        if (Bukkit.getOnlinePlayers().isEmpty() || (Bukkit.getOnlinePlayers().size() == 1 && Bukkit.getOnlinePlayers().stream().allMatch(player -> player == event.getPlayer()))) {
            Bukkit.shutdown();
        }
    }

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
        Location to = event.getTo();
        if (to.getX() > WorldConfig.MAP_MIN_X && to.getX() < WorldConfig.MAP_MAX_X && to.getZ() > WorldConfig.MAP_MIN_Z && to.getZ() < WorldConfig.MAP_MAX_Z && to.getY() > WorldConfig.ESCAPE_HEIGHT - 20) {
            return;
        }

        event.setCancelled(true);
    }

    @EventHandler
    public void onPlayerRespawn(PlayerRespawnEvent event) {
        event.setRespawnLocation(WorldConfig.SPAWN);
    }

    @EventHandler
    public void onAsyncPlayerChat(AsyncPlayerChatEvent event) {
        TowerRunPlayer player = TowerRunPlayer.get(event.getPlayer());
        if (GameState.getCurrentState() == GameStates.RUNNING && (TowerRunGame.isAlive(player) || TowerRunGame.isEscaped(player))) {
            String prefix;
            if (TowerRunGame.isAlive(player)) {
                if (event.getPlayer().getGameMode() == GameMode.SPECTATOR) {
                    prefix = "§c☠";
                } else {
                    prefix = "§6❤";
                }
            } else {
                prefix = "§a✔";
            }
            TowerRun.getMessage().broadcastPrefixless("PARTICIPANT_CHAT", prefix, event.getPlayer().getName(), event.getMessage());
        } else {
            TowerRun.getMessage().broadcastPrefixless("SPECTATOR_CHAT", event.getPlayer().getName(), event.getMessage());
        }
        event.setCancelled(true);
    }
}
