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.Material;
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 java.util.HashMap;

public class SkillsGui implements Listener {

    private static final String invTitle = Config.langData.getMessage("Skills-Title");

    public static void OpenGui(Player player){
        int pLevel = player.getLevel();
        String playerName = player.getName();
        PlayerManager playerManager = SkillsMain.getPlayerManager();
        PlayerData playerData = playerManager.getPlayerData(playerName);
        Inventory inv = Bukkit.createInventory(null,45,invTitle);
        setInvGlass(inv);
        inv.setItem(11, StackUtil.getSkillSlot(pLevel,10));
        inv.setItem(13, StackUtil.getSkillSlot(pLevel,30));
        inv.setItem(15, StackUtil.getSkillSlot(pLevel,50));
        inv.setItem(30, StackUtil.getSkillSlot(pLevel,70));
        inv.setItem(32, StackUtil.getSkillSlot(pLevel,90));
        HashMap<Integer,ItemStack> stackHashMap = playerData.getKeyBoardStackMap();
        for (Integer keyBoard : stackHashMap.keySet()) {
            int slot = 11;
            if(keyBoard == 44){
                slot = 13;
            }else if(keyBoard == 45){
                slot = 15;
            }else if(keyBoard == 46){
                slot = 30;
            }else if(keyBoard == 47){
                slot = 32;
            }
            inv.setItem(slot, stackHashMap.get(keyBoard));
        }
        player.openInventory(inv);
    }

    private static void setInvGlass(Inventory inv){
        for (int slot = 10; slot < 35;slot++){
            inv.setItem(slot, DemonAPI.glass(8,"§r"));
        }
        for (int slot = 0; slot < 10;slot++){
            inv.setItem(slot, DemonAPI.glass(15,"§r"));
        }
        for (int slot = 35; slot < 45;slot++){
            inv.setItem(slot, DemonAPI.glass(15,"§r"));
        }
        for (int slot : new int[]{17,18,26,27}){
            inv.setItem(slot, DemonAPI.glass(15,"§r"));
        }
    }


    @EventHandler
    public void onCllick(InventoryClickEvent e){
        int rawSlot = e.getRawSlot();
        Player player = (Player) e.getWhoClicked();
        int pLevel = player.getLevel();
        ItemStack stack = e.getCurrentItem();
        Inventory inv = e.getInventory();
        if(e.getView().getTitle().equalsIgnoreCase(invTitle)){
            e.setCancelled(true);
            if (rawSlot == 11) {
                setItemSlotGui(player,inv,rawSlot,stack,10);
            }
            if(rawSlot == 13){
                setItemSlotGui(player,inv,rawSlot,stack,30);
            }
            if(rawSlot == 15){
                setItemSlotGui(player,inv,rawSlot,stack,50);
            }
            if(rawSlot == 30){
                setItemSlotGui(player,inv,rawSlot,stack,70);
            }
            if(rawSlot == 32){
                setItemSlotGui(player,inv,rawSlot,stack,90);
            }
        }
    }

    private static void setItemSlotGui(Player player,Inventory inv,int rawSlot,ItemStack stack,int needLevel){
        if(needLevel > player.getLevel()){
            String message = Config.langData.getMessage("levelNotSatisfied");
            MessageUtil.sendMessage(player,message,Sound.ENTITY_VILLAGER_NO);
            return;
        }
        // 判断玩家点击的物品
        if(!DemonAPI.itemIsNull(stack)){
            NbtItem nbtItem = new NbtItem(stack);
            // 若点击的物品是技能石则自动退回到背包
            if(nbtItem.hasKey("skillName")){
                player.getInventory().addItem(stack);
                inv.setItem(rawSlot, StackUtil.getSkillSlot(player.getLevel(),needLevel));
                player.playSound(player.getLocation(), Sound.BLOCK_COMPARATOR_CLICK,1,1);
                return;
            }
            // 若是其他物品则打开宝石界面
            StoneEquipGui.OpenGui(player,rawSlot);
        }
    }




    @EventHandler
    public void onInventoryClose(InventoryCloseEvent e){
        Player player = (Player) e.getPlayer();
        String playerName = player.getName();
        if(e.getView().getTitle().equalsIgnoreCase(invTitle)){
            Inventory inv = e.getInventory();
            PlayerManager playerManager = SkillsMain.getPlayerManager();
            PlayerData playerData = playerManager.getPlayerData(playerName);
            playerData.resetSkillDataMap();
            checkItemSetBindKeyBoard(playerData,inv.getItem(11),11);
            checkItemSetBindKeyBoard(playerData,inv.getItem(13),13);
            checkItemSetBindKeyBoard(playerData,inv.getItem(15),15);
            checkItemSetBindKeyBoard(playerData,inv.getItem(30),30);
            checkItemSetBindKeyBoard(playerData,inv.getItem(32),32);
        }
    }

    public static void checkItemSetBindKeyBoard(PlayerData playerData,ItemStack itemStack, int slot){
        int keyBoard = 19;
        if(slot == 13){
            keyBoard = 44;
        } else if(slot == 15){
            keyBoard = 45;
        } else if(slot == 30){
            keyBoard = 46;
        } else if(slot == 32){
            keyBoard = 47;
        }
        if(!DemonAPI.itemIsNull(itemStack) && !DemonAPI.itemIsLore(itemStack)){
            NbtItem nbtItem = new NbtItem(itemStack);
            if(nbtItem.hasKey("skillName")){
                String skillName = nbtItem.getString("skillName");
                int skillLevel = nbtItem.getInteger("skillLevel");
                playerData.setSkillLevel(skillName,skillLevel);
                playerData.setSkillBindMap(keyBoard,skillName);
                playerData.setKeyBoardStackMap(keyBoard,itemStack);
            } else {
                playerData.setSkillBindMap(keyBoard,"default");
            }
        } else {
            playerData.setSkillBindMap(keyBoard,"default");
        }
    }
}
