package cn.citycraft.AuthMe.converter;

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.SQLite;
import cn.citycraft.AuthMe.settings.Settings;

public class ForceFlatToSqlite implements Converter {

	private final DataSource data;

	public ForceFlatToSqlite(final DataSource data, final AuthMe plugin) {
		this.data = data;
	}

	@Override
	public void run() {
		DataSource sqlite = null;
		try {
			sqlite = new SQLite();
			for (final PlayerAuth auth : data.getAllAuths()) {
				auth.setRealName("Player");
				sqlite.saveAuth(auth);
			}
			Settings.setValue("DataSource.backend", "sqlite");
			ConsoleLogger.info("数据库已成功转换为 sqlite !");
		} catch (final Exception e) {
			ConsoleLogger.showError("一个错误发生了 当尝试将数据从 flatfile 转换为时 sqlite ...");
		} finally {
			if (sqlite != null) {
				sqlite.close();
			}
		}
	}
}
