#include "mainwindow.h"

#include <QApplication>

#include <QtSql>

#include <QMessageBox>

bool createConnection(QString userName, QString password, QString driver,
                      QString server, QString database,
                      QString persistSecurityInfo) {
  auto db = QSqlDatabase::addDatabase("QODBC");
  QString databaseName("DRIVER={%1};");
  databaseName.append("SERVER=%2;");
  databaseName.append("DATABASE=%3;");
  databaseName.append("Persist Security Info=%4;");
  databaseName =
      databaseName.arg(driver, server, database, persistSecurityInfo);
  db.setDatabaseName(databaseName);
  db.setUserName(userName);
  db.setPassword(password);
  return db.open();
}

int main(int argc, char *argv[]) {
  QApplication a(argc, argv);
  bool success = createConnection("user", "user", "SQL Server",
                                  "(local)\\SQLEXPRESS", "Database", "true");

  if (!success) {
#ifndef NDEBUG
    QMessageBox::critical(nullptr, QObject::tr("Error"),
                          QObject::tr("Database connection error: %2")
                              .arg(QSqlDatabase().lastError().text()));
#else
    QMessageBox::critical(
        nullptr, QObject::tr("Database error"),
        QObject::tr("Connection to the database is not possible."));
#endif
    return 1;
  }

  MainWindow mainWindow;
  mainWindow.show();

  return a.exec();
}
