package cn.citycraft.AuthMe.converter;

import java.util.List;

import org.bukkit.command.CommandSender;

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

public class SqlToFlat implements Converter {

	public DataSource database;
	public AuthMe plugin;
	public CommandSender sender;

	public SqlToFlat(AuthMe plugin, CommandSender sender) {
		this.plugin = plugin;
		this.database = plugin.database;
		this.sender = sender;
	}

	@Override
	public void run() {
		try {
			FlatFile flat = new FlatFile();
			List<PlayerAuth> auths = database.getAllAuths();
			int i = 0;
			final int size = auths.size();
			for (PlayerAuth auth : auths) {
				flat.saveAuth(auth);
				i++;
				if ((i % 100) == 0) {
					sender.sendMessage("Conversion Status : " + i + " / " + size);
				}
			}
			sender.sendMessage("Successfully convert from SQL table to file auths.db");
		} catch (Exception ex) {
			ConsoleLogger.showError(ex.getMessage());
			Messages.getInstance().send(sender, "error");
		}
	}

}
