//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      App/main.cpp
//! @brief     Main function of the whole GUI
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "App/MessageHandler.h"
#include "App/appoptions.h"
#include "GUI/Application/GlobalSettings.h"
#include "GUI/Session/SessionData.h"
#include "GUI/Views/Loaders/DataLoaderUtil.h"
#include "GUI/Views/Main/mainwindow.h"
#include "GUI/utils/Helpers.h"
#include "GUI/utils/hostosinfo.h"
#include "config_build.h"
#include <QDir>
#include <QIcon>
#include <QLocale>
#include <QMessageBox>
#include <QMetaType>
#include <boost/exception/diagnostic_information.hpp>

void messageHandler(QtMsgType, const QMessageLogContext&, const QString&) {}

int main(int argc, char* argv[])
{
    ApplicationOptions options(argc, argv);
    if (!options.isConsistent())
        return 0;

    QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
    qRegisterMetaType<QVector<double>>("QVector<double>");

    if (!options.disableHighDPISupport())
        QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);

    QApplication app(argc, argv);
    app.setApplicationName("BornAgain");
    app.setApplicationVersion(GUI::Helpers::getBornAgainVersionString());
    app.setOrganizationName("BornAgain");
    if (!GUI::Utils::OS::HostOsInfo::isMacHost())
        QApplication::setWindowIcon(QIcon(":/images/BornAgain.ico"));

    GlobalSettings GSettings;
    SessionData GSession;
    int ret;

#if !defined(BUILD_DEBUG)
    try {
#endif
        qInstallMessageHandler(MessageHandler);

        register1DDataLoaders();

        auto style = GSettings.settings().styleToUse();
        GSettings.loadStyle(style);

        QString dir = SessionData::appDataFolder();
        if (!QDir().exists(dir))
            QDir().mkpath(dir);

        GSession.instrumentLibrary.load();

        MainWindow win;
        if (options.find("geometry"))
            win.resize(options.mainWindowSize());

        win.show();

        ret = QApplication::exec();

        GSession.instrumentLibrary.saveIfModified();
#if !defined(BUILD_DEBUG)
    } catch (...) {
        QMessageBox box;
        box.setWindowTitle("Fatal error");
        box.setIcon(QMessageBox::Critical);
        box.setText("BornAgain encountered a fatal error and can't continue execution.\n\n"
                    "Diagnostics of error:\n"
                    + QString::fromStdString(boost::current_exception_diagnostic_information()));
        box.addButton("Acknowledge", QMessageBox::AcceptRole);
        box.exec();
        ret = EXIT_FAILURE;
    }
#endif

    return ret;
}