/*
 *   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.bausystem.features.world;

import de.steamwar.bausystem.utils.NMSWrapper;
import de.steamwar.linkage.Linked;
import org.bukkit.attribute.Attribute;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

@Linked
public class InventoryListener implements Listener {

    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) {
        ItemStack stack = e.getCurrentItem();
        if (stack == null || !stack.hasItemMeta()) {
            return;
        }
        assert stack.getItemMeta() != null;
        if (stack.getItemMeta().hasEnchants()) {
            for (Enchantment en : Enchantment.values()) {
                if (stack.getEnchantmentLevel(en) > en.getMaxLevel()) {
                    stack.removeEnchantment(en);
                }
            }
        }

        if (stack.getItemMeta().hasAttributeModifiers()) {
            ItemMeta meta = stack.getItemMeta();
            for (Attribute a : Attribute.values()) {
                meta.removeAttributeModifier(a);
            }
            stack.setItemMeta(meta);
        }
        if (NMSWrapper.impl.checkItemStack(stack)) {
            e.setCurrentItem(null);
            e.setCancelled(true);
            return;
        }

        e.setCurrentItem(stack);
    }

    @EventHandler(ignoreCancelled = true)
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player p = event.getPlayer();
        ItemStack[] content = event.getPlayer().getInventory().getContents();
        for (int i = 0; i < content.length; i++) {
            if (content[i] == null) continue;
            int finalI = i;
            if (NMSWrapper.impl.checkItemStack(content[finalI])) {
                p.getInventory().setItem(i, null);
            }
        }
    }

    @EventHandler(ignoreCancelled = true)
    public void onBlockPlace(BlockPlaceEvent event) {
        Player p = event.getPlayer();
        if (NMSWrapper.impl.checkItemStack(event.getItemInHand())) {
            event.setCancelled(true);
            event.setBuild(false);
            p.getInventory().setItemInMainHand(null);
            p.getInventory().setItemInOffHand(null);
        }
    }
}
