/*
 * This file is part of GenericsLib for Bukkit, licensed under the MIT License (MIT).
 *
 * Copyright (c) JCThePants (www.jcwhatever.com)
 *
 * 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 com.jcwhatever.bukkit.generic.commands;

import com.jcwhatever.bukkit.generic.commands.arguments.CommandArguments;
import com.jcwhatever.bukkit.generic.commands.exceptions.InvalidValueException;
import com.jcwhatever.bukkit.generic.internal.Lang;
import com.jcwhatever.bukkit.generic.messaging.ChatPaginator;
import com.jcwhatever.bukkit.generic.permissions.Permissions;
import com.jcwhatever.bukkit.generic.utils.TextUtils;
import com.jcwhatever.bukkit.generic.utils.TextUtils.FormatTemplate;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionDefault;

import java.util.ArrayList;
import java.util.List;

@ICommandInfo(
        command={"help", "?"},
        staticParams={"page=1"},
        usage="/{command} help [page]",
        description="Show commands.",
        permissionDefault=PermissionDefault.TRUE,
        isHelpVisible=false)

public class HelpCommand extends AbstractCommand {

    @Override
    public void execute(CommandSender sender, CommandArguments args) throws InvalidValueException {

        int page = args.getInteger("page");

        showHelp(sender, page);
    }

    @Override
    public void showHelp(final CommandSender sender, int page) {

        String paginTitle = Lang.get("Commands");
        final ChatPaginator pagin = new ChatPaginator(_plugin, 6, paginTitle);

        final List<AbstractCommand> categories = new ArrayList<AbstractCommand>(getCommandHandler().getCommands().size());

        Permissions.runBatchOperation(true, new Runnable() {

            @Override
            public void run () {

                for (AbstractCommand cmd : getCommandHandler().getCommands()) {

                    if (cmd.getSubCommands().size() > 0) {
                        categories.add(cmd);
                        continue;
                    }

                    CommandInfoContainer info = cmd.getInfo();

                    if (!info.isHelpVisible())
                        continue;

                    if (sender instanceof Player && !Permissions.has((Player)sender, cmd.getPermission().getName()))
                        continue;

                    pagin.add(info.getUsage(), info.getDescription());
                }

                for (AbstractCommand cmd : categories) {

                    CommandInfoContainer info = cmd.getInfo();

                    if (!info.isHelpVisible())
                        continue;

                    if (sender instanceof Player && !Permissions.has((Player)sender, cmd.getPermission().getName()))
                        continue;

                    // format colors and command name
                    String helpText = TextUtils.format(
                            "{GOLD}/{plugin-command} {GREEN}{0} {GOLD}?", info.getCommandName());

                    // format plugin info
                    helpText = TextUtils.formatPluginInfo(_plugin, helpText);

                    pagin.add(helpText, info.getDescription());
                }

            }

        });

        pagin.show(sender, page, FormatTemplate.DEFINITION);
    }




}


@@ -1470,16 +1470,75 @@
 l.Lang;%0A
+import com.jcwhatever.bukkit.generic.language.Localizable;%0A
 import c
@@ -1591,16 +1591,16 @@
 inator;%0A
-
 import c
@@ -1657,62 +1657,8 @@
 ns;%0A
-import com.jcwhatever.bukkit.generic.utils.TextUtils;%0A
 impo
@@ -2184,24 +2184,185 @@
 tCommand %7B%0A%0A
+    @Localizable static final String _PAGINATOR_TITLE = %22Commands%22;%0A    @Localizable static final String _USAGE = %22%7BGOLD%7D/%7Bplugin-command%7D %7BGREEN%7D%7B0%7D %7BGOLD%7D?%22;%0A%0A
     @Overrid
@@ -2632,58 +2632,8 @@
  %7B%0A%0A
-        String paginTitle = Lang.get(%22Commands%22);%0A
     
@@ -2698,18 +2698,34 @@
  6, 
-paginTitle
+Lang.get(_PAGINATOR_TITLE)
 );%0A%0A
@@ -3976,356 +3976,66 @@
     
-// format colors and command name%0A                    String helpText = TextUtils.format(%0A                            %22%7BGOLD%7D/%7Bplugin-command%7D %7BGREEN%7D%7B0%7D %7BGOLD%7D?%22, info.getCommandName());%0A%0A                    // format plugin info%0A                    helpText = TextUtils.formatPluginInfo(_plugin, helpText);%0A%0A                    pagin.add(helpText
+pagin.add(Lang.get(_plugin, _USAGE, info.getCommandName())
 , in

localize strings