package com.yaohun.demonskills.gui;

import com.yaohun.demonskills.manage.SkillManager;
import com.yaohun.demonskills.data.SkillData;
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.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;

public class SkillStoneGui implements Listener {

    private static String invTitle;

    public SkillStoneGui() {
        invTitle = "技能系统 - 灵石列表";
    }

    public static void OpenGui(Player player,String playerName,int skillLevel){
        Inventory inv = Bukkit.createInventory(null,54,invTitle);
        HashMap<String, SkillData> skillDataMap = SkillManager.skillDataMap;
        for (SkillData skillData : skillDataMap.values()) {
            inv.addItem(skillData.getItemStack(playerName,skillLevel));
        }
        player.openInventory(inv);
    }

    @EventHandler
    public void onClick(InventoryClickEvent e){
        int rawSlot = e.getRawSlot();
        Player player = (Player) e.getWhoClicked();
        if(e.getView().getTitle().equals(invTitle)){
            e.setCancelled(true);
            if(rawSlot >= 0 && rawSlot < 54){
                ItemStack item = e.getCurrentItem().clone();
                if(!DemonAPI.itemIsNull(item)){
                    NbtItem nbtItem = new NbtItem(item);
                    if(nbtItem.hasKey("skillName")){
                        if(e.getClick() == ClickType.LEFT) {
                            item.setAmount(1);
                            player.getInventory().addItem(item);
                        }else if(e.getClick() == ClickType.RIGHT){
                            item.setAmount(10);
                            player.getInventory().addItem(item);
                        }
                        player.playSound(player.getLocation(), Sound.ENTITY_CHICKEN_EGG,1,2);
                    }
                }
            }
        }
    }
}
