#include "server/commands/Commands.h"
#include "server/commands/DefaultCommands.h"
#include "server/commands/SnuviCommands.h"
#include "utils/HashMap.h"
#include "utils/Logger.h"

static HashMap<Commands::Name, Commands::Command> commands;

void Commands::init() {
    DefaultCommands::init();
    SnuviCommands::init();
}

void Commands::add(const Name& name, Command command) {
    commands.add(name, command);
}

void Commands::execute(const Raw& raw) {
    Arguments args(raw);
    if(args.getLength() == 0) {
        LOG_INFO(StringBuffer<100>("invalid command syntax: '")
                     .append(raw)
                     .append("'"));
        return;
    }
    Name command(args[0]);
    Command* c = commands.search(command);
    if(c == nullptr) {
        SnuviCommands::callEvent(args);
        return;
    }
    (*c)(args);
}