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

import de.steamwar.linkage.Linked;
import de.steamwar.lobby.LobbySystem;
import de.steamwar.lobby.command.ModifyCommand;
import de.steamwar.lobby.special.advent.AdventsCalendar;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.bukkit.inventory.ItemStack;

@Linked
public class InventoryInteraction implements Listener {

    @EventHandler
    public void handlePlayerInteract(PlayerInteractEvent event) {
        ItemStack item = event.getItem();
        if(item == null)
            return;

        if(item.getType() == Material.FIREWORK_ROCKET && (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
            int offset;
            if (AdventsCalendar.active()) {
                offset = -1;
            } else {
                offset = 0;
            }

            Bukkit.getScheduler().runTaskLater(LobbySystem.getInstance(), () -> {
                event.getPlayer().getInventory().setItem(PlayerSpawn.FIREWORK_SLOT + offset, PlayerSpawn.FIREWORK);
            }, 1L);
            return;
        }

        if(!ModifyCommand.modifying(event.getPlayer()))
            event.setCancelled(true);
    }

    @EventHandler
    public void handlePlayerDropItem(PlayerDropItemEvent event) {
        event.setCancelled(true);
    }

    @EventHandler
    public void handlePlayerPickupItem(PlayerPickupItemEvent event) {
        event.setCancelled(true);
    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void handleInventoryClick(InventoryClickEvent event) {
        if(!ModifyCommand.modifying(event.getWhoClicked()))
            event.setCancelled(true);
    }

    @EventHandler
    public void handlePlayerSwapHandItemsEvent(PlayerSwapHandItemsEvent event) {
        if(!ModifyCommand.modifying(event.getPlayer()))
            event.setCancelled(true);
    }

}
