#include "mainwindow.h"
#include "net/net.h"
#include "settings/settings.h"
#include "utils/utils.h"
#include <QApplication>
#ifdef BUILD_OFD_MODE
#   include <curl/curl.h>
#   include <ofdscene.h>
#endif
#include <iostream>
#if __GNUC__ < 8 && __clang_major__ < 17
#	include <experimental/filesystem>
	using namespace std::experimental::filesystem;
#else
#	include <filesystem>
	using namespace std::filesystem;
#endif
#include <QDateTime>
#include <QFile>
#include <QStackedLayout>
#include <QTextStream>
#include <QTranslator>
#ifdef BUILD_EMAIL_TO_TEXT_MODE
#   include <emailtextscene.h>
#endif
#ifdef BUILD_OCR_MODE
#   include <ocrscene.h>
#endif
#include <qpushbutton.h>
#include <parser/parser.h>

static QWidget *loadUI(QWidget *parent, std::string filename) {
    QUiLoader loader;

    QFile file(QString::fromStdString(filename));
    file.open(QIODevice::ReadOnly);

    return loader.load(&file, parent);
}

int main(int argc, char *argv[]) {
    srand(time(0));

    QApplication app(argc, argv);

    std::string settings_file_path =
        get_path_relative_to_home(".local/share/checks_parser/settings.json");

    Settings s(settings_file_path);

    QTranslator translator;
    QString lang = "en_US";

    bool languageSettingPresent = false;
    languageSettingPresent = s.get_all_settings().find("language") != s.get_all_settings().end();

    if (languageSettingPresent) {
        lang = QString::fromStdString(s.get_all_settings()["language"]);
    } else if (translator.load(":/translation/"+QLocale::system().name()+".qm")) {
        lang = QLocale::system().name();
    } else {
        lang = QString::fromStdString("en_US");
    }

    std::cout << "Using locale: " << lang.toStdString() << std::endl;

    translator.load(":/translation/" + lang + ".qm");
    app.installTranslator(&translator);

    QUiLoader loader;

    QWidget *window = new QWidget();
    QStackedLayout *sceneLayout = new QStackedLayout;

    // Main Window setup
    QWidget *mainwindowscene = loadUI(window, ":/scenes/scenes/mainwindow.ui");

    sceneLayout->addWidget(mainwindowscene);

    for (auto btn : ((MainWindow *)mainwindowscene)->findChildren<QPushButton *>()) {
        btn->hide();
    }

    // Main Window buttons setup
#ifdef BUILD_EMAIL_TO_TEXT_MODE
    QPushButton *text_from_email_button = ((MainWindow *)mainwindowscene)->findChild<QPushButton *>("text_from_email_button");
    text_from_email_button->show();
    EmailTextScene *emailTextScene = new EmailTextScene();
    sceneLayout->addWidget(emailTextScene);
    QObject::connect(text_from_email_button, &QPushButton::clicked, [&]() {
        // Text from email scene
        int index = sceneLayout->indexOf(emailTextScene);
        sceneLayout->setCurrentIndex(index);
        sceneLayout->widget(index)->show();
    });

#endif
#ifdef BUILD_OCR_MODE
    QPushButton *ocr_button = ((MainWindow *)mainwindowscene)->findChild<QPushButton *>("ocr_button");
    ocr_button->show();
    OCRScene *ocrscene = new OCRScene();
    sceneLayout->addWidget(ocrscene);
    QObject::connect(ocr_button, &QPushButton::clicked, [&]() {
        // OCR scene
        int index = sceneLayout->indexOf(ocrscene);
        sceneLayout->setCurrentIndex(index);
        sceneLayout->widget(index)->show();
    });
#endif
#ifdef BUILD_OFD_MODE
    QPushButton *ofd_button = ((MainWindow *)mainwindowscene)->findChild<QPushButton *>("ofd_button");
    ofd_button->show();
    OFDScene *ofdscene = new OFDScene();
    sceneLayout->addWidget(ofdscene);
    QObject::connect(ofd_button, &QPushButton::clicked, [&]() {
        // OFD scene
        int index = sceneLayout->indexOf(ofdscene);
        sceneLayout->setCurrentIndex(index);
        sceneLayout->widget(index)->show();
    });
#endif

    //Setting all back buttons
    for (uint32_t sceneIndex = 0; sceneIndex < sceneLayout->count(); sceneIndex ++) {
        auto scene = sceneLayout->widget(sceneIndex);

        QPushButton *back_button = scene->findChild<QPushButton *>("back_button");
        if (back_button == nullptr) continue;

        QObject::connect(back_button, &QPushButton::clicked, [&]() {
            sceneLayout->setCurrentIndex(0);
        });
    }

    window->setLayout(sceneLayout);
    window->show();

    return app.exec();
}