package cn.citycraft.AuthMe.commands;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

import cn.citycraft.AuthMe.AuthMe;
import cn.citycraft.AuthMe.converter.Converter;
import cn.citycraft.AuthMe.converter.CrazyLoginConverter;
import cn.citycraft.AuthMe.converter.FlatToSql;
import cn.citycraft.AuthMe.converter.FlatToSqlite;
import cn.citycraft.AuthMe.converter.RakamakConverter;
import cn.citycraft.AuthMe.converter.RoyalAuthConverter;
import cn.citycraft.AuthMe.converter.SqlToFlat;
import cn.citycraft.AuthMe.converter.vAuthConverter;
import cn.citycraft.AuthMe.settings.Messages;

public class ConverterCommand implements CommandExecutor {

	private final Messages m = Messages.getInstance();
	private final AuthMe plugin;

	public ConverterCommand(final AuthMe plugin) {
		this.plugin = plugin;
	}

	@Override
	public boolean onCommand(final CommandSender sender, final Command cmnd, final String label, final String[] args) {
		if (!plugin.authmePermissible(sender, "authme.admin.converter")) {
			m.send(sender, "no_perm");
			return true;
		}

		if (args.length == 0) {
			sender.sendMessage("Usage : /converter flattosql | flattosqlite | xauth | crazylogin | rakamak | royalauth | vauth | sqltoflat");
			return true;
		}

		final ConvertType type = ConvertType.fromName(args[0]);
		if (type == null) {
			m.send(sender, "error");
			return true;
		}
		Converter converter = null;
		switch (type) {
		case ftsql:
			converter = new FlatToSql();
			break;
		case ftsqlite:
			converter = new FlatToSqlite(sender);
			break;
		case crazylogin:
			converter = new CrazyLoginConverter(plugin, sender);
			break;
		case rakamak:
			converter = new RakamakConverter(plugin, sender);
			break;
		case royalauth:
			converter = new RoyalAuthConverter(plugin);
			break;
		case vauth:
			converter = new vAuthConverter(plugin, sender);
			break;
		case sqltoflat:
			converter = new SqlToFlat(plugin, sender);
			break;
		default:
			break;
		}
		if (converter == null) {
			m.send(sender, "error");
			return true;
		}
		Bukkit.getScheduler().runTaskAsynchronously(plugin, converter);
		sender.sendMessage("[AuthMe] Successfully converted from " + args[0]);
		return true;
	}

	public enum ConvertType {

		crazylogin("crazylogin"),
		ftsql("flattosql"),
		ftsqlite("flattosqlite"),
		rakamak("rakamak"),
		royalauth("royalauth"),
		sqltoflat("sqltoflat"),
		vauth("vauth"),
		xauth("xauth");

		String name;

		ConvertType(final String name) {
			this.name = name;
		}

		public static ConvertType fromName(final String name) {
			for (final ConvertType type : ConvertType.values()) {
				if (type.getName().equalsIgnoreCase(name)) {
					return type;
				}
			}
			return null;
		}

		String getName() {
			return this.name;
		}
	}
}
