/****************************************************************************
**
** Copyright (C) 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 "nxtaddvmstartbuildstepprojectlistener.h"

#include <steps/build/nxtbuildsteps.h>

#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/buildsteplist.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/target.h>
#include <qmakeprojectmanager/qmakeproject.h>

using namespace Core;
using namespace ProjectExplorer;
using namespace QmakeProjectManager;

namespace Nxt {
namespace Internal {

NxtAddVmStartBuildStepProjectListener::NxtAddVmStartBuildStepProjectListener(QObject *parent)
    : NxtProjectListener(parent)
{
}

NxtAddVmStartBuildStepProjectListener::~NxtAddVmStartBuildStepProjectListener()
{
}

bool NxtAddVmStartBuildStepProjectListener::handleProject(QmakeProject *project)
{
    foreach (Target *target, project->targets()) {
        foreach (BuildConfiguration *bc, target->buildConfigurations()) {
            ensureHasVmStartStep(bc);
        }
    }

    return true;
}

bool NxtAddVmStartBuildStepProjectListener::forgetProject(Project *project)
{
    Q_UNUSED(project);
    return true;
}

void NxtAddVmStartBuildStepProjectListener::ensureHasVmStartStep(BuildConfiguration *bc)
{
    /*
     * position | id
     * ---------+-----------------------------------------
     *        0 | Nxt.NxtSdkStartStep
     *        1 | QtProjectManager.QMakeBuildStep
     *        2 | Qt4ProjectManager.MakeStep
     *        3 | Nxt.NxtBundleVisionAppStep
     */

    BuildStepList *buildSteps = bc->stepList(Core::Id(ProjectExplorer::Constants::BUILDSTEPS_BUILD));
    Q_ASSERT(buildSteps);

    /* insert steps if necessary */
    if (!buildSteps->contains(NxtSdkStartStep::stepId()))
        buildSteps->insertStep(0, new NxtSdkStartStep(buildSteps));
    if (!buildSteps->contains(NxtBundleStep::stepId()))
        buildSteps->insertStep(3, new NxtBundleStep(buildSteps));

    BuildStepList *cleanSteps = bc->stepList(Core::Id(ProjectExplorer::Constants::BUILDSTEPS_CLEAN));
    Q_ASSERT(cleanSteps);
    if (!cleanSteps->contains(NxtSdkStartStep::stepId()))
        cleanSteps->insertStep(0, new NxtSdkStartStep(cleanSteps));
}

} // namespace Internal
} // namespace Nxt
