/*
 * This file is part of the KDE Baloo Project
 * Copyright (C) 2015  Vishesh Handa <vhanda@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifndef BALOO_ENGINEQUERY_H
#define BALOO_ENGINEQUERY_H

#include "engine_export.h"

#include <QByteArray>
#include <QVector>
#include <QDebug>

namespace Baloo {

class BALOO_ENGINE_EXPORT EngineQuery
{
public:
    enum Operation {
        Equal,
        StartsWith,
        And,
        Or,
        Phrase
    };

    EngineQuery();
    EngineQuery(const QByteArray& term, Operation op = Equal);
    EngineQuery(const QVector<EngineQuery> &subQueries, Operation op = And);

    QByteArray term() const {
        return m_term;
    }

    Operation op() const {
        return m_op;
    }

    void setOp(const Operation& op) {
        m_op = op;
    }

    bool leaf() const {
        return !m_term.isEmpty();
    }

    bool empty() {
        return m_subQueries.isEmpty() && m_term.isEmpty();
    }

    QVector<EngineQuery> subQueries() const {
        return m_subQueries;
    }

    bool operator ==(const EngineQuery& q) const {
        return m_term == q.m_term && m_op == q.m_op && m_subQueries == q.m_subQueries;
    }
private:
    QByteArray m_term;
    Operation m_op;

    QVector<EngineQuery> m_subQueries;
};

inline QDebug operator<<(QDebug d, const Baloo::EngineQuery& q)
{
    QDebugStateSaver state(d);
    d.setAutoInsertSpaces(false);

    using Operation = Baloo::EngineQuery::Operation;
    if ((q.op() == Operation::Equal) || q.op() == Operation::StartsWith) {
        Q_ASSERT(q.subQueries().isEmpty());
        return d << q.term() << (q.op() == Operation::StartsWith ? ".." : "");
    }

    if (q.op() == Operation::And) {
        d << "[AND";
    } else if (q.op() == Operation::Or) {
        d << "[OR";
    } else if (q.op() == Operation::Phrase) {
        d << "[PHRASE";
    }
    for (auto &sq : q.subQueries()) {
        d << " " << sq;
    }
    return d << "]";
}

/**
 * Helper for QTest
 * \sa QTest::toString
 *
 * @since: 5.70
 */
inline char *toString(const EngineQuery& query)
{
    QString buffer;
    QDebug stream(&buffer);
    stream << query;
    return qstrdup(buffer.toUtf8().constData());
}

} // namespace Baloo
#endif
