package cn.citycraft.AuthMe.converter;

import java.io.File;

import org.bukkit.OfflinePlayer;

import cn.citycraft.AuthMe.AuthMe;
import cn.citycraft.AuthMe.ConsoleLogger;
import cn.citycraft.AuthMe.cache.auth.PlayerAuth;
import cn.citycraft.AuthMe.datasource.DataSource;

public class RoyalAuthConverter implements Converter {

	public AuthMe plugin;
	private DataSource data;

	public RoyalAuthConverter(AuthMe plugin) {
		this.plugin = plugin;
		this.data = plugin.database;
	}

	@Override
	public void run() {
		for (OfflinePlayer o : plugin.getServer().getOfflinePlayers()) {
			try {
				String name = o.getName().toLowerCase();
				String sp = File.separator;
				File file = new File("." + sp + "plugins" + sp + "RoyalAuth" + sp + "userdata" + sp + name + ".yml");
				if (data.isAuthAvailable(name))
					continue;
				if (!file.exists())
					continue;
				RoyalAuthYamlReader ra = new RoyalAuthYamlReader(file);
				PlayerAuth auth = new PlayerAuth(name, ra.getHash(), "127.0.0.1", ra.getLastLogin(), "your@email.com", o.getName());
				data.saveAuth(auth);
			} catch (Exception e) {
				ConsoleLogger.writeStackTrace(e);
				ConsoleLogger.showError("Error while trying to import " + o.getName() + " RoyalAuth datas");
			}
		}
	}

}
