package com.fernandobarillas.mchorse.executors;

import com.fernandobarillas.mchorse.Constants;
import com.fernandobarillas.mchorse.helpers.MessageHelper;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

public class McHorseCommandExecutor implements CommandExecutor {

    private final static String PLUGIN_COMMAND_LONG_NAME = "horse";
    private final static String PLUGIN_COMMAND_SHORT_NAME = "h";
    private final Plugin mPlugin;

    public McHorseCommandExecutor(Plugin plugin) {
        mPlugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] arguments) {

        // All plugin commands can only be sent by players
        if (!(sender instanceof Player)) {
            sender.sendMessage(Constants.PLUGIN_NAME + " commands can only be run by a player");
            return true;
        }

        // Now we know that the sender was a player
        Player player = (Player) sender;
        String commandName = command.getName();

        if (commandName.equalsIgnoreCase(PLUGIN_COMMAND_LONG_NAME) || commandName.equalsIgnoreCase(PLUGIN_COMMAND_SHORT_NAME)) {
            if (arguments.length < 1) {
                MessageHelper.pluginMessage(player, "Not enough arguments");
                return false;
            }

            if (arguments.length > 2) {
                MessageHelper.pluginMessage(player, "Too many arguments");
                return false;
            }

            String commandArgument, otherPlayerName;

            commandArgument = arguments[0];
            if (arguments.length == 2) {
                // TODO: Add permission for allowing players to add/remove other players
                otherPlayerName = arguments[1];
            }

            // Handle the plugin command arguments here to allow players to add, remove, etc. themselves from the queue
            // TODO: Add, remove, start, stop, score, players
            switch (commandArgument) {
                case "a":
                case "add":
                    MessageHelper.pluginMessage(player, "Adding yourself to the queue");
                    // TODO: Send an error message to the player if they're already in a queue
                    return true;
                case "r":
                case "remove":
                    MessageHelper.pluginMessage(player, "Removing yourself from the queue");
                    // TODO: Send an error message to the player if they're not in the queue
                    return true;
                default:
                    return false;
            }
        }

        return false;
    }
}

@@ -89,16 +89,61 @@
 stants;%0A
+import com.fernandobarillas.mchorse.McHorse;%0A
 import c
@@ -189,24 +189,25 @@
 sageHelper;%0A
+%0A
 import org.b
@@ -349,41 +349,8 @@
 yer;
-%0Aimport org.bukkit.plugin.Plugin;
 %0A%0Apu
@@ -481,16 +481,17 @@
 horse%22;%0A
+%0A
     priv
@@ -547,16 +547,17 @@
  = %22h%22;%0A
+%0A
     priv
@@ -566,22 +566,23 @@
 e final 
-Plugin
+McHorse
  mPlugin
@@ -618,22 +618,23 @@
 xecutor(
-Plugin
+McHorse
  plugin)
@@ -1238,32 +1238,49 @@
 ) %7C%7C commandName
+%0A                
 .equalsIgnoreCas
@@ -2098,32 +2098,86 @@
  score, players%0A
+            // TODO: Teleport to source block option?%0A
             swit
@@ -2253,16 +2253,80 @@
  %22add%22:%0A
+                    mPlugin.getPlayerQueue().addPlayer(player);%0A
         
@@ -2377,19 +2377,18 @@
 er, %22Add
-ing
+ed
  yoursel
@@ -2397,32 +2397,126 @@
 to the queue%22);%0A
+                    MessageHelper.pluginMessage(player, mPlugin.getPlayerQueue().toString());%0A
                 
@@ -2684,16 +2684,83 @@
 emove%22:%0A
+                    mPlugin.getPlayerQueue().removePlayer(player);%0A
         
@@ -2813,19 +2813,18 @@
 , %22Remov
-ing
+ed
  yoursel
@@ -2867,80 +2867,655 @@
     
-// TODO: Send an error message to the player if they're not in the queue
+MessageHelper.pluginMessage(player, mPlugin.getPlayerQueue().toString());%0A                    // TODO: Send an error message to the player if they're not in the queue%0A                    return true;%0A                case %22start%22:%0A                    break;%0A                case %22stop%22:%0A                    break;%0A                case %22p%22:%0A                case %22players%22:%0A                case %22s%22:%0A                case %22scores%22:%0A                    MessageHelper.pluginMessage(player, %22Player count: %22 + mPlugin.getPlayerQueue().getPlayerCount());%0A                    MessageHelper.pluginMessage(player, mPlugin.getPlayerQueue().getScoreStrings());
 %0A   

Add queue support to the command executor