package com.io.yutian.boxsimulate.gui;

import cn.hamster3.cdapi.CDTimeAPI;
import com.io.yutian.boxsimulate.BoxSimulate;
import com.io.yutian.boxsimulate.data.BoxData;
import com.praya.myitems.MyItems;
import com.yaohun.itemlibrary.api.ItemKuAPI;
import me.Demon.DemonPlugin.DemonAPI;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class BoxSimulateGui implements InventoryHolder, GuiHolder {

    private Inventory inventory;
    private Player player;

    private Map<Integer, String> cacheItems = new HashMap<>();

    public BoxSimulateGui(Player player) {
        this.inventory = Bukkit.createInventory(this, 27, "装备武器箱 - 模拟开箱");
        this.player = player;
        init();
    }

    private void init() {
        Map<String, BoxData> boxDatas = BoxSimulate.getBoxDatas();
        List<BoxData> boxDataList = boxDatas.values().stream().collect(Collectors.toList());
        int i = 0;
        for (Map.Entry<String, BoxData> entry : boxDatas.entrySet()) {
            BoxData boxData = boxDataList.get(i);
            ItemStack itemStack = getBoxItem(boxData.getItem());
            inventory.setItem(i, itemStack);
            cacheItems.put(i, entry.getKey());
            i++;
        }
    }

    private ItemStack getBoxItem(String id) {
        return ItemKuAPI.getItems(id);
    }

    private ItemStack generateItem(String item) {
        MyItems myItems = (MyItems) Bukkit.getPluginManager().getPlugin("MyItems");
        return myItems.getGameManager().getItemGeneratorManager().getItemGenerator(item).generateItem();
    }

    @Override
    public void onClick(InventoryClickEvent event) {
        int slot = event.getRawSlot();
        event.setCancelled(true);
        if (slot >= 54) {
            return;
        }
        if (!cacheItems.containsKey(slot)) {
            return;
        }
        String id = cacheItems.get(slot);
        long time = CDTimeAPI.getCD(player.getUniqueId(),"box_simulate_box_"+id) / 1000;
        String timeShow = String.format("%.1f",(double) time);
        if(!player.isOp()) {
            if (CDTimeAPI.isCD(player.getUniqueId(), "box_simulate_box_" + id)) {
                player.sendMessage("§7[§6帝国战争§7] §f你需要等待 §e" + timeShow + "秒 §f后才能再次打开.");
                player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1.6F, 1.8F);
                return;
            }
        }
        BoxData boxData = BoxSimulate.getBoxDatas().get(id);
        if (!DemonAPI.hasPoints(player, boxData.getNeed())) {
            player.sendMessage("§7[§6帝国战争§7] §f你需要拥有§d"+DemonAPI.MoneyFormat(boxData.getNeed())+"点券§f才能模拟打开.");
            player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO,1.6F,1.8F);
            return;
        }
        inventory.setItem(slot, generateItem(boxData.getItem()));
        player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP,1.6F,1.8F);
        CDTimeAPI.setPlayerCD(player.getUniqueId(), "box_simulate_box_"+id, 1000 * 60 * 60);
    }

    @Override
    public void onClose(InventoryCloseEvent event) {
    }

    @Override
    public void open() {
        player.openInventory(inventory);
    }

    @Override
    public Inventory getInventory() {
        return inventory;
    }

}
