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

import de.steamwar.fightsystem.ArenaMode;
import de.steamwar.fightsystem.listener.PersonalKitCreator;
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.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.PlayerInventory;

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

@Linked
public class HotbarKitListener implements Listener {

    private static final Set<Player> clicked = new HashSet<>();

    public HotbarKitListener() {
        new StateDependentListener(ArenaMode.AntiReplay, FightState.All, this);
        new StateDependentTask(ArenaMode.AntiReplay, FightState.All, clicked::clear, 10, 10);
    }

    @EventHandler
    public void handlePlayerInteract(PlayerInteractEvent event) {
        if (event.getAction() == Action.PHYSICAL || event.getHand() != EquipmentSlot.HAND) return;

        Player player = event.getPlayer();
        int slot = player.getInventory().getHeldItemSlot();
        click(player, slot, event);
    }

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        int slot = event.getSlot();
        if (slot < 0 || slot >= HotbarKit.HOTBAR_SIZE) return;
        if (!(event.getClickedInventory() instanceof PlayerInventory)) return;

        Player player = (Player) event.getWhoClicked();
        click(player, slot, event);
    }

    private void click(Player player, int slot, Cancellable event) {
        Kit activeKit = Kit.activeKits.get(player);
        if (!(activeKit instanceof HotbarKit) || PersonalKitCreator.inKitCreator(player) || activeKit.getInventory()[slot] == null) {
            return;
        }

        event.setCancelled(true);
        if (!clicked.add(player)) return;

        ((HotbarKit) activeKit).onClicks[slot].accept(player);
    }
}
