package com.yaohun.storyline.util;

import com.yaohun.storyline.Storyline;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import java.io.File;
import java.util.HashMap;

public class MessageUtil {

    private static HashMap<String,String> messageMap = new HashMap<>();

    public static void init(Storyline plugin) {
        messageMap.clear();
        File file = new File(plugin.getDataFolder(),"Language.yml");
        // 如果没有这个文件就新建一个~
        if(!file.exists()){
            plugin.saveResource("Language.yml",false);
        }
        FileConfiguration yml = YamlConfiguration.loadConfiguration(file);
        for (String ymlKey : yml.getKeys(false)){
            ConfigurationSection section = yml.getConfigurationSection(ymlKey);
            for (String key : section.getKeys(false)){
                String message = section.getString(key);
                messageMap.put(key,message.replace("&","§"));
            }
        }
        Bukkit.getConsoleSender().sendMessage("[手礼任务] 已载入语言配置 "+messageMap.size()+" 条");
    }

    public static String getMessage(String key){
        if(messageMap.containsKey(key)){
            return messageMap.get(key);
        }
        return "§c未找到节点.§7#"+key;
    }

    public static void broadcast(String msg) {
        Bukkit.getServer().broadcastMessage( msg.replace("&","§"));
    }

    public static void sendMessage(CommandSender sender, String message){
        sender.sendMessage(message);
    }

    public static void sendMessage(CommandSender sender, String message, Sound sound){
        sender.sendMessage(message);
        if(sender instanceof Player){
            Player player = (Player) sender;
            player.playSound(player.getLocation(),sound,1,1);
        }
    }
}
