#include "ddlpreviewdialog.h"
#include "ui_ddlpreviewdialog.h"
#include "services/codeformatter.h"
#include "uiconfig.h"
#include "letos.h"
#include "db/db.h"
#include "common/dialogsizehandler.h"
#include "mainwindow.h"
#include "windows/editorwindow.h"
#include <QPushButton>
#include <QDialogButtonBox>

DdlPreviewDialog::DdlPreviewDialog(Db* db, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DdlPreviewDialog),
    db(db)
{
    ui->setupUi(this);
    connect(ui->openInEditorBtn, SIGNAL(clicked(bool)), this, SLOT(openInEditor()));
    DialogSizeHandler::applyFor(this);
}

DdlPreviewDialog::~DdlPreviewDialog()
{
    delete ui;
}

void DdlPreviewDialog::setDdl(const QString& ddl)
{
    QString formatted = LETOS->getCodeFormatter()->format("sql", ddl, db);
    ui->ddlEdit->setPlainText(formatted);
}

void DdlPreviewDialog::setDdl(const QStringList& ddlList)
{
    QStringList fixedList;
    QString newDdl;
    for (const QString& ddl : ddlList)
    {
        newDdl = ddl.trimmed();
        if (!newDdl.endsWith(";"))
            newDdl.append(";");

        fixedList << LETOS->getCodeFormatter()->format("sql", newDdl, db);
    }
    setDdl(fixedList.join("\n"));
}

void DdlPreviewDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
        case QEvent::LanguageChange:
            ui->retranslateUi(this);
            break;
        default:
            break;
    }
}

void DdlPreviewDialog::openInEditor()
{
    QString ddl = ui->ddlEdit->toPlainText();
    auto editor = MAINWINDOW->openSqlEditor(db, ddl);
    editor->getMdiWindow()->rename(windowTitle());
    QDialog::reject();
}

void DdlPreviewDialog::accept()
{
    CFG_UI.General.DontShowDdlPreview.set(ui->dontShowAgainCheck->isChecked());
    QDialog::accept();
}
