/****************************************************************************
**
** Copyright (C) 2012 - 2014 Jolla Ltd.
** Contact: http://jolla.com/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid Commercial Qt licenses may use this file in
** accordance with the Commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "nxtdeployconfiguration.h"

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

#include <coreplugin/idocument.h>
#include <projectexplorer/deploymentdataview.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/target.h>
#include <qmakeprojectmanager/qmakeproject.h>
#include <utils/qtcassert.h>

#include <QLabel>
#include <QVBoxLayout>

using namespace ProjectExplorer;
using namespace QmakeProjectManager;
using namespace RemoteLinux;

namespace Nxt {
namespace Internal {

namespace {
const int ADD_REMOVE_SPECIAL_STEPS_DELAY_MS = 1000;
} // anonymous namespace

NxtDeployConfiguration::NxtDeployConfiguration(Target *parent, Core::Id id,const QString& displayName)
    : DeployConfiguration(parent, id)
{
    setDisplayName(displayName);
    setDefaultDisplayName(displayName);

    connect(target()->project(), &Project::parsingFinished, this, &NxtDeployConfiguration::addRemoveSpecialSteps);
}

NamedWidget *NxtDeployConfiguration::createConfigWidget()
{
#if 0
    // visible table
    return new DeploymentDataView(target());
#else
    // hidden table
    return new NamedWidget();
#endif
}

void NxtDeployConfiguration::addRemoveSpecialSteps()
{
    // Avoid temporary changes
    m_addRemoveSpecialStepsTimer.start(ADD_REMOVE_SPECIAL_STEPS_DELAY_MS, this);
}

// Override to add specific deploy steps based on particular project configuration
void NxtDeployConfiguration::doAddRemoveSpecialSteps()
{
}

void NxtDeployConfiguration::timerEvent(QTimerEvent *event)
{
    if (event->timerId() == m_addRemoveSpecialStepsTimer.timerId())
    {
        m_addRemoveSpecialStepsTimer.stop();
        doAddRemoveSpecialSteps();
    }
    else
    {
        DeployConfiguration::timerEvent(event);
    }
}

void NxtDeployConfiguration::removeStep(BuildStepList *stepList, Core::Id stepId)
{
    for (int i = 0; i < stepList->count(); ++i)
    {
        if (stepList->at(i)->id() == stepId)
        {
            stepList->removeStep(i);
            break;
        }
    }
}

////////////////////////////////////////////////////////////////////////////////////////////

NxtRestDeployConfiguration::NxtRestDeployConfiguration(Target *parent)
    : NxtDeployConfiguration(parent, configurationId() ,displayName())
{
}

void NxtRestDeployConfiguration::initialize()
{
    NxtDeployConfiguration::initialize();

    stepList()->insertStep(0, new NxtPrepareTargetStep(stepList()));
    stepList()->insertStep(1, new NxtRestDeployStep(stepList()));
}

QString NxtRestDeployConfiguration::displayName()
{
    return tr("NXT Deploy");
}

Core::Id NxtRestDeployConfiguration::configurationId()
{
    return Core::Id("QmakeProjectManager.NxtRestDeployConfiguration");
}

void NxtRestDeployConfiguration::doAddRemoveSpecialSteps()
{
}

} // namespace Internal
} // namespace Nxt
