#include "restconnection.h"
#include "restconnection_pimpl.h"

namespace Nxt {
namespace Internal {

const int restConnectionTimeoutDefault = 10;

RestConnectionParameters::RestConnectionParameters(QObject *parent) : QObject(parent)
  , m_timeout_sec(restConnectionTimeoutDefault)
  , m_authenticationType(AuthenticationTypePassword)
{
    m_url.setPort(ConnectionPort::ConnectionPortHttp);
}

RestConnectionParameters::RestConnectionParameters(const RestConnectionParameters &params, QObject *parent) : QObject(parent)
  , m_timeout_sec(restConnectionTimeoutDefault)
  , m_authenticationType(AuthenticationTypePassword)
{
    m_url.setPort(ConnectionPort::ConnectionPortHttp);
    m_url.setHost(params.host());
    m_url.setUserName(params.userName());
    m_url.setPassword(params.password());
    if (params.timeout() > 0)
        m_timeout_sec = params.timeout();
}

RestConnectionParameters::RestConnectionParameters(const QString &host, const quint16 &port, const QString &userName, const QString &password, const quint16 &timeout, QObject *parent) : QObject(parent)
  , m_timeout_sec(restConnectionTimeoutDefault)
  , m_authenticationType(AuthenticationTypePassword)
{
    m_url.setPort(ConnectionPort::ConnectionPortHttp);
    m_url.setHost(host);
    m_url.setUserName(userName);
    m_url.setPassword(password);
    if (timeout > 0)
        m_timeout_sec = timeout;
}

#ifdef NXT_LIBRARY
RestConnectionParameters::RestConnectionParameters(const QSsh::SshConnectionParameters &params, QObject *parent) : QObject(parent)
  , m_timeout_sec(restConnectionTimeoutDefault)
  , m_authenticationType(AuthenticationTypePassword)
{
    m_url.setPort(ConnectionPortHttp);
    m_url.setHost(params.host());
    m_url.setUserName(params.userName());
    m_url.setPassword(params.password());
    if (params.timeout > 0)
        m_timeout_sec = static_cast<quint16>(std::abs(params.timeout));
}
#endif

RestConnectionParameters::~RestConnectionParameters()
{
}

RestConnection::RestConnection(const RestConnectionParameters &params, QObject *parent) : QObject(parent)
  , m_params(params)
  , m_pimpl(new pimpl(this))
{
    connect(m_pimpl.get(), &RestConnection::pimpl::authenticated, this, &RestConnection::authenticated, Qt::QueuedConnection);
    connect(m_pimpl.get(), &RestConnection::pimpl::connected, this, &RestConnection::connected, Qt::QueuedConnection);
    connect(m_pimpl.get(), &RestConnection::pimpl::dataAvailable, this, &RestConnection::dataAvailable, Qt::QueuedConnection);
    connect(m_pimpl.get(), &RestConnection::pimpl::disconnected, this, &RestConnection::disconnected, Qt::QueuedConnection);
    connect(m_pimpl.get(), &RestConnection::pimpl::error, this, &RestConnection::error, Qt::QueuedConnection);
}

RestConnection::~RestConnection()
{
}

const RestConnectionParameters& RestConnection::connectionParameters() const
{
    return m_params;
}

void RestConnection::authenticate()
{
    m_pimpl->authenticate();
}

void RestConnection::connectToHost()
{
    m_pimpl->connectToHost();
}

void RestConnection::disconnectFromHost()
{
    m_pimpl->closeConnection(RestError::RestNoError, RestError::RestNoError, "", QString());
}

#if 0
RestConnection::State RestConnection::state() const
{

}

RestError RestConnection::errorState() const
{

}

QString RestConnection::errorString() const
{

}
#endif

} // namespace Internal
} // namespace Nxt
