package com.yaohun.demonskills.gui;

import com.yaohun.demonskills.SkillsMain;
import com.yaohun.demonskills.config.Config;
import com.yaohun.demonskills.config.MessageUtil;
import com.yaohun.demonskills.data.PlayerData;
import com.yaohun.demonskills.manage.PlayerManager;
import com.yaohun.demonskills.util.StackUtil;
import me.Demon.DemonPlugin.DemonAPI;
import me.Demon.DemonPlugin.data.NbtItem;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.ArrayList;
import java.util.List;

public class StoneEquipGui implements Listener {

    private static String invTitle = "§s§k§e§q§u§i§p§r";

    public static void OpenGui(Player player,int rawSlot) {
        List<ItemStack> stackList = new ArrayList<ItemStack>();
        Inventory pinv = player.getInventory();
        for (int i = 0;i < 36;i++) {
            ItemStack item = pinv.getItem(i);
            if(!DemonAPI.itemIsNull(item)) {
                NbtItem nbtItem = new NbtItem(item);
                if(nbtItem.hasKey("skillName")){
                    stackList.add(item);
                    pinv.setItem(i,null);
                }
            }
        }
        if(stackList.isEmpty()){
            player.closeInventory();
            MessageUtil.sendMessage(player,Config.langData.getMessage("equipFail"),Sound.ENTITY_VILLAGER_NO);
            return;
        }
        Inventory inv = Bukkit.createInventory(null,18,invTitle+ Config.langData.getMessage("Equip-Title"));
        for (ItemStack stack : stackList) {
            inv.addItem(stack);
        }
        inv.setItem(17, StackUtil.tagSkillStack(rawSlot));
        player.openInventory(inv);
    }

    @EventHandler
    public void onClick(InventoryClickEvent e){
        int slot = e.getRawSlot();
        Player player = (Player) e.getWhoClicked();
        String playerName = player.getName();
        Inventory inv = e.getInventory();
        ItemStack skillStone = e.getCurrentItem();
        if(e.getView().getTitle().contains(invTitle)){
            e.setCancelled(true);
            if(slot >= 0 && slot < 16) {
                ItemStack stack = inv.getItem(17);
                if (!DemonAPI.itemIsNull(stack)) {
                    NbtItem nbtItem = new NbtItem(stack);
                    int rawSlot = nbtItem.getInteger("RawSlot");
                    NbtItem skNbtItem = new NbtItem(skillStone);
                    if (skNbtItem.hasKey("skillName")) {
                        inv.setItem(slot,null);
                        player.closeInventory();
                        PlayerManager playerManager = SkillsMain.getPlayerManager();
                        PlayerData playerData = playerManager.getPlayerData(playerName);

                        SkillsGui.checkItemSetBindKeyBoard(playerData, skillStone, rawSlot);
                        player.playSound(player.getLocation(), Sound.BLOCK_COMPARATOR_CLICK,1,1);
                        new BukkitRunnable() {
                            @Override
                            public void run() {
                                SkillsGui.OpenGui(player);
                            }
                        }.runTaskLater(SkillsMain.inst(), 5L);
                    }
                }
            }
        }
    }

    @EventHandler
    public void onClose(InventoryCloseEvent event) {
        Player player = (Player) event.getPlayer();
        Inventory inv = event.getInventory();
        if(event.getView().getTitle().contains(invTitle)) {
            for (int i = 0;i < 16;i++) {
                ItemStack item = inv.getItem(i);
                if(!DemonAPI.itemIsNull(item)){
                    player.getInventory().addItem(item);
                }
            }
        }
    }
}
