package com.yaohun.storyline.gui;

import com.yaohun.storyline.Storyline;
import com.yaohun.storyline.config.Config;
import com.yaohun.storyline.core.Quest;
import com.yaohun.storyline.data.DialogueData;
import com.yaohun.storyline.data.PlayerData;
import com.yaohun.storyline.manage.PlayerManager;
import com.yaohun.storyline.manage.QuestManager;
import me.Demon.DemonPlugin.data.NbtItem;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

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

public class DialogueGui implements Listener {

    private static String invTitle = "§r§n§p§c§d§h§r";

    public static void OpenGui(Player player, Quest quest){
        Inventory inv = Bukkit.createInventory(null,18,invTitle+"#"+quest.getQuestID());
        DialogueData dialogueData = Config.getDialogueData(quest);
        if(dialogueData == null){
            System.out.println("[主线任务 - 错误] NPC对话任务出现错误 "+quest.getDisplayName());
            return;
        }
        ItemStack stack = new ItemStack(Material.PAPER);
        ItemMeta meta = stack.getItemMeta();
        meta.setDisplayName(dialogueData.getPlayerContent());
        stack.setItemMeta(meta);
        NbtItem nbtItem = new NbtItem(stack);
        nbtItem.setInteger("dialogueID",quest.getQuestID());
        inv.setItem(17,nbtItem.getItem());
        setNpcDialogueData(inv,dialogueData.getNpcName(),dialogueData.getNpcContent());
        player.openInventory(inv);
    }


    public static void OpenGuiTest(Player player,DialogueData data){
        Inventory inv = Bukkit.createInventory(null,18,invTitle+"#");
        ItemStack stack = new ItemStack(Material.PAPER);
        ItemMeta meta = stack.getItemMeta();
        meta.setDisplayName(data.getPlayerContent());
        stack.setItemMeta(meta);
        inv.setItem(17,stack);
        setNpcDialogueData(inv,data.getNpcName(),data.getNpcContent());
        player.openInventory(inv);
    }

    private static void setNpcDialogueData(Inventory inv,String npcName,List<String> strings){
        ItemStack item = new ItemStack(Material.PAPER);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(npcName);
        meta.setLore(strings);
        item.setItemMeta(meta);
        inv.setItem(0, item);
        int slot = 1;
        for (String string : strings){
            ItemStack stack = new ItemStack(Material.PAPER);
            ItemMeta meta2 = stack.getItemMeta();
            List<String> stringList = new ArrayList<>();
            stringList.add(string);
            meta2.setLore(stringList);
            stack.setItemMeta(meta2);
            inv.setItem(slot, stack);
            slot++;
        }
    }

    @EventHandler
    public void onClick(InventoryClickEvent event){
        int rawSlot = event.getRawSlot();
        Player player = (Player) event.getWhoClicked();
        String playerName = player.getName();
        if(event.getView().getTitle().contains(invTitle)){
            event.setCancelled(true);
            if(rawSlot == 17){
                ItemStack item = event.getCurrentItem();
                NbtItem nbtItem = new NbtItem(item);
                int questID = nbtItem.getInteger("dialogueID");
                Quest quest = QuestManager.getQuest(questID);
                if(quest == null){
                    System.out.println("# "+questID+" 任务丢失.");
                    return;
                }
                PlayerManager playerManager = Storyline.getPlayerManager();
                PlayerData playerData = playerManager.getPlayerData(playerName);
                playerData.forcCompleteQuest(quest);

                player.closeInventory();

                DialogueData dialogueData = Config.getDialogueData(quest);
                if(dialogueData != null) {
                    String title = dialogueData.getTitle();
                    String subtitle = dialogueData.getSubtitle();
                    if(title.contains("null")){
                        title = " ";
                    }
                    if(subtitle.contains("null")){
                        subtitle = " ";
                    }
                    player.sendTitle(title, subtitle, 15, 60, 15);
                }
            }
        }
    }
}
