#include "mainwindow.h"
#include "net/net.h"
#include "settings/settings.h"
#include "utils/utils.h"
#include <QApplication>
#include <curl/curl.h>
#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>
#include <emailtextscene.h>
#include <ocrscene.h>
#include <ofdscene.h>
#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";

    if (s.get_all_settings().contains("language")) {
        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");

    // Main Window buttons setup
    QPushButton *text_from_email_button = ((MainWindow *)mainwindowscene)->findChild<QPushButton *>("text_from_email_button");
    QPushButton *ocr_button = ((MainWindow *)mainwindowscene)->findChild<QPushButton *>("ocr_button");
    QPushButton *ofd_button = ((MainWindow *)mainwindowscene)->findChild<QPushButton *>("ofd_button");

    QObject::connect(text_from_email_button, &QPushButton::clicked, [&]() {
        // Text from email scene
        sceneLayout->setCurrentIndex(1);
        sceneLayout->widget(1)->show();
    });

    QObject::connect(ocr_button, &QPushButton::clicked, [&]() {
        // OCR scene
        sceneLayout->setCurrentIndex(2);
        sceneLayout->widget(2)->show();
    });

    QObject::connect(ofd_button, &QPushButton::clicked, [&]() {
        // OFD scene
        sceneLayout->setCurrentIndex(3);
        sceneLayout->widget(3)->show();
    });

    EmailTextScene *emailTextScene = new EmailTextScene();
    OCRScene *ocrscene = new OCRScene();
    OFDScene *ofdscene = new OFDScene();

    sceneLayout->addWidget(mainwindowscene);
    sceneLayout->addWidget(emailTextScene);
    sceneLayout->addWidget(ocrscene);
    sceneLayout->addWidget(ofdscene);

    //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();

    app.exec();

    return 0;

    // QApplication app(argc, argv);

    // QWidget *window = new QWidget;
    // QStackedLayout *stackedLayout = new QStackedLayout;

    // QWidget *scene1 = new QWidget;
    // QWidget *scene2 = new QWidget;

    // // Add some widgets to each scene
    // QPushButton *button1 = new QPushButton("Switch to Scene 2");
    // scene1->setLayout(new QVBoxLayout);
    // scene1->layout()->addWidget(button1);

    // QPushButton *button2 = new QPushButton("Switch to Scene 1");
    // scene2->setLayout(new QVBoxLayout);
    // scene2->layout()->addWidget(button2);

    // // Add the scenes to the stacked layout
    // stackedLayout->addWidget(scene1);
    // stackedLayout->addWidget(scene2);

    // // Set the layout of the window
    // window->setLayout(stackedLayout);

    // // Connect the buttons to switch scenes
    // QObject::connect(button1, &QPushButton::clicked, [&]() {
    //     stackedLayout->setCurrentIndex(1);
    // });

    // QObject::connect(button2, &QPushButton::clicked, [&]() {
    //     stackedLayout->setCurrentIndex(0);
    // });

    // window->show();
    // app.exec();

    // return 0;
    curl_global_init(CURL_GLOBAL_ALL);

    std::string program_data_path = get_path_relative_to_home(".local/share/checks_parser");
    create_directories(program_data_path);

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

    // Settings s(settings_file_path);
    Net n;

    Parser p;
    std::vector<std::string> stores_updates = p.check_updates();
    for (const std::string &update : stores_updates) {
        std::cout << "Downloading "
                  << s.get_setting("stores_modules_url")  + update << " to "
                  << get_path_relative_to_home(s.get_setting("stores_modules_dir") +
                                               "/" + update)
                  << std::endl;

        n.get_file(s.get_setting("stores_modules_url") + "/" + update,
                   get_path_relative_to_home(s.get_setting("stores_modules_dir") +
                                             "/" + update));
    }

    QApplication a(argc, argv);

    // QTranslator translator;
    // QString lang = "en_US";

    // if (s.get_all_settings().contains("language")) {
    //     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");

    // a.installTranslator(&translator);
    MainWindow w;
    w.update();
    w.show();

    return a.exec();
}