/*
 *   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.sql.SteamwarUser;
import de.steamwar.towerrun.TowerRun;
import de.steamwar.towerrun.config.Config;
import de.steamwar.towerrun.config.WorldConfig;
import de.steamwar.towerrun.game.TowerRunGame;
import de.steamwar.towerrun.game.TowerRunPlayer;
import de.steamwar.towerrun.state.GameStateBukkitListener;
import de.steamwar.towerrun.state.GameStates;
import org.bukkit.GameMode;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;

import java.util.EnumSet;
import java.util.HashMap;

@Linked
public class LobbyListener extends GameStateBukkitListener {
    public LobbyListener() {
        super(EnumSet.of(GameStates.WAITING));
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();

        if (TowerRun.getTowerGenerator() != null) {
            player.teleport(TowerRun.getTowerGenerator().getSpawn());
            player.setGameMode(GameMode.SPECTATOR);
        } else {
            player.teleport(WorldConfig.SPAWN);
            player.setGameMode(GameMode.SURVIVAL);
        }

        if (Config.event()) {
            SteamwarUser user = SteamwarUser.get(player.getUniqueId());
            int team = user.getTeam();

            if (team != Config.EVENT_TEAM_BLUE_ID && team != Config.EVENT_TEAM_RED_ID) {
                player.setGameMode(GameMode.SPECTATOR);
                return;
            }

            if (TowerRunGame.PLAYERS_ALIVE.stream().map(towerRunPlayer -> SteamwarUser.get(towerRunPlayer.player().getUniqueId()).getTeam()).filter(integer -> integer == team).count() < Config.EVENT_MAXIMUM_TEAM_MEMBERS) {
                TowerRunGame.PLAYERS_ALIVE.add(TowerRunPlayer.get(player));
            } else {
                player.setGameMode(GameMode.SPECTATOR);
            }
        } else {
            TowerRunGame.PLAYERS_ALIVE.add(TowerRunPlayer.get(player));
        }
    }

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
        if (TowerRun.getTowerGenerator() != null) {
            return;
        }
        if (event.getTo().getY() < WorldConfig.SPAWN.getY() - 10) {
            event.getPlayer().teleport(WorldConfig.SPAWN);
        }
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        event.setCancelled(true);
    }

    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        event.setCancelled(true);
    }

    @EventHandler
    public void onEntityDamage(EntityDamageEvent event) {
        if (event.getEntityType() == EntityType.PLAYER) {
            event.setCancelled(true);
        }
    }
}
