#include <iostream>
#include <unordered_map>
#include <vector>

#include "server/commands/CommandManager.h"
#include "server/commands/CommandUtils.h"

static void commandTest(ServerCommands&, const std::vector<std::string>& args) {
    std::cout << "test command" << std::endl;
    for(size_t i = 0; i < args.size(); i++) {
        std::cout << " - " << args[i] << std::endl;
    }
}

static void commandStop(ServerCommands& sc, const std::vector<std::string>&) {
    sc.stop();
}

typedef void (*Command) (ServerCommands&, const std::vector<std::string>&);

static std::unordered_map<std::string, Command> commands( {
    {"test", commandTest},
    {"stop", commandStop}
});

void CommandManager::execute(ServerCommands& sc, const std::string& rawCommand) {
    std::vector<std::string> args;
    std::string command;
    if(CommandUtils::splitString(rawCommand, command, args)) {
        std::cout << "Invalid command syntax: '" << rawCommand << "'\n";
        return;
    }

    const std::unordered_map<std::string, Command>::const_iterator& iter = commands.find(command);
    if(iter == commands.end()) {
        std::cout << "Unknown command: '" << command << "'" << std::endl;
        return;
    }
    iter->second(sc, args);
}