package com.loganmagnan.eventcore.listeners;

import com.loganmagnan.eventcore.EventCore;
import com.loganmagnan.eventcore.managers.hotbar.ClickableItem;
import com.loganmagnan.eventcore.managers.hotbar.HotBar;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;

public class ClickableItemListener implements Listener {

    private EventCore main = EventCore.getInstance();

    @EventHandler
    public void onBlockPlace(BlockPlaceEvent event) {
        Player player = event.getPlayer();

        ItemStack itemStack = player.getItemInHand();

        if (itemStack.getType() == Material.AIR) {
            return;
        }

        for (HotBar hotBar : this.main.getHotBarManager().getHotBars()) {
            for (ClickableItem clickableItem : hotBar.getClickableItems().values()) {
                if (!itemStack.isSimilar(clickableItem.getItemStack())) {
                    continue;
                }

                event.setCancelled(true);

                break;
            }
        }
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        Player player = event.getPlayer();

        ItemStack itemStack = player.getItemInHand();

        if (itemStack.getType() == Material.AIR || (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK)) {
            return;
        }

        for (HotBar hotBar : this.main.getHotBarManager().getHotBars()) {
            for (ClickableItem clickableItem : hotBar.getClickableItems().values()) {
                if (!itemStack.isSimilar(clickableItem.getItemStack())) {
                    continue;
                }

                event.setCancelled(true);
                event.setUseItemInHand(Event.Result.DENY);
                event.setUseInteractedBlock(Event.Result.DENY);

                if (clickableItem.getClickHandler() == null) {
                    continue;
                }

                clickableItem.getClickHandler().executeAs(player);

                break;
            }
        }
    }

    @EventHandler
    public void onPlayerDropItem(PlayerDropItemEvent event) {
        Player player = event.getPlayer();

        ItemStack itemStack = event.getItemDrop().getItemStack();

        for (HotBar hotBar : this.main.getHotBarManager().getHotBars()) {
            for (ClickableItem clickableItem : hotBar.getClickableItems().values()) {
                if (!itemStack.isSimilar(clickableItem.getItemStack()) || clickableItem.isDroppable()) {
                    continue;
                }

                event.setCancelled(true);

                break;
            }
        }
    }

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        Player player = (Player) event.getWhoClicked();

        for (HotBar hotBar : this.main.getHotBarManager().getHotBars()) {
            for (ClickableItem clickableItem : hotBar.getClickableItems().values()) {
                if (event.getCurrentItem() != null && event.getCurrentItem().isSimilar(clickableItem.getItemStack()) && !clickableItem.isMoveable()) {
                    event.setCancelled(true);
                    event.setResult(Event.Result.DENY);

                    break;
                }

                if (event.getCursor() != null && event.getCursor().isSimilar(clickableItem.getItemStack()) && !clickableItem.isMoveable()) {
                    event.setCancelled(true);
                    event.setResult(Event.Result.DENY);

                    break;
                }

                if (event.getHotbarButton() != -1 && player.getInventory().getItem(event.getHotbarButton()) != null && player.getInventory().getItem(event.getHotbarButton()).isSimilar(clickableItem.getItemStack()) && !clickableItem.isMoveable()) {
                    event.setCancelled(true);
                    event.setResult(Event.Result.DENY);

                    break;
                }
            }
        }
    }
}
