package com.yaohun.storyline.quest;

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.gui.DialogueGui;
import com.yaohun.storyline.manage.PlayerManager;
import com.yaohun.storyline.manage.QuestManager;
import com.yaohun.storyline.util.MessageUtil;
import com.yaohun.storyline.util.QuestType;
import me.Demon.DemonPlugin.DemonAPI;
import me.Demon.DemonPlugin.Util.AreaType;
import me.messageofdeath.commandnpc.event.NpcInteractEvent;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;

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

public class NpcInteractListener implements Listener {

    @EventHandler
    public void onNpcInteract(NpcInteractEvent event) {
        Player player = event.getPlayer();
        String playerName = player.getName();

        PlayerManager playerManager = Storyline.getPlayerManager();
        PlayerData playerData = playerManager.getPlayerData(playerName);

        if(playerData == null) {
            return;
        }

        // 玩家若已完成所有任务则直接返回
        if(playerData.isAllCompleted()){
            return;
        }
        int currentQuestID = playerData.getCurrentQuest();
        // 当前正在进行的任务id小于15时拦截
        if(DemonAPI.getAreaType() == AreaType.DEFAULT) {
            if (event.getNpcName().contains("器魂觉醒台")) {
                if (currentQuestID < 15) {
                    event.setCancelled(true);
                    player.sendTitle("§f[§c⚠警告⚠§f]", "§a请先与§e[冥洛]§a进行对话了解器魂觉醒", 10, 40, 20);
                    MessageUtil.sendMessage(player, "§f[§c主线§f] §a请先与§e[冥洛]§a进行对话了解器魂觉醒。", Sound.ENTITY_VILLAGER_NO);
                    return;
                }
            }
            if (event.getNpcName().contains("竹离")) {
                if(currentQuestID < 16) {
                    event.setCancelled(true);
                    player.sendTitle("§f[§c⚠警告⚠§f]", "§a你需要领取任务[§e来自福利官的激励§a]才能领取", 10, 40, 20);
                    MessageUtil.sendMessage(player, "§f[§c主线§f] §a你需要领取任务[§e来自福利官的激励§a]才能领取。", Sound.ENTITY_VILLAGER_NO);
                    return;
                }
            }
        } else {
            if (event.getNpcName().contains("器魂覺醒台")) {
                if (currentQuestID < 15) {
                    event.setCancelled(true);
                    player.sendTitle("§f[§c⚠警告⚠§f]", "§a請先與§e[冥洛]§a進行對話了解器魂覺醒", 10, 40, 20);
                    MessageUtil.sendMessage(player, "§f[§c主線§f] §a請先與§e[冥洛]§a進行對話了解器魂覺醒。", Sound.ENTITY_VILLAGER_NO);
                    return;
                }
            }
            if(event.getNpcName().contains("竹離")){
                if(currentQuestID < 16) {
                    event.setCancelled(true);
                    player.sendTitle("§f[§c⚠警告⚠§f]", "§a你需要領取任務[§e來自福利官的激勵§a]才能領取", 10, 40, 20);
                    MessageUtil.sendMessage(player, "§f[§c主線§f] §a你需要領取任務[§e來自福利官的激勵§a]才能領取。", Sound.ENTITY_VILLAGER_NO);
                    return;
                }
            }
        }
        Quest quest = QuestManager.getQuest(currentQuestID);
        if(quest != null) {
            if (quest.getQuestType() == QuestType.DUI_HUA_NAME) {
                String npcName = event.getNpcName();
                if (npcName.contains(quest.getTargetName())) {
                    DialogueGui.OpenGui(player, quest);
                    event.setCancelled(true);
                }
            }
        }
    }


    @EventHandler
    public void onNpcInteractTest(NpcInteractEvent event) {
        Player player = event.getPlayer();
        String playerName = player.getName();
        if(Config.debug){
            List<DialogueData> dataList = Config.dialogueDataList;
            for (DialogueData dialogueData : dataList) {
                String name = dialogueData.getNpcName().replaceAll("§[0-9a-fk-or]", "");;
                if(event.getNpcName().contains(name)){
                    DialogueGui.OpenGuiTest(player,dialogueData);
                    event.setCancelled(true);
                }
            }
        }
    }
}
