#include "nxtresttargetrunner.h"
#include <device/hardware/nxthardwaredevicerest.h>
#include <libs/ssh/sshconnection.h>
#include <QDir>
#include <QThread>
namespace Nxt {
namespace Internal {

NxtRestTargetRunner::NxtRestTargetRunner(ProjectExplorer::RunControl *runControl)
    : ProjectExplorer::SimpleTargetRunner(runControl)
{
    setDisplayName("NxtRestTargetRunner");
    m_runnable = runControl->runnable();
    m_device = runControl->device();

    vappName = runControl->runConfiguration()->property("vappName").toString();
    targetName = runControl->runConfiguration()->property("targetName").toString();
    projectName = runControl->runConfiguration()->property("projectName").toString();
    remotePath = runControl->runConfiguration()->property("remotePath").toString();
}

void NxtRestTargetRunner::start()
{
    const auto ssh = m_device->sshParameters();

    appendMessage(ProjectExplorer::RunControl::tr("Starting VApp '%1' on NXT device '%2'...").arg(vappName, m_device.data()->displayName()), Utils::NormalMessageFormat);

    NxtHardwareDeviceREST nxt(ssh.host(), ssh.userName(), ssh.password());

    if (!nxt.available())
    {
        appendMessage(ProjectExplorer::RunControl::tr("NXT device '%1' is not available.").arg(m_device.data()->displayName()), Utils::ErrorMessageFormat);
        return;
    }

    if (nxt.authenticate() != 200)
    {
        appendMessage(ProjectExplorer::RunControl::tr("Authentication on NXT device '%1' failed.").arg(m_device.data()->displayName()), Utils::ErrorMessageFormat);
        return;
    }

    if (!nxt.startVApp(vappName))
    {
        appendMessage(ProjectExplorer::RunControl::tr("Failed to start VApp '%1' on NXT device '%2'.").arg(vappName, m_device.data()->displayName()), Utils::ErrorMessageFormat);
        return;
    }

    appendMessage(ProjectExplorer::RunControl::tr("VApp '%1' has been started on NXT device '%2'.").arg(vappName, m_device.data()->displayName()), Utils::NormalMessageFormat);
}

void NxtRestTargetRunner::stop()
{
#ifdef IDS_NXT_DEBUG
    appendMessage(ProjectExplorer::RunControl::tr("NxtRestTargetRunner::stop()"), Utils::ErrorMessageFormat);
#endif /* IDS_NXT_DEBUG */
}

void NxtRestTargetRunner::onProcessStarted()
{
#ifdef IDS_NXT_DEBUG
    appendMessage(ProjectExplorer::RunControl::tr("NxtRestTargetRunner::onProcessStarted()"), Utils::ErrorMessageFormat);
#endif /* IDS_NXT_DEBUG */
}

void NxtRestTargetRunner::onProcessFinished(int exitCode, QProcess::ExitStatus status)
{
    Q_UNUSED(exitCode);
    Q_UNUSED(status);

#ifdef IDS_NXT_DEBUG
    appendMessage(ProjectExplorer::RunControl::tr("NxtRestTargetRunner::onProcessFinished()"), Utils::ErrorMessageFormat);
#endif /* IDS_NXT_DEBUG */
}

void NxtRestTargetRunner::onProcessError(QProcess::ProcessError error)
{
    Q_UNUSED(error);

#ifdef IDS_NXT_DEBUG
    appendMessage(ProjectExplorer::RunControl::tr("NxtRestTargetRunner::onProcessError()"), Utils::ErrorMessageFormat);
#endif /* IDS_NXT_DEBUG */
}

ProjectExplorer::IDevice::ConstPtr NxtRestTargetRunner::device() const
{
    return m_device;
}

void NxtRestTargetRunner::setRunnable(const ProjectExplorer::Runnable &runnable)
{
    m_runnable = runnable;
}

void NxtRestTargetRunner::setDevice(const ProjectExplorer::IDevice::ConstPtr &device)
{
    m_device = device;
}

} // namespace Internal
} // namespace Nxt
