/*
 * Copyright (c) 2016-2018 Daniel Ennis (Aikar) - MIT License
 *
 *  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 co.aikar.commands;

import org.jetbrains.annotations.NotNull;

public class CommandHelpFormatter {

    private final CommandManager manager;

    public CommandHelpFormatter(CommandManager manager) {
        this.manager = manager;
    }

    // ########
    // # help #
    // ########

    public void printHelpHeader(CommandHelp help, CommandIssuer issuer) {
        issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_HEADER, getHeaderFooterFormatReplacements(help));
    }

    public void printHelpEntry(CommandHelp help, CommandIssuer issuer, HelpEntry entry) {
        String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_FORMAT, getEntryFormatReplacements(help, entry));
        for (String msg : ACFPatterns.NEWLINE.split(formatted)) {
            issuer.sendMessageInternal(ACFUtil.rtrim(msg));
        }
    }

    public void printHelpFooter(CommandHelp help, CommandIssuer issuer) {
        if (help.isLastPage()) {
            return;
        }
        issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_PAGE_INFORMATION, getHeaderFooterFormatReplacements(help));
    }

    // ##########
    // # search #
    // ##########

    public void printSearchHeader(CommandHelp help, CommandIssuer issuer) {
        issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_SEARCH_HEADER, getHeaderFooterFormatReplacements(help));
    }

    public void printSearchEntry(CommandHelp help, CommandIssuer issuer, HelpEntry page) {
        String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_FORMAT, getEntryFormatReplacements(help, page));
        for (String msg : ACFPatterns.NEWLINE.split(formatted)) {
            issuer.sendMessageInternal(ACFUtil.rtrim(msg));
        }
    }

    public void printSearchFooter(CommandHelp help, CommandIssuer issuer) {
        if (help.isLastPage()) {
            return;
        }
        issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_PAGE_INFORMATION, getHeaderFooterFormatReplacements(help)
        );
    }


    // ############
    // # detailed #
    // ############

    public void printDetailedHelpHeader(CommandHelp help, CommandIssuer issuer, String command, HelpEntry entry) {
        issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_DETAILED_HEADER,
                "{command}", entry.getCommand(),
                "{command}", command
        );
    }

    public void printDetailedParameter(CommandHelp help, CommandIssuer issuer, HelpEntry entry, CommandParameter param) {
        String formattedMsg = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_DETAILED_PARAMETER_FORMAT, getParameterFormatReplacements(help, param.getName(), param.getDescription(), entry));
        for (String msg : ACFPatterns.NEWLINE.split(formattedMsg)) {
            issuer.sendMessageInternal(ACFUtil.rtrim(msg));
        }
    }

    public void printDetailedHelpFooter(CommandHelp help, CommandIssuer issuer, HelpEntry entry) {
        // default doesn't have a footer
    }

    /**
     * Override this to control replacements
     *
     * @param help
     * @return
     */
    public String[] getHeaderFooterFormatReplacements(CommandHelp help) {
        return new String[]{
                "{search}", help.search != null ? String.join(" ", help.search) : "",
                "{command}", help.getCommandName(),
                "{rootcommand}", help.getCommandName(),
                "{page}", "" + help.getPage(),
                "{totalpages}", "" + help.getTotalPages(),
                "{results}", "" + help.getTotalResults()
        };
    }

    /**
     * Override this to control replacements
     *
     * @param help
     * @param e
     * @return
     */
    public String[] getEntryFormatReplacements(CommandHelp help, HelpEntry e) {
        //{command} {parameters} {separator} {description}
        return new String[]{
                "{command}", e.getCommand(),
                "{rootcommand}", help.getCommandName(),
                "{parameters}", e.getParameterSyntax(),
                "{separator}", e.getDescription().isEmpty() ? "" : "-",
                "{description}", e.getDescription()
        };
    }

    /**
     * Override this to control replacements
     *
     * @param help
     * @param name
     * @param description
     * @param page
     * @return
     */
    @NotNull
    public String[] getParameterFormatReplacements(CommandHelp help, String name, String description, HelpEntry page) {
        //{name} {description}
        return new String[]{
                "{name}", name,
                "{description}", description,
                "{command}", page.getCommand(),
                "{rootcommand}", help.getCommandName()
        };
    }
}

@@ -3862,42 +3862,8 @@
 aram
-.getName(), param.getDescription()
 , en
@@ -5424,38 +5424,13 @@
 ram 
-name%0A     * @param description
+param
 %0A   
@@ -5554,39 +5554,30 @@
 lp, 
-String name, String description
+CommandParameter param
 , He
@@ -5678,20 +5678,103 @@
 name%7D%22, 
-name
+param.getName(),%0A                %22%7Bsyntax%7D%22, ACFUtil.nullDefault(param.getSyntax(), %22%22)
 ,%0A      
@@ -5800,17 +5800,46 @@
 tion%7D%22, 
-d
+ACFUtil.nullDefault(param.getD
 escripti
@@ -5840,16 +5840,23 @@
 cription
+(), %22%22)
 ,%0A      

add {syntax} replacement to param help