#include "View/mainwindow.h"
#include <View/beforestart.h>
#include <translatableapp.h>
#include <QDir>
#include <QStringList>
#include <Manager/settingsmanager.h>
#include <splashscreen.h>
#include <QThread>

void showInitTips(MainWindow *w);
void showInitImage();
void showExitImage();

int main(int argc, char *argv[])
{
    TranslatableApp app(argc,argv);



    QStringList languages;
    languages << "es" << "en";

    QString language = SettingsManager::getCurrentLanguage();

    if(language.isEmpty())
    {
        language = "es";
    }

    app.createTranslators(languages);
    app.swithLanguage(language);

    showInitImage();
    //app.processEvents();

    MainWindow *mainWindow = new MainWindow();
    mainWindow->show();
    mainWindow->activateWindow();
    mainWindow->raise();

    showInitTips(mainWindow);


    app.processEvents();

    int ex = app.exec();
    showExitImage();
    return ex;
}

void showInitImage()
{
    QPixmap pixmap(":/resources/icon/init/Load0.jpeg");
    SplashScreen* splash = new SplashScreen(pixmap);

    splash->show();
    QStringList elements = QDir(":/resources/icon/init/").entryList();

    while(!elements.isEmpty()) {
        QString element = elements.takeFirst();
        QString path = QString(":/resources/icon/init/").append(element);
        QPixmap pixmap(path);
        splash->updateProgress(pixmap);
        QThread::msleep(40);
    }

    QThread::msleep(400);

    splash->close();
}

void showExitImage()
{
    QPixmap pixmap(":/resources/icon/init/Load92.jpeg");
    SplashScreen* splash = new SplashScreen(pixmap);

    splash->show();
    QStringList elements = QDir(":/resources/icon/init/").entryList();

    while(!elements.isEmpty()) {
        QString element = elements.takeLast();
        QString path = QString(":/resources/icon/init/").append(element);
        QPixmap pixmap(path);
        splash->updateProgress(pixmap);
        QThread::msleep(40);
    }

    QThread::msleep(400);

    splash->close();
}

void showInitTips(MainWindow *w)
{
    bool showInitTips = SettingsManager::getShowInitTips();

    if(showInitTips)
    {
        w->setEnabled(false);
        BeforeStart *initWindow = new BeforeStart(w);
        initWindow->show(w->size(), w->pos());
        initWindow->activateWindow();
        initWindow->raise();
    }
}

