/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** 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 "rmnxttargetoperation.h"
#include "addnxttargetoperation.h"
#include "addkeysoperation.h"
#include "rmkeysoperation.h"
#include "../nxtplugin/src/nxtconstants.h"
#include "../nxtplugin/src/qtc/nxttargetsxmlparser.h"
#include <iostream>
#include <QTextStream>
#include <QDir>

const char NXT_PARAM_NXT_TARGETS_DIR[] = "--nxt-targets-dir";
const char NXT_PARAM_TARGET_NAME[] = "--target-name";

RmNxtTargetOperation::RmNxtTargetOperation()
{
}

QString RmNxtTargetOperation::name() const
{
    return QLatin1String("removeNxtTarget");
}

QString RmNxtTargetOperation::helpText() const
{
    return QLatin1String("remove nxt target to Qt Creator configuration");
}

QString RmNxtTargetOperation::argumentsHelpText() const
{
    const QString indent = QLatin1String("    ");
    return indent + QLatin1String(NXT_PARAM_NXT_TARGETS_DIR) + QLatin1String(" <PATH>   shared \"targets\" folder (required).\n")
         + indent + QLatin1String(NXT_PARAM_TARGET_NAME) + QLatin1String(" <NAME>       display name (required).\n");
}

bool RmNxtTargetOperation::setArguments(const QStringList &args)
{
    for (int i = 0; i < args.count(); ++i) {
        const QString current = args.at(i);
        const QString next = ((i + 1) < args.count()) ? args.at(i + 1) : QString();

        if (current == QLatin1String(NXT_PARAM_NXT_TARGETS_DIR)) {
            if (next.isNull())
                return false;
            ++i; // skip next;
            m_targetsDir = next;
            continue;
        }

        if (current == QLatin1String(NXT_PARAM_TARGET_NAME)) {
            if (next.isNull())
                return false;
            ++i; // skip next;
            m_targetName = next;
            continue;
        }
    }
    return !m_targetsDir.isEmpty() && !m_targetName.isEmpty();
}


int RmNxtTargetOperation::execute() const
{
    QVariantMap map = AddNxtTargetOperation::load(m_targetsDir);
    if (map.isEmpty())
        map = AddNxtTargetOperation::initializeTargets();

    const QVariantMap result = removeTarget(map, m_targetName);

    if (result.isEmpty() || map == result)
        return 2;

    return AddNxtTargetOperation::save(result, m_targetsDir) ? 0 : 3;
}

QVariantMap RmNxtTargetOperation::removeTarget(const QVariantMap &map, const QString &targetName)
{
    QVariantMap result = AddNxtTargetOperation::initializeTargets();

    QVariantList targetList;
    QVariantList targetData = map.value(QLatin1String(Nxt::Constants::NXT_TARGET_KEY)).toList();
    foreach (const QVariant &data, targetData) {
        Nxt::NxtTargetData d = data.value<Nxt::NxtTargetData>();
        if ( d.name == targetName)
            continue;
        targetList << data;
    }

    if (targetList.count() == targetData.count()) {
        std::cerr << "Error: Target was not found." << std::endl;
        return map;
    }

    KeyValuePairList data;
    data << KeyValuePair(QStringList() << QLatin1String(Nxt::Constants::NXT_TARGET_KEY), targetList);
    return AddKeysOperation::addKeys(result, data);
}

#ifdef WITH_TESTS
bool RmNxtTargetOperation::test() const
{
    QFile file(QLatin1String("./test"));
    file.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream out(&file);
    out << "This file is generated by unit test\n";
    file.close();

    QVariantMap map = AddNxtTargetOperation::addTarget(AddNxtTargetOperation::initializeTargets(),
                                                       QLatin1String("target1"),
                                                       QLatin1String("./test"),
                                                       QLatin1String("./test"));


    map = AddNxtTargetOperation::addTarget(map,
                                           QLatin1String("target2"),
                                           QLatin1String("./test"),
                                           QLatin1String("./test"));
    if (map.count() != 2
            || !map.contains(QLatin1String(Nxt::Constants::NXT_TARGET_FILE_VERSION_KEY))
            || map.value(QLatin1String(Nxt::Constants::NXT_TARGET_FILE_VERSION_KEY)).toInt() != 2
            || !map.contains(QLatin1String(Nxt::Constants::NXT_TARGET_KEY)))
        return false;

    QVariantMap result = removeTarget(map, QLatin1String("target1"));
    if (result.count() != 2
            || !map.contains(QLatin1String(Nxt::Constants::NXT_TARGET_FILE_VERSION_KEY))
            || map.value(QLatin1String(Nxt::Constants::NXT_TARGET_FILE_VERSION_KEY)).toInt() != 2
            || !map.contains(QLatin1String(Nxt::Constants::NXT_TARGET_KEY)))
        return false;

    QVariantList targetData = result.value(QLatin1String(Nxt::Constants::NXT_TARGET_KEY)).toList();

    foreach (const QVariant &data, targetData) {
         Nxt::NxtTargetData d = data.value<Nxt::NxtTargetData>();
         if ( d.name == QLatin1String("target1"))
             return false;
    }

    result = removeTarget(map, QLatin1String("unknown"));
    if (result != map)
        return false;

    result = removeTarget(map, QLatin1String("target2"));
    if (result.count() != 2
            || !map.contains(QLatin1String(Nxt::Constants::NXT_TARGET_FILE_VERSION_KEY))
            || map.value(QLatin1String(Nxt::Constants::NXT_TARGET_FILE_VERSION_KEY)).toInt() != 2
            || !map.contains(QLatin1String(Nxt::Constants::NXT_TARGET_KEY)))
        return false;

    targetData = result.value(QLatin1String(Nxt::Constants::NXT_TARGET_KEY)).toList();
    foreach (const QVariant &data, targetData) {
         Nxt::NxtTargetData d = data.value<Nxt::NxtTargetData>();
         if ( d.name == QLatin1String("target2"))
             return false;
    }

    QDir().remove(file.fileName());

    return true;
}
#endif
