package tech.nevets.tvpn.cli.commands

import tech.nevets.tvpn.cli.Command
import tech.nevets.tvpn.cli.CommandBrigadier
import kotlin.system.exitProcess

class HelpCmd(private val cb: CommandBrigadier) : Command {

    override fun execute(args: Array<String>) {
        if (args.isNotEmpty()) {
            for (c in cb.getCommands()) {
                if (args.size != 1 && args[1] == c.getName()) {
                    println(("TVPN - Usage Menu [" + c.getName()) + "]")
                    println((c.getName() + " " + c.getUsage()) + ": " + c.getDescription())
                    exitProcess(0)
                }
            }
        }

        val sb = StringBuilder()
        sb.append("TVPN - Help Menu\n")
        for (c in cb.getCommands()) {
            sb.append(" - ").append(c.getName())
                .append(" ").append(c.getUsage())
                .append(": ").append(c.getDescription())
                .append("\n")
        }

        println(sb)
        exitProcess(0)
    }

    override fun getName(): String {
        return "help"
    }

    override fun getUsage(): String {
        return ""
    }

    override fun getDescription(): String {
        return "Displays the help menu"
    }

}