/***************************************************************************
 *   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 operation management.
 *
 * @author Stephane MANKOWSKI / Guillaume DE BURE
 */
#include "skgoperationboardwidgetqml.h"

#include <qdom.h>

#include <qaction.h>
#include <qwidgetaction.h>
#include <qquickwidget.h>
#include <qqmlcontext.h>

#include "skgdocumentbank.h"
#include "skgreportbank.h"
#include "skgtraces.h"
#include "skgservices.h"
#include "skgobjectbase.h"
#include "skgmainpanel.h"
#include "skgperiodedit.h"

SKGOperationBoardWidgetQml::SKGOperationBoardWidgetQml(SKGDocument* iDocument)
    : SKGHtmlBoardWidget(iDocument, i18nc("Dashboard widget title", "Income & Expenditure"),
                         QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("skrooge/html/default/income_vs_expenditure.qml")), QStringList() << QStringLiteral("v_operation")),
      m_periodEdit1(nullptr), m_periodEdit2(nullptr)
{
    SKGTRACEINFUNC(10);

    QStringList overlayopen;
    overlayopen.push_back(QStringLiteral("quickopen"));
    m_menuOpen = new QAction(SKGServices::fromTheme(QStringLiteral("view-statistics"), overlayopen), i18nc("Verb", "Open report..."), this);
    connect(m_menuOpen, &QAction::triggered, SKGMainPanel::getMainPanel(), [ = ]() {
        SKGMainPanel::getMainPanel()->SKGMainPanel::openPage();
    });
    addAction(m_menuOpen);

    {
        auto sep = new QAction(this);
        sep->setSeparator(true);
        addAction(sep);
    }

    m_menuTransfert = new QAction(i18nc("Noun, a type of operations", "Transfers"), this);
    m_menuTransfert->setCheckable(true);
    m_menuTransfert->setChecked(false);
    connect(m_menuTransfert, &QAction::triggered, this, &SKGOperationBoardWidgetQml::dataModified);
    addAction(m_menuTransfert);
    m_Quick->rootContext()->setContextProperty(QStringLiteral("transfertWidget"), m_menuTransfert);


    m_menuTracked = new QAction(i18nc("Noun, a type of operations", "Tracked"), this);
    m_menuTracked->setCheckable(true);
    m_menuTracked->setChecked(true);
    connect(m_menuTracked, &QAction::triggered, this, &SKGOperationBoardWidgetQml::dataModified);
    addAction(m_menuTracked);
    m_Quick->rootContext()->setContextProperty(QStringLiteral("trackerWidget"), m_menuTracked);

    m_menuSuboperation = new QAction(i18nc("Noun, a type of operations", "On suboperations"), this);
    m_menuSuboperation->setCheckable(true);
    m_menuSuboperation->setChecked(false);
    connect(m_menuSuboperation, &QAction::triggered, this, &SKGOperationBoardWidgetQml::dataModified);
    addAction(m_menuSuboperation);
    m_Quick->rootContext()->setContextProperty(QStringLiteral("suboperationsWidget"), m_menuSuboperation);

    {
        auto sep = new QAction(this);
        sep->setSeparator(true);
        addAction(sep);
    }

    m_periodEdit1 = new SKGPeriodEdit(this, true);
    m_periodEdit1->setObjectName(QStringLiteral("m_periodEdit1"));
    {
        // Set default
        QDomDocument doc(QStringLiteral("SKGML"));
        QDomElement root = doc.createElement(QStringLiteral("parameters"));
        doc.appendChild(root);
        root.setAttribute(QStringLiteral("period"), SKGServices::intToString(static_cast<int>(SKGPeriodEdit::CURRENT)));
        m_periodEdit1->setState(doc.toString());

        // Add widget in menu
        auto periodEditWidget = new QWidgetAction(this);
        periodEditWidget->setObjectName(QStringLiteral("m_periodEdit1Action"));
        periodEditWidget->setDefaultWidget(m_periodEdit1);

        addAction(periodEditWidget);
    }
    m_Quick->rootContext()->setContextProperty(QStringLiteral("period1Widget"), m_periodEdit1);

    {
        auto sep = new QAction(this);
        sep->setSeparator(true);
        addAction(sep);
    }

    m_periodEdit2 = new SKGPeriodEdit(this, true);
    m_periodEdit2->setObjectName(QStringLiteral("m_periodEdit2"));
    {
        // Set default
        QDomDocument doc(QStringLiteral("SKGML"));
        QDomElement root = doc.createElement(QStringLiteral("parameters"));
        doc.appendChild(root);
        root.setAttribute(QStringLiteral("period"), SKGServices::intToString(static_cast<int>(SKGPeriodEdit::PREVIOUS)));
        m_periodEdit2->setState(doc.toString());

        // Add widget in menu
        auto periodEditWidget = new QWidgetAction(this);
        periodEditWidget->setObjectName(QStringLiteral("m_periodEdit2Action"));
        periodEditWidget->setDefaultWidget(m_periodEdit2);

        addAction(periodEditWidget);
    }
    m_Quick->rootContext()->setContextProperty(QStringLiteral("period2Widget"), m_periodEdit2);

    // Refresh
    connect(m_periodEdit1, &SKGPeriodEdit::changed, this, &SKGOperationBoardWidgetQml::dataModified);
    connect(m_periodEdit2, &SKGPeriodEdit::changed, this, &SKGOperationBoardWidgetQml::dataModified);
}

SKGOperationBoardWidgetQml::~SKGOperationBoardWidgetQml()
{
    SKGTRACEINFUNC(10);
    m_menuTransfert = nullptr;
    m_menuTracked = nullptr;
}

QString SKGOperationBoardWidgetQml::getState()
{
    QDomDocument doc(QStringLiteral("SKGML"));
    QDomElement root = doc.createElement(QStringLiteral("parameters"));
    doc.appendChild(root);

    root.setAttribute(QStringLiteral("menuTransfert"), m_menuTransfert && m_menuTransfert->isChecked() ? QStringLiteral("Y") : QStringLiteral("N"));
    root.setAttribute(QStringLiteral("menuTracked"), m_menuTracked && m_menuTracked->isChecked() ? QStringLiteral("Y") : QStringLiteral("N"));
    root.setAttribute(QStringLiteral("menuSuboperation"), m_menuSuboperation && m_menuSuboperation->isChecked() ? QStringLiteral("Y") : QStringLiteral("N"));
    root.setAttribute(QStringLiteral("period1"), m_periodEdit1 ? m_periodEdit1->getState() : QStringLiteral(""));
    root.setAttribute(QStringLiteral("period2"), m_periodEdit2 ? m_periodEdit2->getState() : QStringLiteral(""));
    return doc.toString();
}

void SKGOperationBoardWidgetQml::setState(const QString& iState)
{
    QDomDocument doc(QStringLiteral("SKGML"));
    doc.setContent(iState);
    QDomElement root = doc.documentElement();
    if (m_menuTransfert) {
        m_menuTransfert->setChecked(root.attribute(QStringLiteral("menuTransfert")) == QStringLiteral("Y"));
    }
    if (m_menuTracked) {
        m_menuTracked->setChecked(root.attribute(QStringLiteral("menuTracked")) != QStringLiteral("N"));
    }
    if (m_menuSuboperation) {
        m_menuSuboperation->setChecked(root.attribute(QStringLiteral("menuSuboperation")) == QStringLiteral("Y"));
    }
    QString period1 = root.attribute(QStringLiteral("period1"));
    if (m_periodEdit1 && !period1.isEmpty()) {
        m_periodEdit1->setState(period1);
    }
    QString period2 = root.attribute(QStringLiteral("period2"));
    if (m_periodEdit2 && !period2.isEmpty()) {
        m_periodEdit2->setState(period2);
    }

    SKGHtmlBoardWidget::dataModified(QStringLiteral(""), 0);
}

void SKGOperationBoardWidgetQml::dataModified()
{
    SKGTRACEINFUNC(10);
    if (m_periodEdit1 && m_periodEdit2 && m_menuOpen) {
        SKGDocumentBank* doc = qobject_cast<SKGDocumentBank*>(getDocument());
        if (doc) {
            QString url = QStringLiteral("skg://skrooge_report_plugin/?grouped=")
                          % (m_menuTransfert && m_menuTransfert->isChecked() ? QStringLiteral("Y") : QStringLiteral("N")) % "&transfers="
                          % (m_menuTransfert && m_menuTransfert->isChecked() ? QStringLiteral("Y") : QStringLiteral("N")) % "&tracked="
                          % (m_menuTracked && m_menuTracked->isChecked() ? QStringLiteral("Y") : QStringLiteral("N")) % "&expenses=Y&incomes=Y&lines2=t_TYPEEXPENSENLS&columns=d_DATEMONTH&currentPage=-1" %
                          "&mode=0&interval=3&period=3" %
                          "&tableAndGraphState.graphMode=1&tableAndGraphState.allPositive=Y&tableAndGraphState.show=graph&title=" %
                          SKGServices::encodeForUrl(i18nc("Noun, the title of a section", "Income & Expenditure"));
            m_menuOpen->setData(url);
        }
    }
}
