/*
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA  02110-1301, USA.

    ---
    Copyright (C) 2009 Alexander Rieder <alexanderrieder@gmail.com>
 */

#include "backendtest.h"

#include "backend.h"
#include "session.h"
#include "../../config-cantor.h"

#include <KLocalizedString>
#include <QSignalSpy>
#include <QDebug>

void BackendTest::createSession()
{
    // Add our custom plugins path, where we install our plugins, if it isn't default path
    const QString& path = QString::fromLocal8Bit(PATH_TO_CANTOR_PLUGINS);
    qDebug() << "Adding additional application library path" << path;
    if (!QCoreApplication::libraryPaths().contains(path))
        QCoreApplication::addLibraryPath(path);

    Cantor::Backend* b=Cantor::Backend::getBackend( backendName() );
    if(!b || !b->requirementsFullfilled() )
    {
        m_session = nullptr;
        return;
    }

    m_session=b->createSession();

    QSignalSpy spy(m_session, SIGNAL(loginDone()) );
    m_session->login();
    if(spy.isEmpty())
        waitForSignal(m_session, SIGNAL(loginDone()) );

    QVERIFY(!spy.isEmpty());
}

Cantor::Expression* BackendTest::evalExp(const QString& exp )
{
   Cantor::Expression* e=m_session->evaluateExpression(exp);

   if(e->status()==Cantor::Expression::Queued)
   {
       waitForSignal( e, SIGNAL(statusChanged(Cantor::Expression::Status)) );
   }

   if (e->status()==Cantor::Expression::Computing)
   {
       waitForSignal( e, SIGNAL(statusChanged(Cantor::Expression::Status)) );
   }
   return e;
}

QString BackendTest::cleanOutput(const QString& out)
{
    QString cleaned=out;
    cleaned.replace( QLatin1String("&nbsp;"),QLatin1String(" ") );
    cleaned.remove( QLatin1String("<br/>") );
    cleaned.replace( QChar::ParagraphSeparator, QLatin1Char('\n') );
    cleaned.replace( QRegularExpression( QStringLiteral("\\n{2}") ), QStringLiteral("\n") );
    cleaned.replace( QRegularExpression( QStringLiteral("\\n\\s*") ), QStringLiteral("\n") );

    return cleaned.trimmed();
}

void BackendTest::initTestCase()
{
    QCoreApplication::setApplicationName(QLatin1String("cantor"));
    createSession();
    if (!m_session)
    {
        QString reason = i18n("This test requires a functioning %1 backend", backendName() );
        QSKIP( reason.toStdString().c_str(), SkipAll );
    }
}

void BackendTest::cleanupTestCase()
{
    if (m_session)
    {
        m_session->logout();
    }
}

Cantor::Session* BackendTest::session()
{
    return m_session;
}

void BackendTest::waitForSignal(QObject* sender, const char* signal)
{
    QTimer timeout( this );
    timeout.setSingleShot( true );

    QEventLoop loop;
    connect( sender, signal, &loop, SLOT(quit()) );
    connect(&timeout, &QTimer::timeout, &loop, &QEventLoop::quit);
    timeout.start( 25000 );
    loop.exec();
}



