#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <core.h>

#include <QPushButton>
#include <QVBoxLayout>
#include <QFileDialog>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_lblUser = new QLabel(this);
    ui->statusBar->addWidget(m_lblUser);

    m_loginDialog = new LoginDialog(this);

    QFile styleFile(":/style.css");

    if (styleFile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        this->setStyleSheet(styleFile.readAll());
    }

    connect(m_loginDialog, &LoginDialog::accepted, [this]{
        PermissionService service;
        QSharedPointer<User> u = service.loadUser(m_loginDialog->login());
        m_lblUser->setText(u->name());
        m_loginDialog->reset();
        Context::instance().setCurrentUser(u);
    });

    connect(m_loginDialog, &LoginDialog::rejected, [this]{
        close();
    });

    Context::instance().loadPlugins();
    int i = 0;

    foreach (IPlugin *plugin, Context::instance().plugins()) {
        QPushButton *plugButton = new QPushButton(this);
        plugButton->setText(plugin->pluginName());
        ui->navigation->layout()->addWidget(plugButton);
        plugButton->setProperty(PLUGIN_INDEX, i);
        i++;
        connect(plugButton, SIGNAL(clicked()), this, SLOT(openPlugin()) );
    }

   ((QVBoxLayout*)ui->navigation->layout())->addStretch(1);

    if (Context::instance().db() != NULL)
    {
        ui->navigation->setEnabled(true);
    }
}

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

void MainWindow::on_actionExit_triggered()
{
    this->close();
    Context::instance().destroy();
}

void MainWindow::openPlugin()
{
    QVariant var = QObject::sender()->property(PLUGIN_INDEX);
    IPlugin *plugin = Context::instance().plugins().at(var.toInt());

    for (int i = 0; i < ui->tabWidget->count(); i++) {
        if (ui->tabWidget->widget(i)->objectName() == plugin->pluginId()) {
            ui->tabWidget->setCurrentIndex(i);
            return;
        }
    }

    ui->tabWidget->addTab(plugin->ui(), QIcon(), plugin->pluginName());
    ui->tabWidget->widget(ui->tabWidget->count() - 1)->setObjectName(plugin->pluginId());
    ui->tabWidget->setCurrentIndex(ui->tabWidget->count() - 1);
}

void MainWindow::on_actionOpen_database_triggered()
{
    /*QFileDialog dialog(this);
    dialog.setNameFilter(tr("Database Files (*.db)"));
    dialog.setWindowTitle(tr("Open Database"));*/

    QString dbFile = QFileDialog::getOpenFileName(this, "Open Database", "", "Database Files (*.db)");
    if (!dbFile.isEmpty())
    {
        Context::instance().openDb(dbFile);
        ui->navigation->setEnabled(true);
        on_actionLogin_triggered();
    }
}

void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
    ui->tabWidget->removeTab(index);
}

void MainWindow::on_actionLogin_triggered()
{
    QSharedPointer<User> u;
    Context::instance().setCurrentUser(u);
    m_lblUser->setText("");
    m_loginDialog->show();
}

void MainWindow::showEvent(QShowEvent *evt)
{
    QWidget::showEvent(evt);
    if (Context::instance().db() != NULL && Context::instance().currentUser().data() == NULL)
    {
        m_loginDialog->show();
    }
}
