/*
 *   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.FightSystem;
import de.steamwar.fightsystem.fight.Fight;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.StateDependentListener;
import de.steamwar.linkage.Linked;
import net.md_5.bungee.api.ChatMessageType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.event.hanging.HangingBreakEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerKickEvent;

@Linked
public class DenyWorldInteraction implements Listener {

    public DenyWorldInteraction() {
        new StateDependentListener(ArenaMode.Test, FightState.PreRunning, this);
        new StateDependentListener(ArenaMode.AntiTest, FightState.AntiRunning, this);
    }

    @EventHandler
    public void handleBlockBreak(BlockBreakEvent event) {
        Player player = event.getPlayer();
        if(Fight.fighting(player)) {
            event.setCancelled(true);
            FightSystem.getMessage().sendPrefixless("NO_BLOCK_BREAK", player, ChatMessageType.ACTION_BAR);
        }
    }

    @EventHandler
    public void handleItemDrop(PlayerDropItemEvent event) {
        Player player = event.getPlayer();
        if(Fight.fighting(player)) {
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void handleHangingBreak(HangingBreakEvent event) {
        if(Config.ArenaRegion.inRegion(event.getEntity().getLocation())) {
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void handleBlockPlace(BlockPlaceEvent event) {
        Player player = event.getPlayer();
        if(Fight.fighting(player)) {
            event.setCancelled(true);
            FightSystem.getMessage().sendPrefixless("NO_BLOCK_PLACE", player, ChatMessageType.ACTION_BAR);
        }
    }

    @EventHandler
    public void handleEntityExplode(EntityExplodeEvent event) {
        if(!Config.ArenaLeaveable || Config.ArenaRegion.inRegion(event.getLocation()))
            event.setCancelled(true);
    }

    @EventHandler
    public void handleBlockBurn(BlockIgniteEvent event) {
        if(!Config.ArenaLeaveable || Config.ArenaRegion.inRegion(event.getBlock()))
            event.setCancelled(true);
    }

    @EventHandler
    public void handlePlayerKickEvent(PlayerKickEvent e){
        if(e.getReason().contains("Flying is not enabled"))
            e.setCancelled(true);
    }

    @EventHandler
    public void handleProjectileLaunch(ProjectileLaunchEvent event) {
        if(event.getEntity().getShooter() instanceof Player) {
            Player player = (Player) event.getEntity().getShooter();
            if(Fight.fighting(player)) {
                event.setCancelled(true);
                FightSystem.getMessage().sendPrefixless("NO_BOW_USAGE", player, ChatMessageType.ACTION_BAR);
            }
        }
    }
}
