/****************************************************************************
**
** Copyright (C) 2012 - 2014 Jolla Ltd.
** Contact: http://jolla.com/
**
** This file is part of Qt Creator.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Digia.
**
****************************************************************************/

#include "nxtrunconfiguration.h"

#include <nxtconstants.h>
#include <steps/deploy/nxtdeployconfiguration.h>

#include <projectexplorer/kitinformation.h>
#include <projectexplorer/deployconfiguration.h>
#include <projectexplorer/deploymentdata.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/project.h>
#include <projectexplorer/runnables.h>
#include <projectexplorer/target.h>
#include <remotelinux/remotelinuxenvironmentaspect.h>

using namespace ProjectExplorer;
using namespace RemoteLinux;
using namespace Utils;

namespace Nxt {
namespace Internal {

NxtRunConfiguration::NxtRunConfiguration(Target *parent)
    : RemoteLinuxRunConfiguration(parent, Constants::NXT_RUNCONFIGURATION_PREFIX)
{
    connect(target(), &Target::activeDeployConfigurationChanged, this, &NxtRunConfiguration::updateEnabledState);
}

QString NxtRunConfiguration::disabledReason() const
{
    if (m_disabledReason.isEmpty())
        return RemoteLinuxRunConfiguration::disabledReason();
    else
        return m_disabledReason;
}

void NxtRunConfiguration::updateEnabledState()
{
    RemoteLinuxRunConfiguration::updateEnabledState();
}

QString NxtRunConfiguration::defaultRemoteExecutableFilePath() const
{
    DeployConfiguration* conf = target()->activeDeployConfiguration();
    if (!conf)
        return QString();

    QString executable = RemoteLinuxRunConfiguration::defaultRemoteExecutableFilePath();
    if (conf->id() == NxtRestDeployConfiguration::configurationId()) {
        QString projectName = target()->project()->displayName();
        return QLatin1String("/opt/sdk/") + projectName + executable;
    }

    return executable;
}

Runnable NxtRunConfiguration::runnable() const
{
    auto r = RemoteLinuxRunConfiguration::runnable().as<StandardRunnable>();
    // required by qtbase not to direct logs to journald
    // for Qt < 5.4
    r.environment.appendOrSet(QLatin1String("QT_NO_JOURNALD_LOG"), QLatin1String("1"));
    // for Qt >= 5.4
    r.environment.appendOrSet(QLatin1String("QT_LOGGING_TO_CONSOLE"), QLatin1String("1"));

    return r;
}

bool NxtRunConfiguration::fromMap(const QVariantMap &map)
{
    if (!RemoteLinuxRunConfiguration::fromMap(map))
        return false;

    setDefaultDisplayName(defaultDisplayName());
    return true;
}

QString NxtRunConfiguration::defaultDisplayName() const
{
    if (!buildSystemTarget().isEmpty())
        //: %1 is the name of a project which is being run on Nxt device
        return tr("%1 (on IDS NXT Device)").arg(buildSystemTarget());
    //: Nxt run configuration default display name
    return tr("Run on IDS NXT Device");
}

} // namespace Internal
} // namespace Nxt
