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

import de.steamwar.fightsystem.ArenaMode;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.events.TeamDeathEvent;
import de.steamwar.fightsystem.events.TeamLeaveEvent;
import de.steamwar.fightsystem.events.TeamSpawnEvent;
import de.steamwar.fightsystem.fight.Fight;
import de.steamwar.fightsystem.fight.FightPlayer;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.StateDependentListener;
import de.steamwar.fightsystem.states.StateDependentTask;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;

@Linked
public class ArenaBorder implements Listener {

    private final Border spectatorBorder = new Border(Config.PlayerRegion, true, 5, "NO_ARENA_LEAVING", "ArenaBorder.spectatorBorder");
    private final Border playerBorder = new Border(Config.PlayerRegion.to2d(), true, 5, "NO_ARENA_LEAVING", "ArenaBorder.playerBorder");

    public ArenaBorder() {
        new StateDependentListener(ArenaMode.All, FightState.All, this);
        new StateDependentTask(ArenaMode.All, FightState.Running, this::damage, 2, 2);
        new StateDependentListener(!Config.GroundWalkable, FightState.AntiRunning, new Listener() {
            @EventHandler
            public void onMove(PlayerMoveEvent e) {
                Player player = e.getPlayer();
                if(e.getTo().getY() <= Config.PlayerRegion.getMinY() && playerBorder.contains(player))
                    player.teleport(Fight.getPlayerTeam(player).getSpawn());
            }
        });
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e) {
        addToSpectator(e.getPlayer());
    }

    @EventHandler
    public void onTeamJoin(TeamSpawnEvent e) {
        e.getFightPlayer().ifPlayer(player -> {
            spectatorBorder.removePlayer(player);
            playerBorder.addPlayer(player);
        });
    }

    @EventHandler
    public void onTeamDeath(TeamDeathEvent e) {
        e.getFightPlayer().ifPlayer(player -> {
            playerBorder.removePlayer(player);
            addToSpectator(player);
        });
    }

    @EventHandler
    public void onTeamLeave(TeamLeaveEvent e) {
        e.getFightPlayer().ifPlayer(player -> {
            playerBorder.removePlayer(player);
            addToSpectator(player);
        });
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent e) {
        spectatorBorder.removePlayer(e.getPlayer());
    }

    private void damage() {
        Fight.teams().forEach(team -> {
            for(FightPlayer fp : team.getPlayers()) {
                LivingEntity entity = fp.getEntity();
                if(fp.isLiving() && entity.getLocation().getY() <= Config.PlayerRegion.getMinY())
                    entity.damage(1);
            }
        });
    }

    private void addToSpectator(Player player) {
        if(Config.ArenaLeaveable || !player.isOnline() || playerBorder.contains(player))
            return;

        spectatorBorder.addPlayer(player);
    }
}
