/***************************************************************************
 *   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
 * This file is Skrooge plugin for interest computation.
 *
 * @author Stephane MANKOWSKI / Guillaume DE BURE
 */
#include "skginterestboardwidget.h"

#include <qdom.h>

#include "skgdocumentbank.h"
#include "skgtraces.h"
#include "skgservices.h"
#include "skgaccountobject.h"
#include "skgunitobject.h"
#include "skgmainpanel.h"

SKGInterestBoardWidget::SKGInterestBoardWidget(SKGDocument* iDocument)
    : SKGBoardWidget(iDocument, i18nc("Title of a dashboard widget", "Estimated interests"))
{
    SKGTRACEINFUNC(10);

    m_label = new QLabel();
    setMainWidget(m_label);

    // Refresh
    connect(getDocument(), &SKGDocument::tableModified, this, &SKGInterestBoardWidget::dataModified, Qt::QueuedConnection);
    connect(m_label, &QLabel::linkActivated, [ = ](const QString & val) {
        SKGMainPanel::getMainPanel()->openPage(val);
    });
}

SKGInterestBoardWidget::~SKGInterestBoardWidget()
{
    SKGTRACEINFUNC(10);
}

QString SKGInterestBoardWidget::getState()
{
    return QStringLiteral("");
}

void SKGInterestBoardWidget::setState(const QString& iState)
{
    Q_UNUSED(iState);
    dataModified(QStringLiteral(""), 0);
}

QString SKGInterestBoardWidget::getDefaultStateAttribute()
{
    return QStringLiteral("SKGINTERESTBOARD_DEFAULT_PARAMETERS");
}

void SKGInterestBoardWidget::dataModified(const QString& iTableName, int iIdTransaction)
{
    SKGTRACEINFUNC(10);
    Q_UNUSED(iIdTransaction);

    if (iTableName == QStringLiteral("v_account") || iTableName == QStringLiteral("interest") || iTableName.isEmpty()) {
        SKGDocumentBank* doc = qobject_cast<SKGDocumentBank*>(getDocument());
        if (doc) {
            SKGServices::SKGUnitInfo primary = doc->getPrimaryUnit();

            // Build display
            QString html = QStringLiteral("<html><body>");
            SKGObjectBase::SKGListSKGObjectBase objs;
            SKGError err = doc->getObjects(QStringLiteral("v_account"), QStringLiteral("t_close='N' AND EXISTS(select 1 from interest where interest.rd_account_id=v_account.id) ORDER BY t_name"), objs);
            IFOK(err) {
                int nb = objs.count();
                if (nb) {
                    html += QStringLiteral("<table>");
                    double sum = 0;
                    for (int i = 0; i < nb; ++i) {
                        SKGAccountObject obj(objs.at(i));
                        html += QString("<tr><td><a href=\"skg://Skrooge_calculator_plugin/?currentPage=0&account=" % SKGServices::encodeForUrl(obj.getName()) % "\">") % SKGServices::stringToHtml(obj.getName()) % "</a></td><td align=\"right\">";

                        SKGAccountObject::SKGInterestItemList oInterestList;
                        double oInterests = 0;
                        obj.getInterestItems(oInterestList, oInterests);
                        sum += oInterests;

                        html += doc->formatMoney(oInterests, primary);
                        html += QStringLiteral("</td></tr>");
                    }
                    html += "<tr><td><b>" % SKGServices::stringToHtml(i18nc("Noun, the numerical total of a sum of values", "Total")) % "</b></td>"
                            "<td align=\"right\"><b>" % doc->formatMoney(sum, primary) % "</b></td>"
                            "</tr>";
                    html += QStringLiteral("</table>");
                } else {
                    html += i18nc("A Message for the user about financial interests", "No interest defined for accounts<br>on the <a href=\"%1\">\"Simulation\"</a> page.", "skg://Skrooge_calculator_plugin/?currentPage=0");
                }
                html += QStringLiteral("</body></html>");
                m_label->setText(html);
            }

            // No widget if no account
            bool exist = false;
            getDocument()->existObjects(QStringLiteral("account"), QStringLiteral(""), exist);
            if (parentWidget()) {
                setVisible(exist);
            }
        }
    }
}


