package tech.nevets.tvpn.cli.commands;

import tech.nevets.tvpn.cli.Command;
import tech.nevets.tvpn.cli.CommandBrigadier;

public class HelpCmd implements Command {
    private final CommandBrigadier cb;

    public HelpCmd(CommandBrigadier cb) {
        this.cb = cb;
    }

    @Override
    public void execute(String[] args) {
        if (args.length > 0) {
            for (Command c : cb.getCommands()) {
                if (!(args.length == 1) && args[1].equals(c.getName())) {
                    System.out.println("TVPN - Usage Menu [" + c.getName() + "]");
                    System.out.println(c.getName() + " " + c.getUsage() + ": " + c.getDescription());
                    System.exit(0);
                }
            }
        }

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

        System.out.println(sb);
        System.exit(0);
    }

    @Override
    public String getName() {
        return "help";
    }

    @Override
    public String getUsage() {
        return "";
    }

    @Override
    public String getDescription() {
        return "Displays the help menu";
    }
}
