/***************************************************************************
 *   Copyright (C) 2008 by S. MANKOWSKI / G. DE BURE support@mankowski.fr  *
 *                                                                         *
 *   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, see <http://www.gnu.org/licenses/>  *
 ***************************************************************************/
/** @file
 * A skrooge plugin to track operations
 *
 * @author Stephane MANKOWSKI
 */
#include "skgtrackerplugin.h"

#include <KActionCollection>
#include <KStandardAction>
#include <kaboutdata.h>
#include <kpluginfactory.h>

#include "skgtrackerpluginwidget.h"
#include "skgtracker_settings.h"
#include "skgtraces.h"
#include "skgdocumentbank.h"

/**
 * This plugin factory.
 */
K_PLUGIN_FACTORY(SKGTrackerPluginFactory, registerPlugin<SKGTrackerPlugin>();)

SKGTrackerPlugin::SKGTrackerPlugin(QWidget* iWidget, QObject* iParent, const QVariantList& /*iArg*/) : SKGInterfacePlugin(iParent)
{
    Q_UNUSED(iWidget);
    SKGTRACEINFUNC(10);
}

SKGTrackerPlugin::~SKGTrackerPlugin()
{
    SKGTRACEINFUNC(10);
    m_currentBankDocument = NULL;
}

bool SKGTrackerPlugin::setupActions(SKGDocument* iDocument, const QStringList& iArgument)
{
    SKGTRACEINFUNC(10);
    Q_UNUSED(iArgument);
    if (qobject_cast<SKGDocumentBank*>(iDocument) == NULL) {
        return false;
    }

    m_currentBankDocument = iDocument;

    setComponentName("skrooge_tracker", title());
    setXMLFile("skrooge_tracker.rc");

    // Create yours actions here
    return true;
}

SKGTabPage* SKGTrackerPlugin::getWidget()
{
    SKGTRACEINFUNC(10);
    return new SKGTrackerPluginWidget(m_currentBankDocument);
}

KConfigSkeleton* SKGTrackerPlugin::getPreferenceSkeleton()
{
    return skgtracker_settings::self();
}

QString SKGTrackerPlugin::title() const
{
    return i18nc("Noun, something that is used to track items", "Trackers");
}

QString SKGTrackerPlugin::icon() const
{
    return "checkbox";
}

QString SKGTrackerPlugin::toolTip() const
{
    return i18nc("A tool tip", "Trackers management");
}

int SKGTrackerPlugin::getOrder() const
{
    return 25;
}

QStringList SKGTrackerPlugin::tips() const
{
    QStringList output;
    output.push_back(i18nc("Description of a tips", "<p>...you can follow your refunds by using a tracker.</p>"));
    return output;
}

bool SKGTrackerPlugin::isInPagesChooser() const
{
    return true;
}

SKGAdviceList SKGTrackerPlugin::advice(const QStringList& iIgnoredAdvice)
{
    SKGTRACEINFUNC(10);
    SKGAdviceList output;
    // Search old trackers
    if (!iIgnoredAdvice.contains("skgtrackerplugin_old")) {
        SKGStringListList result;
        m_currentBankDocument->executeSelectSqliteOrder("SELECT t_name  FROM v_refund_display WHERE t_close='N' AND julianday('now')-julianday(d_LASTDATE)>300 ORDER BY julianday('now')-julianday(d_LASTDATE) DESC;", result);
        int nb = result.count();
        for (int i = 1; i < nb; ++i) {  // Ignore header
            SKGAdvice ad;
            ad.setUUID("skgtrackerplugin_old|" % result.at(i).at(0));
            ad.setPriority(2);
            ad.setShortMessage(i18nc("Advice on making the best (short)", "'%1' is an old tracker", result.at(i).at(0)));
            ad.setLongMessage(i18nc("Advice on making the best (long)", "This tracker does not contain recent operation. You may want to close it if you do not intend to add other operations"));
            output.push_back(ad);
        }
    }

    return output;
}

#include <skgtrackerplugin.moc>
