package com.yaohun.storyline.config;

import com.yaohun.storyline.Storyline;
import com.yaohun.storyline.core.Quest;
import com.yaohun.storyline.data.DialogueData;
import com.yaohun.storyline.manage.QuestManager;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Config {

    public static boolean debug = false;
    private static String dataPath;
    private static HashMap<String,String> worldAlias = new HashMap<>();
    private static HashMap<String,String> questTypeAlias = new HashMap<>();
    private static HashMap<Quest, DialogueData> dialogueData = new HashMap<>();
    public static List<DialogueData> dialogueDataList = new ArrayList<>();

    public static void reloadConfig(Storyline plugin) {
        plugin.reloadConfig();
        plugin.saveConfig();
        FileConfiguration config = plugin.getConfig();
        dataPath = config.getString("DataPath","plugins/AuData/ShouLiQuest");
        loadWorldAliasData(config);
        loadQuestTypeAliasData(config);
        loadNpcDialogueData();
    }

    private static void loadWorldAliasData(FileConfiguration config) {
        ConfigurationSection section = config.getConfigurationSection("WorldAlias");
        if(section == null){
            return;
        }
        for (String world : section.getKeys(false)) {
            worldAlias.put(world,section.getString(world).replace("&","§"));
        }
    }

    private static void loadQuestTypeAliasData(FileConfiguration config) {
        ConfigurationSection section = config.getConfigurationSection("QuestType");
        if(section == null){
            return;
        }
        for (String questType : section.getKeys(false)) {
            String name = section.getString(questType);
            questTypeAlias.put(questType,name);
        }
    }

    private static void loadNpcDialogueData(){
        dialogueData.clear();
        Bukkit.getConsoleSender().sendMessage("§c[主线剧情] §f载入任务对话: ");
        File file = new File("plugins/QuestStoryline","Dialogue.yml");
        FileConfiguration configuration = YamlConfiguration.loadConfiguration(file);
        for (String idKey : configuration.getKeys(false)) {
            int questID = Integer.parseInt(idKey);
            Quest quest = QuestManager.getQuest(questID);
            if(quest == null){
                continue;
            }
            String name = configuration.getString(questID+".name");
            List<String> stringList = configuration.getStringList(questID+".npcContent");
            String player = configuration.getString(questID+".playerContent");
            String title = configuration.getString(questID+".title");
            String subtitle = configuration.getString(questID+".subTitle");
            dialogueData.put(quest,new DialogueData(questID,name,stringList,player,title,subtitle));
            Bukkit.getConsoleSender().sendMessage(" - QuestID: "+quest.getQuestID()+"  对话: "+name);
            dialogueDataList.add(new DialogueData(questID,name,stringList,player,title,subtitle));
        }
    }

    public static String getDataPath() {
        return dataPath;
    }

    public static String getWorldAlias(String world) {
        if(worldAlias.containsKey(world)){
            return worldAlias.get(world);
        }
        return "§c未知世界";
    }

    public static String getQuestTypeAlias(String questType) {
        if(questTypeAlias.containsKey(questType)){
            return questTypeAlias.get(questType);
        }
        return "§c未知类型";
    }

    public static DialogueData getDialogueData(Quest quest) {
        if(dialogueData.containsKey(quest)){
            return dialogueData.get(quest);
        }
        return null;
    }
}
