#include <filesystem>

#include <ftxui/component/component.hpp>
#include <ftxui/component/screen_interactive.hpp>
#include <ftxui/dom/elements.hpp>
#include <toml.hpp>

#include "katja/sbo.hpp"
#include "katja/database.hpp"
#include "component.hpp"

int main(int argc, const char **argv)
{
    auto configuration = toml::parse("config/katja.toml");
    std::multimap<std::string, katja::database_package> installed_database = katja::read_installed_database();

    for (const auto& [repository_name, repository_value] : configuration.as_table())
    {
        std::filesystem::path slackbuild_repository{ repository_value.at("path").as_string() };
        katja::sbo_repository repository{ slackbuild_repository };
        auto updates = repository.get_updates(installed_database);

        auto screen = ftxui::ScreenInteractive::Fullscreen();
        std::vector<std::string> menu_entries = {
            "Updates",
            "Search",
            "Quit"
        };
        int menu_selected = 0;
        ftxui::MenuOption menu_option = ftxui::MenuOption::Horizontal();
        menu_option.on_enter = screen.ExitLoopClosure();
        ftxui::Component menu = ftxui::Menu(&menu_entries, &menu_selected, menu_option);

        auto custom_component = std::make_shared<katja::UpdatesPage>(std::move(updates));
        ftxui::Component renderer = ftxui::Container::Vertical({
                menu,
                custom_component
        });
        screen.Loop(renderer);
    }
    return EXIT_SUCCESS;
}