package cn.citycraft.TellRaw;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;

/**
 * PluginHelper 通用配置文件类
 *
 * @author 喵♂呜
 */
public class PluginHelperConfig {
	public static YamlConfiguration a;
	public static File configfile = new File(Bukkit.getUpdateFolderFile().getParentFile(), "PluginHelper" + File.separatorChar + "config.yml");

	static {
		try {
			if (!configfile.exists()) {
				configfile.createNewFile();
			}
			a = YamlConfiguration.loadConfiguration(configfile);
			initFile(a);
		} catch (final IOException e) {
			a = new YamlConfiguration();
			try {
				initFile(a);
			} catch (final IOException e1) {
			}
		}
	}

	public static YamlConfiguration getConfig() {
		try {
			return YamlConfiguration.loadConfiguration(configfile);
		} catch (final Exception e) {
			return a;
		}
	}

	public static String getGUID() {
		return a.getString("guid");
	}

	private static void initFile(final YamlConfiguration config) throws IOException {
		if (config.getString("guid") == null) {
			config.options().header("数据中心 http://yum.citycraft.cn 收集的数据仅用于统计插件使用情况").copyDefaults(true);
			config.set("guid", UUID.randomUUID().toString());
			config.set("debug", false);
			config.save(configfile);
		}
		if (!config.contains("YumAccount")) {
			config.set("YumAccount.username", "Username Not Set");
			config.set("YumAccount.password", "Password NotSet");
			config.save(configfile);
		}
		if (!config.contains("TellrawManualHandle")) {
			config.set("TellrawManualHandle", false);
			config.save(configfile);
		}
	}
}
