package cn.citycraft.AuthMe.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import cn.citycraft.AuthMe.AuthMe;
import cn.citycraft.AuthMe.cache.auth.PlayerAuth;
import cn.citycraft.AuthMe.security.RandomString;
import cn.citycraft.AuthMe.settings.Messages;
import cn.citycraft.AuthMe.settings.Settings;

public class RegisterCommand implements CommandExecutor {

	public PlayerAuth auth;
	public AuthMe plugin;
	private Messages m = Messages.getInstance();

	public RegisterCommand(AuthMe plugin) {
		this.plugin = plugin;
	}

	@Override
	public boolean onCommand(CommandSender sender, Command cmnd, String label, String[] args) {
		if (!(sender instanceof Player)) {
			sender.sendMessage("Player Only! Use 'authme register <playername> <password>' instead");
			return true;
		}
		final Player player = (Player) sender;
		if (args.length == 0 || (Settings.getEnablePasswordVerifier && args.length < 2)) {
			m.send(player, "usage_reg");
			return true;
		}
		if (!plugin.authmePermissible(player, "authme." + label.toLowerCase())) {
			m.send(player, "no_perm");
			return true;
		}
		if (Settings.emailRegistration && !Settings.getmailAccount.isEmpty()) {
			if (Settings.doubleEmailCheck) {
				if (args.length < 2 || !args[0].equals(args[1])) {
					m.send(player, "usage_reg");
					return true;
				}
			}
			final String email = args[0];
			if (!Settings.isEmailCorrect(email)) {
				m.send(player, "email_invalid");
				return true;
			}
			RandomString rand = new RandomString(Settings.getRecoveryPassLength);
			final String thePass = rand.nextString();
			plugin.management.performRegister(player, thePass, email);
			return true;
		}
		if (args.length > 1 && Settings.getEnablePasswordVerifier)
			if (!args[0].equals(args[1])) {
				m.send(player, "password_error");
				return true;
			}
		plugin.management.performRegister(player, args[0], "");
		return true;
	}
}
