package com.yaohun.storyline.manage;

import com.yaohun.storyline.Storyline;
import com.yaohun.storyline.core.Quest;
import com.yaohun.storyline.util.QuestType;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class QuestManager {

    private static List<Quest> questArrayList = new ArrayList<>();

    public static void init(Storyline plugin) {
        reloadQuestConfig(plugin); // 载入每日任务配置
    }

    public static void reloadQuestConfig(Storyline plugin) {
        questArrayList = new ArrayList<>();
        File file = new File(plugin.getDataFolder(), "Quest.yml");
        FileConfiguration config = YamlConfiguration.loadConfiguration(file);
        int count = 0;
        for (String questKey : config.getKeys(false)) {
            int questID = Integer.parseInt(questKey);
            String displayName = config.getString(questKey+".displayName");
            String description = config.getString(questKey+".description");
            QuestType questType = QuestType.valueOf(config.getString(questKey+".questType"));
            String targetName = config.getString(questKey+".target.name");
            int targetAmount = config.getInt(questKey+".target.amount");
            List<String> rewardList = config.getStringList(questKey+".rewards");
            List<String> commandList = config.getStringList(questKey+".finish");
            int nextQuestID = config.getInt(questKey+".nextQuestID");
            Location location = AnalyticalCoordinates(config.getString(questKey+".location"));
            List<String> introduce = config.getStringList(questKey+".introduce");
            questArrayList.add(new Quest(questID, displayName, description,questType, targetName, targetAmount, rewardList, commandList,nextQuestID,location,introduce));
            count++;
        }
        Bukkit.getConsoleSender().sendMessage("§e[手礼任务] §f载入主线任务 ➨ §a"+count+"个");
    }

    private static Location AnalyticalCoordinates(String string){
        String[] split = string.split(",");
        if(split.length == 6) {
            String worldName = split[0];
            double x = Double.parseDouble(split[1]);
            double y = Double.parseDouble(split[2]);
            double z = Double.parseDouble(split[3]);
            float yaw = Float.parseFloat(split[4]);
            float pitch = Float.parseFloat(split[5]);
            return new Location(Bukkit.getWorld(worldName), x, y, z, yaw, pitch);
        } else {
            return null;
        }
    }

    // 根据任务ID获取任务
    public static Quest getQuest(int id) {
        for (Quest quest : questArrayList) {
            if (quest.getQuestID() == id) {
                return quest;
            }
        }
        return null;
    }

    // 获取对应的任务列表
    public static List<Quest> getQuestArrayList() {
        return new ArrayList<>(questArrayList);
    }

}
