package com.blocklaunch.blwarps.commands;

import java.util.List;

import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.TextBuilder;
import org.spongepowered.api.text.Texts;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.util.command.CommandCallable;
import org.spongepowered.api.util.command.CommandException;
import org.spongepowered.api.util.command.CommandResult;
import org.spongepowered.api.util.command.CommandSource;

import com.blocklaunch.blwarps.BLWarps;
import com.blocklaunch.blwarps.Warp;
import com.blocklaunch.blwarps.manager.WarpManager;
import com.google.common.base.Optional;

public class ListWarpsCommand implements CommandCallable {

	private static final Text USAGE = Texts.of("/listwarps [page number]");
	private static final Text HELP = Texts
			.of("Lists all warps, split up into pages. Optionally, specify a page number as an argument");
	private static final Text SHORT_DESC = Texts.of("List warps");

	private static final Text ERROR_PARSING_NUMBER_MSG = Texts.of(TextColors.RED, BLWarps.PREFIX
			+ " There was an error parsing the page number.");

	private static final int WARPS_PER_PAGE = 10;

	@Override
	public List<String> getSuggestions(CommandSource source, String arguments) throws CommandException {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * Gets the currently loaded warps, paginates them into pages of size
	 * WARPS_PER_PAGE, and sends the warp names in a message to the player
	 * 
	 * @param source
	 * @param arguments
	 * @param parents
	 * @return
	 * @throws CommandException
	 */
	@Override
	public Optional<CommandResult> process(CommandSource source, String arguments) throws CommandException {
		int pageNum = 1;

		// If there are arguments, parse them and get a page number
		if (!arguments.isEmpty()) {
			String[] args = arguments.split(" ");
			try {
				pageNum = Integer.parseInt(args[0]);
			} catch (Exception e) {
				source.sendMessage(ERROR_PARSING_NUMBER_MSG);
				return Optional.of(CommandResult.empty());
			}
		}

		// Minus 1 to convert from page number to page index
		int pageIndex = pageNum - 1;

		// This is only a double so that the Math.ceil behaves properly
		double numWarps = WarpManager.warps.size();

		int numPages = (int) Math.ceil(numWarps / WARPS_PER_PAGE);

		// 0-based index of where in the array to start listing the warps
		int warpStartIndex = pageIndex * WARPS_PER_PAGE;

		Warp[] warpArray = WarpManager.warps.toArray(new Warp[0]);

		// For lack of a better name, "listNumber" is the number preceding
		// the warp name in the ordered list
		int listNumber = warpStartIndex + 1;

		source.sendMessage(Texts.of(TextColors.BLUE, "Warps Page " + pageNum + "/" + numPages));
		TextBuilder textBuilder = Texts.builder();
		for (int index = warpStartIndex; index < warpStartIndex + WARPS_PER_PAGE; index++) {
			// Check if the array at the index exists to eliminate
			// ArrayIndexOutOfBoundExceptions
			if (warpArray.length <= index) {
				// element does not exist at that index
				break;
			}

			textBuilder.append(Texts.builder(listNumber + ". ").color(TextColors.RED)
					.append(Texts.builder(warpArray[index].getName()).color(TextColors.GOLD).build()).build());
			textBuilder.append(Texts.of("\n"));

			listNumber++;
		}
		source.sendMessage(textBuilder.build());

		return Optional.of(CommandResult.success());
	}

	@Override
	public Optional<Text> getHelp(CommandSource arg0) {
		return Optional.of(HELP);
	}

	@Override
	public Optional<Text> getShortDescription(CommandSource arg0) {
		return Optional.of(SHORT_DESC);
	}

	@Override
	public Text getUsage(CommandSource arg0) {
		return USAGE;
	}

	@Override
	public boolean testPermission(CommandSource source) {
		return false;
	}

}

@@ -519,45 +519,8 @@
 ps;%0A
-import com.blocklaunch.blwarps.Warp;%0A
 impo
@@ -563,24 +563,24 @@
 arpManager;%0A
+
 import com.g
@@ -1091,16 +1091,142 @@
 mber.%22);
+%0A%09private static final Text NO_WARPS_MSG = Texts.of(TextColors.GREEN, BLWarps.PREFIX%0A%09%09%09+ %22 There were no warps to display.%22);
 %0A%0A%09priva
@@ -1810,16 +1810,143 @@
 ption %7B%0A
+%09%09if (WarpManager.warps.isEmpty()) %7B%0A%09%09%09source.sendMessage(NO_WARPS_MSG);%0A%09%09%09return Optional.of(CommandResult.success());%0A%09%09%7D%0A%0A
 %09%09int pa
@@ -2660,70 +2660,8 @@
 E;%0A%0A
-%09%09Warp%5B%5D warpArray = WarpManager.warps.toArray(new Warp%5B0%5D);%0A%0A
 %09%09//
@@ -3091,21 +3091,16 @@
 e%0A%09%09%09// 
-Array
 IndexOut
@@ -3128,24 +3128,32 @@
 if (
-warpArray.length
+WarpManager.warps.size()
  %3C= 
@@ -3330,24 +3330,36 @@
 der(
-warpArray%5B
+WarpManager.warps.get(
 index
-%5D
+)
 .get
@@ -3397,16 +3397,22 @@
 build())
+%0A%09%09%09%09%09
 .build()

Remove unnecessary array copy of Warp list