/*
 *   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.FightState;
import de.steamwar.misslewars.MWTeam;
import de.steamwar.misslewars.MissileWars;
import de.steamwar.misslewars.items.SpecialItem;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.entity.ItemDespawnEvent;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;

import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;

@Linked
public class ItemListener extends BasicListener {

    private Set<Integer> RED_ITEMS = new HashSet<>();
    private Set<Integer> BLUE_ITEMS = new HashSet<>();

    public ItemListener() {
        super(EnumSet.of(FightState.FIGHTING));
    }

    @Override
    public void enable() {
        super.enable();
        RED_ITEMS.clear();
        BLUE_ITEMS.clear();
    }

    @EventHandler
    public void onItemClick(PlayerInteractEvent e) {
        ItemStack item = e.getItem();
        if (item == null) return;
        if (e.getPlayer().hasCooldown(item.getType())) return;
        if (e.getAction() != Action.RIGHT_CLICK_BLOCK && e.getAction() != Action.RIGHT_CLICK_AIR) return;

        if (SpecialItem.handleUse(item, e.getPlayer()) && item.getType() != Material.SPLASH_POTION) {
            item.setAmount(item.getAmount() - 1);
            e.getPlayer().updateInventory();
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onThrow(ProjectileLaunchEvent e) {
        SpecialItem.handleThrow(e);
    }

    @EventHandler
    public void onHit(ProjectileHitEvent e) {
        SpecialItem.handleHit(e);
    }

    @EventHandler
    public void onItemDrop(PlayerDropItemEvent e) {
        MWTeam mwTeam = MissileWars.getTeam(e.getPlayer());
        if (mwTeam == null) {
            e.setCancelled(true);
            return;
        }
        if (e.getItemDrop().getItemStack().isSimilar(MWTeam.bow)) {
            e.setCancelled(true);
            return;
        }
        if (mwTeam == MissileWars.getRedTeam()) {
            RED_ITEMS.add(e.getItemDrop().getEntityId());
        } else {
            BLUE_ITEMS.add(e.getItemDrop().getEntityId());
        }
    }

    @EventHandler
    public void onEntityPickupItem(EntityPickupItemEvent e) {
        Player player = (Player) e.getEntity();
        MWTeam mwTeam = MissileWars.getTeam(player);
        if (mwTeam == null) {
            e.setCancelled(true);
            return;
        }
        if (mwTeam == MissileWars.getRedTeam() && !RED_ITEMS.contains(e.getItem().getEntityId())) {
            e.setCancelled(true);
        } else if (mwTeam == MissileWars.getBlueTeam() && !BLUE_ITEMS.contains(e.getItem().getEntityId())) {
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onItemDespawn(ItemDespawnEvent event) {
        RED_ITEMS.remove(event.getEntity().getEntityId());
        BLUE_ITEMS.remove(event.getEntity().getEntityId());
    }
}
