/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qbs.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "depscanner.h"
#include "artifact.h"
#include "buildgraph.h"
#include "projectbuilddata.h"
#include "transformer.h"

#include <jsextensions/moduleproperties.h>
#include <language/language.h>
#include <language/propertymapinternal.h>
#include <language/resolvedfilecontext.h>
#include <language/scriptengine.h>
#include <logging/translator.h>
#include <plugins/scanner/scanner.h>
#include <tools/error.h>
#include <tools/fileinfo.h>
#include <tools/stringconstants.h>

#include <QtCore/qvariant.h>

#include <vector>

namespace qbs {
namespace Internal {

static QStringList collectProductBuildDirectories(const ResolvedProduct *product)
{
    QStringList buildDirectories;
    if (!product)
        return buildDirectories;

    buildDirectories << product->buildDirectory();
    // Add build directories of dependent products
    for (const auto &dep : product->dependencies) {
        if (dep.product)
            buildDirectories << dep.product->buildDirectory();
    }
    return buildDirectories;
}

DependencyScanner::DependencyScanner(
    ResolvedScannerPtr scanner, ScriptEngine *engine, ScannerPlugin *plugin)
    : m_scanner(std::move(scanner))
    , m_engine(engine)
    , m_global(engine->context(), JS_NewObjectProto(engine->context(), m_engine->globalObject()))
    , m_plugin(plugin)
{
    if (!m_plugin) {
        setupScriptEngineForFile(
            m_engine, m_scanner->scanScript.fileContext(), m_global, ObserveMode::Enabled);
    }
}

QString DependencyScanner::id() const
{
    if (m_id.isEmpty())
        m_id = createId();
    return m_id;
}

QStringList DependencyScanner::collectSearchPaths(Artifact *artifact)
{
    if (!m_plugin)
        return evaluate(artifact, nullptr, m_scanner->searchPathsScript);

    const QStringList buildDirectories = collectProductBuildDirectories(artifact->product.get());
    return m_plugin->collectSearchPaths(artifact->properties->value(), buildDirectories);
}

QStringList DependencyScanner::collectDependencies(
    Artifact *artifact, FileResourceBase *file, const char *fileTags)
{
    QStringList result;
    if (m_plugin)
        result = m_plugin->scan(file->filePath(), fileTags, artifact->properties->value());
    else
        result = evaluate(artifact, file, m_scanner->scanScript);
    result.removeDuplicates();
    return result;
}

bool DependencyScanner::recursive() const
{
    return m_scanner->recursive;
}

QString DependencyScanner::createId() const
{
    if (m_plugin)
        return m_plugin->name();
    return m_scanner->scanScript.sourceCode();
}

bool DependencyScanner::areModulePropertiesCompatible(
    const PropertyMapConstPtr &m1, const PropertyMapConstPtr &m2) const
{
    // This changes when our C++ scanner starts taking defines into account.
    if (m_plugin)
        return true;
    // TODO: This should probably be made more fine-grained. Perhaps the Scanner item
    //       could declare the relevant properties, or we could figure them out automatically
    //       somehow.
    return m1 == m2 || *m1 == *m2;
}

bool DependencyScanner::cacheIsPerFile() const
{
    return m_scanner->cacheIsPerFile;
}

class ScriptEngineActiveFlagGuard
{
    ScriptEngine *m_engine;
public:
    ScriptEngineActiveFlagGuard(ScriptEngine *engine)
        : m_engine(engine)
    {
        m_engine->setActive(true);
    }

    ~ScriptEngineActiveFlagGuard()
    {
        m_engine->setActive(false);
    }
};

QStringList DependencyScanner::evaluate(
    Artifact *artifact, const FileResourceBase *fileToScan, const PrivateScriptFunction &script)
{
    ScriptEngineActiveFlagGuard guard(m_engine);

    if (artifact->product.get() != m_product) {
        m_product = artifact->product.get();
        setupScriptEngineForProduct(
            m_engine, artifact->product.get(), m_scanner->module.get(), m_global, true);
    }

    JSValueList args;
    args.reserve(fileToScan ? 4 : 3);
    args.push_back(getJsProperty(m_engine->context(), m_global, StringConstants::projectVar()));
    args.push_back(getJsProperty(m_engine->context(), m_global, StringConstants::productVar()));
    args.push_back(Transformer::translateFileConfig(m_engine, artifact, m_scanner->module->name));
    if (fileToScan)
        args.push_back(makeJsString(m_engine->context(), fileToScan->filePath()));
    const ScopedJsValueList argsMgr(m_engine->context(), args);

    const TemporaryGlobalObjectSetter gos(m_engine, m_global);
    const JSValue function = script.getFunction(m_engine, Tr::tr("Invalid scan script."));
    const ScopedJsValue result(
        m_engine->context(),
        JS_Call(
            m_engine->context(),
            function,
            m_engine->globalObject(),
            int(args.size()),
            args.data()));

    m_engine->mergeAndClearTrackedScriptAccesses(*m_scanner->scriptAccesses);

    if (m_engine->checkForJsError(script.location())) {
        ErrorInfo err = m_engine->getAndClearJsError();
        err.prepend(Tr::tr("Error evaluating scan script"));
        throw err;
    }

    QStringList list;
    if (JS_IsArray(result)) {
        const int count = getJsIntProperty(
            m_engine->context(), result, StringConstants::lengthProperty());
        list.reserve(count);
        for (qint32 i = 0; i < count; ++i) {
            JSValue item = JS_GetPropertyUint32(m_engine->context(), result, i);
            if (!JS_IsUninitialized(item) && !JS_IsUndefined(item))
                list.push_back(getJsString(m_engine->context(), item));
            JS_FreeValue(m_engine->context(), item);
        }
    }
    return list;
}

} // namespace Internal
} // namespace qbs
