package com.yuyu.srdisassemble.disassemeble;

import com.germ.germplugin.api.dynamic.gui.GermGuiCanvas;
import com.germ.germplugin.api.dynamic.gui.GermGuiScreen;
import com.germ.germplugin.api.dynamic.gui.GermGuiScroll;
import com.germ.germplugin.api.dynamic.gui.GermGuiSlot;
import com.sakurarealm.sritem.api.ItemStackHelper;
import com.sakurarealm.sritem.api.SrItemAPI;
import com.sakurarealm.sritem.api.SrItemManager;
import com.sakurarealm.sritem.api.builder.SrItemHandler;
import com.yuyu.srdisassemble.data.JdbcSqlClass;
import com.yuyu.srdisassemble.pojo.PauseBox;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.HashMap;
import java.util.List;

/**
 * @author 峰。
 * @version 1.0
 * @project SrDisassemble
 * @date 2024/6/21 18:50:18
 * @description 用于存放暂存箱
 */
public class TemporarilyBox {
    public static HashMap<String, HashMap<String,GermGuiScreen>> temporarilyBoxHashMap = new HashMap<>();

    public static GermGuiScreen getGermGuiScreen(String playname,String guiName) {
        if(temporarilyBoxHashMap.containsKey(playname)) {
            HashMap<String, GermGuiScreen> stringGermGuiScreenHashMap = temporarilyBoxHashMap.get(playname);
            if(stringGermGuiScreenHashMap.containsKey(guiName)) {
                return stringGermGuiScreenHashMap.get(guiName);
            }else {
                return null;
            }
        }else {
            return null;
        }
    }

    public static void deleteGuiScreen(String playname,String guiName) {
        if(temporarilyBoxHashMap.containsKey(playname)) {
            temporarilyBoxHashMap.get(playname).remove(guiName);
        }
    }

    public static void addGuiScreen(String playname,String guiName,GermGuiScreen guiScreen) {
        if(temporarilyBoxHashMap.containsKey(playname)) {
            temporarilyBoxHashMap.get(playname).put(guiName, guiScreen);
        }else {
            HashMap<String,GermGuiScreen> stringGermGuiScreenHashMap = new HashMap<>();
            stringGermGuiScreenHashMap.put(guiName, guiScreen);
            temporarilyBoxHashMap.put(playname, stringGermGuiScreenHashMap);
        }
    }


    /**
     * 用于判断当前服务器缓存中是否存在该gui
     */
    public static boolean isHasGui(String playname,String guiName) {
        if(temporarilyBoxHashMap.containsKey(playname)) {
            return temporarilyBoxHashMap.get(playname).containsKey(guiName);
        }else {
            return false;
        }
    }


    /**
     * 用于插件启动时，读取玩家的数据库存储
     */
    public static void getPauseBoxData(){
        List<String> playerList = JdbcSqlClass.getPlayerList();

        for (String player : playerList){
            List<PauseBox> pauseBoxData = JdbcSqlClass.getPauseBoxData(player);

            //暂存箱gui
            GermGuiScreen pauseBox = GermGuiScreen.getGermGuiScreen("TemporarilyBox", DisassembleManager.getTemporarilyBox());
            GermGuiCanvas canvas = (GermGuiCanvas) pauseBox.getGuiPart("canvas");
            GermGuiScroll scroll = (GermGuiScroll) canvas.getGuiPart("scroll");

            for (PauseBox p : pauseBoxData){
                String username = p.getUsername();
                int slotNum = p.getSlotnumber();
                int nums = p.getNums();
                String index = p.getProductindex();
                String guiname = p.getGuiname();

                String slot = "slot_"+slotNum;
                GermGuiSlot germGuiSlot = (GermGuiSlot) scroll.getGuiPart(slot);
                ItemStack item = SrItemAPI.getItem(index, new HashMap<>(), null, true, true);


                if (item == null){
                    continue;
                }
                item.setAmount(nums);
                ItemMeta itemMeta = item.getItemMeta();
                itemMeta.setLore(p.getLores());
                item.setItemMeta(itemMeta);
                germGuiSlot.setItemStack(item);
            }

            //循环完毕之后封存
            TemporarilyBox.addGuiScreen(player,"TemporarilyBox",pauseBox);
        }
    }

    /**
     * 此方法仅仅用于保存暂存箱的数据
     */
    public static void savePauseBoxData(){
        for (String username : temporarilyBoxHashMap.keySet()){
            GermGuiScreen temporarilyBox = temporarilyBoxHashMap.get(username).get("TemporarilyBox");
            GermGuiCanvas canvas = (GermGuiCanvas) temporarilyBox.getGuiPart("canvas");
            GermGuiScroll scroll = (GermGuiScroll) canvas.getGuiPart("scroll");
            for (int i = 1 ; i <= 16 ; i++){
                String slot = "slot_"+i;
                GermGuiSlot germGuiSlot = (GermGuiSlot) scroll.getGuiPart(slot);
                ItemStack itemStack = germGuiSlot.getItemStack();
                if (itemStack.getType() != Material.AIR) {
                    SrItemHandler srItemHandler = ItemStackHelper.getSrItemHandler(itemStack);
                    String index = srItemHandler.getLore().get(0);
                    index = index.substring(1);
                    List<String> lore = itemStack.getItemMeta().getLore();
                    String lores = "";
                    for (String s : lore){
                        lores = lores+s+"|";
                    }
                    JdbcSqlClass.insertPauseBoxData(username,i,itemStack.getAmount(),index,"TemporarilyBox",lores);
                }
            }
        }
    }
}
