#include "GrantCommand.h"

#include "Game.h"

CrantCommand::CrantCommand()
    : ChatCommand("grant", "Grants a player a permission", 0)
{
    addParam(new PlayerNameParameter());
    addParam(new IntegerParameter("securityLavel",
        "The security level to grant",
        true,
        [](Entity* actor) {
            if (actor) return actor->getChatSecurityLevel();
            return INT32_MAX;
        }));
}

void CrantCommand::execute(
    Framework::RCArray<Framework::Text> params, Entity* zActor) const
{
    int securityLevel = (int)*params.z(1);
    Entity* zTarget = Game::INSTANCE->zPlayerByName(*params.z(0));
    if (zTarget)
    {
        zTarget->setChatSecurityLevel(securityLevel);
        Text message = "The security level of player ";
        message.append() << *params.z(0) << " is now set to " << securityLevel;
        Game::INSTANCE->zChat()->sendMessageTo(
            message, zActor, Chat::CHANNEL_INFO);
    }
}

int CrantCommand::getSecurityLevel(
    Framework::RCArray<Framework::Text> params) const
{
    return (int)*params.z(1);
}