package com.namelessmc.spigot.commands;

import java.util.Optional;

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

import com.namelessmc.java_api.NamelessException;
import com.namelessmc.java_api.NamelessUser;
import com.namelessmc.java_api.exception.AlreadyHasOpenReportException;
import com.namelessmc.java_api.exception.ReportUserBannedException;
import com.namelessmc.java_api.exception.UnableToCreateReportException;
import com.namelessmc.spigot.Config;
import com.namelessmc.spigot.Message;
import com.namelessmc.spigot.NamelessPlugin;
import com.namelessmc.spigot.Permission;

import xyz.derkades.derkutils.ListUtils;

public class ReportCommand extends Command {

	public ReportCommand() {
		super(Config.COMMANDS.getConfig().getString("report"),
				Message.COMMAND_REPORT_DESCRIPTION.getMessage(),
				Message.COMMAND_REPORT_USAGE.getMessage("command", Config.COMMANDS.getConfig().getString("report")),
				Permission.COMMAND_REPORT);
	}

	@Override
	public boolean execute(final CommandSender sender, final String[] args) {
		if (args.length < 2) {
			return false;
		}
		
		if (!(sender instanceof Player)) {
			sender.sendMessage(Message.COMMAND_NOTAPLAYER.getMessage());
			return false;
		}
		
		final Player player = (Player) sender;
		
		NamelessPlugin.getInstance().getServer().getScheduler().runTaskAsynchronously(NamelessPlugin.getInstance(), () -> {
			try {
				final String username = args[0];
				final String[] reasonWordsArray = ListUtils.removeFirstStringFromArray(args);
				final String reason = String.join(" ", reasonWordsArray); //Join with space in between words
				final Optional<NamelessUser> user = NamelessPlugin.getApi().getUser(player.getUniqueId());
				if (!user.isPresent()) {
					sender.sendMessage(Message.PLAYER_SELF_NOTREGISTERED.getMessage());
					return;
				}
				
				user.get().createReport(username, reason);
				sender.sendMessage(Message.COMMAND_REPORT_OUTPUT_SUCCESS.getMessage());
			} catch (final ReportUserBannedException e) {
				sender.sendMessage(Message.PLAYER_SELF_COMMAND_BANNED.getMessage());
			} catch (final AlreadyHasOpenReportException e) {
				sender.sendMessage(Message.COMMAND_REPORT_OUTPUT_FAIL_ALREADY_OPEN.getMessage());
			} catch (final UnableToCreateReportException e) {
				sender.sendMessage(Message.COMMAND_REPORT_OUTPUT_FAIL_GENERIC.getMessage());
			} catch (final NamelessException e) {
				sender.sendMessage(Message.COMMAND_REPORT_OUTPUT_FAIL_GENERIC.getMessage());
				e.printStackTrace();
				return;
			}
		});
		return true;
	}

}
@@ -1419,17 +1419,23 @@
  String 
-u
+targetU
 sername 
@@ -1465,29 +1465,34 @@
 ring
-%5B%5D
  reason
-WordsArray =
+ = String.join(%22 %22,
  Lis
@@ -1534,106 +1534,10 @@
 rgs)
+)
 ;
-%0A%09%09%09%09final String reason = String.join(%22 %22, reasonWordsArray); //Join with space in between words
 %0A%09%09%09
@@ -1754,16 +1754,236 @@
 %09%7D%0A%09%09%09%09%0A
+%09%09%09%09final Optional%3CNamelessUser%3E target = NamelessPlugin.getApi().getUser(targetUsername);%0A%09%09%09%09if (!target.isPresent()) %7B%0A%09%09%09%09%09sender.sendMessage(Message.PLAYER_OTHER_NOTREGISTERED.getMessage());%0A%09%09%09%09%09return;%0A%09%09%09%09%7D%0A%09%09%09%09%0A
 %09%09%09%09user
@@ -2006,16 +2006,20 @@
 ort(
-username
+target.get()
 , re

Update for new endpoint