package com.namelessmc.plugin.common.command;

import com.namelessmc.java_api.NamelessAPI;
import com.namelessmc.java_api.NamelessException;
import com.namelessmc.java_api.NamelessUser;
import com.namelessmc.java_api.exception.AccountAlreadyActivatedException;
import com.namelessmc.java_api.exception.InvalidValidateCodeException;
import com.namelessmc.plugin.common.CommonObjectsProvider;
import com.namelessmc.plugin.common.LanguageHandler.Term;
import com.namelessmc.plugin.spigot.NamelessPlugin;

import java.util.Optional;

public class VerifyCommand extends CommonCommand {

	public VerifyCommand(final CommonObjectsProvider provider) {
		super(provider);
	}

	@Override
	public void execute(final CommandSender sender, final String[] args, final String usage) {
		if (args.length != 1) {
			sender.sendLegacyMessage(usage);
			return;
		}

		if (!sender.isPlayer()) {
			sender.sendMessage(getLanguage().getComponent(Term.COMMAND_NOTAPLAYER));
			return;
		}

		NamelessPlugin.getInstance().getServer().getScheduler().runTaskAsynchronously(NamelessPlugin.getInstance(), () -> {
			final Optional<NamelessAPI> optApi = this.getApi();
			if (!optApi.isPresent()) {
				sender.sendMessage(getLanguage().getComponent(Term.COMMAND_NOTIFICATIONS_OUTPUT_FAIL));
				return;
			}
			final NamelessAPI api = optApi.get();

			try {
				final Optional<NamelessUser> user = NamelessPlugin.getInstance().getApiProvider().userFromPlayer(api, sender.getUniqueId(),sender.getName());
				if (!user.isPresent()) {
					sender.sendMessage(getLanguage().getComponent(Term.PLAYER_SELF_NOTREGISTERED));
					return;
				}

				final String code = args[0];
				user.get().verifyMinecraft(code);
				sender.sendMessage(getLanguage().getComponent(Term.COMMAND_VALIDATE_OUTPUT_SUCCESS));
			} catch (final InvalidValidateCodeException e) {
				sender.sendMessage(getLanguage().getComponent(Term.COMMAND_VALIDATE_OUTPUT_FAIL_INVALIDCODE));
			} catch (final AccountAlreadyActivatedException e) {
				sender.sendMessage(getLanguage().getComponent(Term.COMMAND_VALIDATE_OUTPUT_FAIL_ALREADYVALIDATED));
			} catch (final NamelessException e) {
				sender.sendMessage(getLanguage().getComponent(Term.COMMAND_VALIDATE_OUTPUT_FAIL_GENERIC));
				getExceptionLogger().logException(e);
			}
		});
	}

}

@@ -138,128 +138,8 @@
 on;%0A
-import com.namelessmc.java_api.NamelessUser;%0Aimport com.namelessmc.java_api.exception.AccountAlreadyActivatedException;%0A
 impo
@@ -1218,102 +1218,59 @@
 nal 
-Optional%3CNamelessUser%3E user = NamelessPlugin.getInstance().getApiProvider().userFromPlayer(api
+String code = args%5B0%5D;%0A%09%09%09%09api.verifyMinecraft(code
 , se
@@ -1288,16 +1288,17 @@
 queId(),
+ 
 sender.g
@@ -1312,213 +1312,8 @@
 ));%0A
-%09%09%09%09if (!user.isPresent()) %7B%0A%09%09%09%09%09sender.sendMessage(getLanguage().getComponent(Term.PLAYER_SELF_NOTREGISTERED));%0A%09%09%09%09%09return;%0A%09%09%09%09%7D%0A%0A%09%09%09%09final String code = args%5B0%5D;%0A%09%09%09%09user.get().verifyMinecraft(code);%0A
 %09%09%09%09
@@ -1553,168 +1553,8 @@
 ));%0A
-%09%09%09%7D catch (final AccountAlreadyActivatedException e) %7B%0A%09%09%09%09sender.sendMessage(getLanguage().getComponent(Term.COMMAND_VALIDATE_OUTPUT_FAIL_ALREADYVALIDATED));%0A
 %09%09%09%7D

Update to new API