package tech.nevets.tvpn.cli;

import tech.nevets.tvpn.Logger;
import tech.nevets.tvpn.cli.commands.HelpCmd;
import tech.nevets.tvpn.cli.commands.InstallTunnelServiceCmd;
import tech.nevets.tvpn.cli.commands.UninstallTunnelServiceCmd;

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

public class CommandBrigadier {
    private static final Logger LOGGER = new Logger(CommandBrigadier.class);

    private final List<Command> registeredCommands;
    private final HelpCmd helpCmd;

    public CommandBrigadier() {
        this.registeredCommands = new ArrayList<>();
        helpCmd = new HelpCmd(this);

        registerCommand(helpCmd);
        registerCommand(new InstallTunnelServiceCmd());
        registerCommand(new UninstallTunnelServiceCmd());
    }

    public void registerCommand(Command cmd) {
        for (Command c : registeredCommands) {
            if (c.getName().equals(cmd.getName())) {
                LOGGER.warn("Command with name " + cmd.getName() + " already registered, skipping.");
                return;
            }
        }
        registeredCommands.add(cmd);
    }

    public void execute(String[] args) {
        for (Command c : registeredCommands) {
            if (c.getName().equals(args[0])) {
                c.execute(args);
                return;
            }
        }
        helpCmd.execute(new String[0]);
    }

    public List<Command> getCommands() {
        return registeredCommands;
    }
}
