/*
 *   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.Config;
import de.steamwar.misslewars.FightState;
import de.steamwar.misslewars.MissileWars;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerPickupArrowEvent;

import java.util.EnumSet;
import java.util.Objects;

@Linked
public class ArenaListener extends BasicListener {

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

    @EventHandler
    public void onMove(PlayerMoveEvent e) {
        Location location = e.getTo();
        Player p = e.getPlayer();

        assert location != null;
        if (location.getY() < Config.ArenaMinY) {
            if (p.getGameMode() == GameMode.SPECTATOR) {
                p.teleport(e.getFrom());
            } else if (MissileWars.getFightState() == FightState.WAITING) {
                p.teleport(Objects.requireNonNull(MissileWars.getTeam(p)).getSpawn());
            } else {
                p.damage(20.0f);
            }
        } else if (
                location.getX() < Config.ArenaMinX ||
                        location.getX() > Config.ArenaMaxX ||
                        location.getZ() < Config.ArenaMinZ ||
                        location.getZ() > Config.ArenaMaxZ) {
            p.teleport(e.getFrom());
        }
    }

    @EventHandler
    public void onArrowPickup(PlayerPickupArrowEvent e) {
        e.setCancelled(true);
    }

    @EventHandler
    public void onHunger(FoodLevelChangeEvent e) {
        e.setCancelled(true);
    }

    @EventHandler(ignoreCancelled = true)
    public void onInventoryOpen(InventoryOpenEvent event) {
        if (event.getView().getType() != InventoryType.PLAYER &&
                event.getView().getType() != InventoryType.CRAFTING &&
                event.getView().getType() != InventoryType.CREATIVE)
            event.setCancelled(true);
    }
}
