/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pw.ian.albkit.command;

import pw.ian.albkit.command.parser.Arguments;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

/**
 *
 * @author ian
 */
public abstract class CommandHandler implements CommandExecutor {

    private final JavaPlugin plugin;

    private final String name;

    private String usage;

    private String description;

    private String permission;

    private boolean async;

    public CommandHandler(JavaPlugin plugin, String name) {
        this.plugin = plugin;
        this.name = name;
        usage = "/" + name;
        description = usage;
        permission = null;
        async = false;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the usage
     */
    public String getUsage() {
        return usage;
    }

    /**
     * @param usage the usage to set
     */
    public void setUsage(String usage) {
        this.usage = usage;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public boolean isAsync() {
        return async;
    }

    public void setAsync(boolean async) {
        this.async = async;
    }

    /**
     * Sends the usage message to the given CommandSender.
     *
     * @param sender
     */
    public void sendUsageMessage(CommandSender sender) {
        sender.sendMessage(ChatColor.RED + "Usage: " + usage);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmnd, String string, String[] args) {
        if (async) {
            executeAsync(sender, args);
        } else {
            this.onCommand(sender, new Arguments(args));
            this.onCommand(sender, args);
        }
        return true;
    }

    /**
     * Executes this command asynchronously.
     *
     * @param sender
     * @param args
     */
    private void executeAsync(final CommandSender sender, final String[] args) {
        (new BukkitRunnable() {
            @Override
            public void run() {
                CommandHandler.this.onCommand(sender, new Arguments(args));
                CommandHandler.this.onCommand(sender, args);
            }
        }).runTaskAsynchronously(plugin);
    }

    /**
     * Command handler method. Override this if you want to use a synchronous
     * command.
     *
     * @param sender
     * @param args
     */
    public void onCommand(final CommandSender sender, final Arguments args) {
    }

    /**
     * Command handler method. Override this if you want to use a synchronous
     * command.
     *
     * @param sender
     * @param args
     */
    public void onCommand(final CommandSender sender, final String[] args) {
    }
}

@@ -2344,65 +2344,8 @@
 e %7B%0A
-            this.onCommand(sender, new Arguments(args));%0A
     
@@ -2699,84 +2699,8 @@
 ) %7B%0A
-                CommandHandler.this.onCommand(sender, new Arguments(args));%0A
     
@@ -2861,71 +2861,8 @@
 hod.
- Override this if you want to use a synchronous%0A     * command.
 %0A   
@@ -2973,16 +2973,68 @@
 , final 
+String%5B%5D args) %7B%0A        this.onCommand(sender, new 
 Argument
@@ -3034,24 +3034,24 @@
 rguments
- 
+(
 args)
- %7B
+);
 %0A    %7D%0A%0A
@@ -3091,71 +3091,32 @@
 thod
-. Override this if you want to use a synchronous%0A     * command
+ using the Arguments API
 .%0A  
@@ -3224,32 +3224,33 @@
 nder, final 
-String%5B%5D
+Arguments
  args) %7B%0A   

Change how command handler works