#include "pch.h"
#include "app.h"
#include "legacy_preference_storage.h"
#include "legacy_ui_overlay_services.h"
#include "node_about.h"
#include "node_changelog.h"
#include "node_remote_page.h"
#include "node_shorcuts.h"
#include "node_usermanual.h"
#include "version.h"

namespace pp::panopainter {

namespace {

template <typename DialogNode>
void open_info_dialog(App& app, const char* log_name)
{
    auto* overlay_anchor = app.layout[app.main_id];
    if (!overlay_anchor) {
        LOG("%s dialog open failed: main layout anchor is missing", log_name);
        return;
    }

    auto dialog = make_legacy_overlay_node<DialogNode>(app);
    const auto overlay = open_legacy_overlay_node_with_handle(*overlay_anchor, dialog);
    if (!overlay) {
        LOG("%s dialog open failed: %s", log_name, overlay.status().message);
        return;
    }
    const auto overlay_handle = overlay.value();

    const auto close_dialog = [overlay_anchor, overlay_handle]() {
        const auto close_status = close_legacy_overlay_node(*overlay_anchor, overlay_handle);
        (void)close_status;
    };

    dialog->btn_ok->on_click = [close_dialog](Node*) {
        close_dialog();
    };
}

} // namespace

void open_usermanual_dialog(App& app)
{
    open_info_dialog<NodeUserManual>(app, "User manual");
}

void open_changelog_dialog(App& app)
{
    open_info_dialog<NodeChangelog>(app, "Changelog");
}

void open_about_dialog(App& app)
{
    open_info_dialog<NodeAbout>(app, "About");
}

void open_whatsnew_dialog(App& app, bool force_show)
{
    auto* overlay_anchor = app.layout[app.main_id];
    if (!overlay_anchor) {
        LOG("What's new dialog open failed: main layout anchor is missing");
        return;
    }

    const auto overlay_handle = std::make_shared<pp::ui::NodeHandle>();
    const auto open_overlay = [overlay_anchor, overlay_handle](const std::shared_ptr<NodeRemotePage>& page) {
        if (overlay_handle->valid()) {
            return;
        }

        const auto overlay = open_legacy_overlay_node_with_handle(*overlay_anchor, page);
        if (!overlay) {
            return;
        }
        *overlay_handle = overlay.value();
    };
    const auto close_overlay = [overlay_anchor, overlay_handle]() {
        if (!overlay_handle->valid()) {
            return;
        }

        const auto close_status = close_legacy_overlay_node(*overlay_anchor, *overlay_handle);
        (void)close_status;
        *overlay_handle = {};
    };

    auto whatsnew = std::make_shared<NodeRemotePage>();
    whatsnew->set_manager(&app.layout);
    whatsnew->init();
    std::string url = fmt::format("https://panopainter.com/app-content/whatsnew/?version={}", g_version_build);
    whatsnew->load_url(url, [whatsnew, force_show, open_overlay](bool success) {
        if (success)
        {
            int last_id = legacy_whatsnew_id_or(0);
            if (force_show || (whatsnew->m_page_id <= g_version_build && whatsnew->m_page_id > last_id))
            {
                whatsnew->set_title(fmt::format("What's new in version {}", g_version_number));
                if (!force_show)
                    open_overlay(whatsnew);
            }
        }
    });
    whatsnew->add_button("Reload", 120, [whatsnew](Node*) {
        whatsnew->reload();
    });
    whatsnew->add_button("Read Later", 120, [whatsnew, close_overlay](Node*) {
        clear_legacy_whatsnew_id();
        save_legacy_preferences();
        close_overlay();
    });
    whatsnew->add_button("Close", 100, [whatsnew, close_overlay](Node*) {
        set_legacy_whatsnew_id(whatsnew->m_page_id);
        save_legacy_preferences();
        close_overlay();
    });
    if (force_show)
        open_overlay(whatsnew);
}

void open_shortcuts_dialog(App& app)
{
    (void)add_legacy_overlay_node<NodeShortcuts>(app);
}

} // namespace pp::panopainter
