#include "printing.h"
#include "iconmanager.h"
#include "printingexport.h"
#include "mainwindow.h"
#include "windows/editorwindow.h"
#include "dataview.h"
#include "common/extactionprototype.h"
#include "datagrid/sqlquerymodel.h"
#include "exportworker.h"
#include "services/notifymanager.h"
#include "sqleditor.h"
#include "translations.h"
#include <QDebug>
#include <QPrinter>
#include <QPrintDialog>
#include <QThreadPool>

bool Printing::init()
{
    SQLS_INIT_RESOURCE(printing);

    printingExport = new PrintingExport();
    bool printingExportInit = printingExport->init();
    if (!printingExportInit)
        return false;

    loadTranslation("Printing");

    printingConfig = new ExportManager::StandardExportConfig();
    printingConfig->exportTableData = true;
    printingConfig->exportIndexes = false;
    printingConfig->exportTriggers = false;
    printingConfig->codec = defaultCodecName();

    QIcon printerIcon = ICONMANAGER->getIcon("printer", ":/icons/printer.svg");
    printDataAction = new ExtActionPrototype(printerIcon, tr("Print data"), this);
    separatorAction = new ExtActionPrototype(this);
    printQueryAction = new ExtActionPrototype(printerIcon, tr("Print query"), this);

    connect(printDataAction, SIGNAL(triggered(ExtActionContainer*,int)), this, SLOT(dataPrintRequested(ExtActionContainer*)));
    connect(printQueryAction, SIGNAL(triggered(ExtActionContainer*,int)), this, SLOT(queryPrintRequested(ExtActionContainer*)));

    DataView::insertActionAfter(printDataAction, DataView::LAST_PAGE);
    DataView::insertActionAfter(separatorAction, DataView::LAST_PAGE);
    EditorWindow::insertAction(printQueryAction);

    return true;
}

void Printing::deinit()
{
    printingExport->deinit();

    DataView::removeAction(printDataAction);
    DataView::removeAction(separatorAction);
    EditorWindow::removeAction(printQueryAction);
    safe_delete(printingExport);
    safe_delete(printDataAction);
    safe_delete(separatorAction);
    safe_delete(printQueryAction);
    SQLS_CLEANUP_RESOURCE(printing);
}

void Printing::dataPrintRequested(ExtActionContainer* actionContainer)
{
    DataView* dataView = dynamic_cast<DataView*>(actionContainer);
    if (!dataView)
    {
        qCritical() << "Printing::dataPrintRequested() called not from DataView:" << actionContainer;
        return;
    }

    if (dataView->getModel()->rowCount() == 0)
    {
        notifyError(tr("No data to print."));
        return;
    }

    QPrintDialog* printDialog = new QPrintDialog(MAINWINDOW);
    if (printDialog->exec() != QDialog::Accepted)
        return;

    notifyInfo(tr("Printing data."));

    QString query = dataView->getModel()->getQuery();
    Db* db = dataView->getModel()->getDb();

    printingExport->setPaintDevice(printDialog->printer());

    ExportWorker* worker = new ExportWorker(printingExport, printingConfig, nullptr);
    worker->prepareExportQueryResults(db, query);
    connect(worker, SIGNAL(finished(bool,QIODevice*)), printDialog, SLOT(deleteLater()));
    QThreadPool::globalInstance()->start(worker);
}

void Printing::queryPrintRequested(ExtActionContainer* actionContainer)
{
    EditorWindow* editor = dynamic_cast<EditorWindow*>(actionContainer);
    if (!editor)
    {
        qCritical() << "Printing::queryPrintRequested() called not from EditorWindow:" << actionContainer;
        return;
    }

    QPrintDialog* printDialog = new QPrintDialog(MAINWINDOW);
    if (printDialog->exec() != QDialog::Accepted)
        return;

    notifyInfo(tr("Printing query."));

    QTextDocument* doc = editor->getEditor()->document();
    doc->print(printDialog->printer());
    printDialog->deleteLater();
}
