/****************************************************************************
**
** 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 "nxtprojectlistener.h"

#include <qtc/nxtsdkmanager.h>

#include <coreplugin/icore.h>
#include <coreplugin/progressmanager/futureprogress.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
#include <projectexplorer/target.h>
#include <qmakeprojectmanager/qmakenodes.h>
#include <qmakeprojectmanager/qmakeproject.h>
#include <qtsupport/qtkitinformation.h>
#include <utils/fileutils.h>

using namespace ProjectExplorer;
using namespace QtSupport;
using namespace QmakeProjectManager;

namespace Nxt {
namespace Internal {

NxtProjectListener::NxtProjectListener(QObject *parent)
    : QObject(parent)
{
    connect(NxtSdkManager::instance(), &NxtSdkManager::initialized, this, &NxtProjectListener::init);
}

void NxtProjectListener::init()
{
    // This is called just once after the NxtSdkManager is initialized.
    const QList<Project*> projects = SessionManager::projects();
    // O(n2)
    foreach (Project *project, projects)
        onProjectAdded(project);

    connect(SessionManager::instance(), &SessionManager::projectAdded, this, &NxtProjectListener::onProjectAdded);
    connect(SessionManager::instance(), &SessionManager::projectRemoved, this, &NxtProjectListener::onProjectRemoved);
}

void NxtProjectListener::onTargetAddedToProject(Target *target)
{
    Project *project = target->project();
    if (NxtSdkManager::nxtKits().contains(target->kit()))
        handleProject_private(project);
}

void NxtProjectListener::onTargetRemovedFromProject(Target *target)
{
    Project *project = target->project();
    const QList<Kit *> nxtKits = NxtSdkManager::nxtKits();
    int nxtTargetsCount = 0;
    // Counting how many Nxt Targets are left after this removal
    foreach (Target *projectTarget, project->targets())
        if (nxtKits.contains(projectTarget->kit()))
            nxtTargetsCount++;
    if (nxtTargetsCount == 0)
        forgetProject(project);
}

void NxtProjectListener::onProjectAdded(Project *project)
{
    connect(project, &Project::addedTarget, this, &NxtProjectListener::onTargetAddedToProject);
    connect(project, &Project::removedTarget, this, &NxtProjectListener::onTargetRemovedFromProject);

    const QList<Kit *> nxtKits = NxtSdkManager::nxtKits();
    foreach (Kit *kit, nxtKits) {
        if (project->supportsKit(kit)) {
            if (handleProject_private(project))
                break;
        }
    }
}

void NxtProjectListener::onProjectRemoved(Project *project)
{
    forgetProject(project);
}

bool NxtProjectListener::handleProject_private(Project *project)
{
    QmakeProject *qmakeProject = qobject_cast<QmakeProject*>(project);
    if (!qmakeProject)
        return false;
    return handleProject(qmakeProject);
}

} // namespace Internal
} // namespace Nxt
