#include <Authenticator/PasswordAuthenticator/PasswordAuthenticator.hpp>
#include <Protocol/Protocol.hpp>

PasswordAuthenticator::PasswordAuthenticator(std::string userName, std::string password) :
    userName(userName),
    password(password)
{};

bool PasswordAuthenticator::sendGreetingPayload(SOCKET socket)
{
    // Send a LoginHeader.
    Protocol::LoginHeader loginHeader;
    loginHeader.authType = Protocol::AuthenticationType::Password;
    loginHeader.payloadSize = userName.size() + password.size() + 1;
    if (!writeSocket(socket, (char*) &loginHeader, sizeof(loginHeader)))
    {
        return false;
    };

    // Send the username length.
    uint8_t usernameLen = (uint8_t) userName.size();
    if (!writeSocket(socket, (char*) &usernameLen, 1))
    {
        return false;
    };

    // Send the user name.
    if (!writeSocket(socket, userName.c_str(), userName.size()))
    {
        return false;
    };

    // Send the password.
    if (!writeSocket(socket, password.c_str(), password.size()))
    {
        return false;
    };

    return true;
};

std::string PasswordAuthenticator::getUserName()
{
    return userName;
};