/*
 * EmergentMUD - A modern MUD with a procedurally generated world.
 * Copyright (C) 2016-2017 Peter Keeler
 *
 * This file is part of EmergentMUD.
 *
 * EmergentMUD is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * EmergentMUD is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.emergentmud.core.command;

import com.emergentmud.core.model.CommandMetadata;
import com.emergentmud.core.model.Entity;
import com.emergentmud.core.model.stomp.GameOutput;
import com.emergentmud.core.repository.CommandMetadataRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.inject.Inject;

@Component
public class HelpCommand extends BaseCommand {
    private ApplicationContext applicationContext;
    private CommandMetadataRepository commandMetadataRepository;

    @Inject
    public HelpCommand(ApplicationContext applicationContext,
                       CommandMetadataRepository commandMetadataRepository) {

        this.applicationContext = applicationContext;
        this.commandMetadataRepository = commandMetadataRepository;

        setDescription("Shows the documentation for a command.");
        addParameter("command", true);
    }

    @Override
    public GameOutput execute(GameOutput output, Entity entity, String command, String[] tokens, String raw) {
        if (tokens.length == 0) {
            usage(output, command);
        } else {
            CommandMetadata commandMetadata = commandMetadataRepository.findByName(tokens[0]);

            if (commandMetadata != null && (entity.isAdmin() || !commandMetadata.isAdmin())) {
                Command cmd = (Command)applicationContext.getBean(commandMetadata.getBeanName());

                cmd.usage(output, commandMetadata.getName());
            } else {
                output.append("There is no command by that name.");
            }
        }

        return output;
    }
}
@@ -1121,16 +1121,61 @@
 ontext;%0A
+import org.springframework.data.domain.Sort;%0A
 import o
@@ -1215,16 +1215,16 @@
 ponent;%0A
-
 %0Aimport 
@@ -1303,16 +1303,72 @@
 mmand %7B%0A
+    private static final Sort SORT = new Sort(%22name%22);%0A%0A
     priv
@@ -2035,37 +2035,719 @@
         
-usage(output, command
+output.append(%22%5Bdwhite%5D=== %5Bwhite%5DCommand Listing %5Bdwhite%5D===%22);%0A            output.append(%22%5Bwhite%5DType HELP %5Bdwhite%5D&lt;%5Bwhite%5Dcommand%5Bdwhite%5D&gt; %5Bwhite%5Dto get more detailed help for any of these commands.%22);%0A            output.append(%22%22);%0A%0A            commandMetadataRepository.findAll(SORT)%0A                    .stream()%0A                    .filter(cm -%3E !cm.isAdmin() %7C%7C entity.isAdmin())%0A                    .forEach(cm -%3E %7B%0A                        Command bean = (Command)applicationContext.getBean(cm.getBeanName());%0A%0A                        output.append(String.format(%22%5Bwhite%5D%25s %5Bdwhite%5D- %5Bwhite%5D%25s%22,%0A                                cm.getName(), bean.getDescription()));%0A                    %7D
 );%0A     

Add a list of commands that are available to you if you type HELP with no args.