/*
 * This file is part of Carbon, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2016, Jamie Mansfield <https://www.jamierocks.uk/>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package uk.jamierocks.mana.carbon.util.extra.command;

import com.google.common.collect.Lists;
import com.sk89q.intake.CommandCallable;
import com.sk89q.intake.CommandException;
import com.sk89q.intake.CommandMapping;
import com.sk89q.intake.Description;
import com.sk89q.intake.InvocationCommandException;
import com.sk89q.intake.argument.Namespace;
import com.sk89q.intake.util.auth.AuthorizationException;
import org.kitteh.irc.client.library.element.User;
import uk.jamierocks.mana.carbon.Carbon;
import uk.jamierocks.mana.carbon.util.Constants;
import uk.jamierocks.mana.carbon.util.intake.DescriptionBuilder;

import java.util.List;

/**
 * A help command for the help module.
 *
 * @author Jamie Mansfield
 * @since 1.1.0
 */
public final class HelpCommand implements CommandCallable {

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean call(String arguments, Namespace namespace, List<String> parentCommands)
            throws CommandException, InvocationCommandException, AuthorizationException {
        if (arguments != null && !arguments.equals("") &&
                Carbon.getCarbon().getCommandDispatcher().contains(arguments)) {
            StringBuilder builder = new StringBuilder();
            CommandMapping mapping = Carbon.getCarbon().getCommandDispatcher().get(arguments);

            builder.append("Command: ");
            builder.append(mapping.getPrimaryAlias());
            builder.append(" (");
            builder.append(mapping.getCallable().getDescription().getHelp());
            builder.append(") ");
            builder.append("Usage: ");
            builder.append(mapping.getDescription().getUsage());

            namespace.get(User.class).sendMessage(builder.toString());
        } else {
            StringBuilder builder = new StringBuilder();
            builder.append("Commands: ");

            for (CommandMapping mapping : Carbon.getCarbon().getCommandDispatcher().getCommands()) {
                if (mapping.getCallable().testPermission(namespace)) {
                    builder.append(mapping.getPrimaryAlias());
                    builder.append(" (");
                    builder.append(mapping.getCallable().getDescription().getHelp());
                    builder.append(") ");
                }
            }

            namespace.get(User.class).sendMessage(builder.toString());
        }

        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Description getDescription() {
        return new DescriptionBuilder()
                .help("Displays all commands, with their help text")
                .usage(Constants.COMMAND_PREFIX + "help [command]")
                .build();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean testPermission(Namespace namespace) {
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<String> getSuggestions(String arguments, Namespace locals) throws CommandException {
        return Lists.newArrayList();
    }
}

@@ -2298,19 +2298,19 @@
 uals(%22%22)
- &&
+) %7B
 %0A       
@@ -2314,20 +2314,20 @@
         
-    
+if (
 Carbon.g
@@ -2391,61 +2391,8 @@
     
-        StringBuilder builder = new StringBuilder();%0A
     
@@ -2482,17 +2482,161 @@
 ments);%0A
-%0A
+                if (mapping.getCallable().testPermission(namespace)) %7B%0A                    StringBuilder builder = new StringBuilder();%0A%0A        
         
@@ -2664,24 +2664,32 @@
 ommand: %22);%0A
+        
             
@@ -2735,32 +2735,40 @@
 ));%0A            
+        
 builder.append(%22
@@ -2765,32 +2765,40 @@
 r.append(%22 (%22);%0A
+        
             buil
@@ -2863,32 +2863,40 @@
 ));%0A            
+  
+      
 builder.append(%22
@@ -2905,32 +2905,40 @@
 %22);%0A            
+   
+     
 builder.append(%22
@@ -2952,32 +2952,40 @@
 %22);%0A            
+    
+    
 builder.append(m
@@ -3026,32 +3026,40 @@
 );%0A%0A            
+     
+   
 namespace.get(Us
@@ -3093,32 +3093,340 @@
 er.toString());%0A
+                %7D else %7B%0A                    namespace.get(User.class)%0A                            .sendMessage(%22You do not have permission to view the help for that command!%22);%0A                %7D%0A            %7D else %7B%0A                namespace.get(User.class).sendMessage(%22Command not found!%22);%0A            %7D%0A
         %7D else %7B

Check the permissions on the help command