#include "pch.h"

#include "legacy_app_startup_services.h"

#include "app.h"
#include "legacy_preference_storage.h"
#include "renderer_gl/opengl_capabilities.h"

namespace pp::panopainter {
namespace {

class LegacyAppStartupServices final
    : public pp::app::AppStartupServices
    , public pp::app::AppStartupResourceServices {
public:
    explicit LegacyAppStartupServices(App& app) noexcept
        : app_(app)
    {
    }

    void store_run_counter(int value) override
    {
        set_legacy_startup_run_counter(value);
        LOG("run_counter %d", value);
    }

    void save_preferences() override
    {
        if (!save_legacy_preferences())
            LOG("save preferences failed");
    }

    void start_timelapse_recording() override
    {
        app_.rec_start();
    }

    void apply_vr_controllers_enabled(bool enabled) override
    {
        app_.vr_controllers_enabled = enabled;
    }

    void show_license_warning() override
    {
        app_.message_box("License", "Could not validate this license, running in demo mode.");
    }

    void initialize_shaders() override
    {
        app_.initShaders();
    }

    void initialize_assets() override
    {
        app_.initAssets();
    }

    void initialize_layout() override
    {
        app_.initLayout();
    }

    void update_title() override
    {
        app_.title_update();
    }

    void create_ui_render_target(int width, int height) override
    {
        app_.uirtt.create(
            width,
            height,
            -1,
            static_cast<GLint>(pp::renderer::gl::rgba8_internal_format()),
            true);
    }

private:
    App& app_;
};

} // namespace

pp::foundation::Status execute_legacy_app_startup_plan(
    App& app,
    const pp::app::AppStartupPlan& plan)
{
    LegacyAppStartupServices services(app);
    return pp::app::execute_app_startup_plan(plan, services);
}

pp::foundation::Status execute_legacy_app_startup_persistence_plan(
    App& app,
    const pp::app::AppStartupPlan& plan)
{
    LegacyAppStartupServices services(app);
    return pp::app::execute_app_startup_persistence_plan(plan, services);
}

pp::foundation::Status execute_legacy_app_startup_runtime_plan(
    App& app,
    const pp::app::AppStartupPlan& plan)
{
    LegacyAppStartupServices services(app);
    return pp::app::execute_app_startup_runtime_plan(plan, services);
}

pp::foundation::Status execute_legacy_app_startup_resources(
    App& app,
    const pp::app::AppStartupResourcePlan& plan)
{
    LegacyAppStartupServices services(app);
    return pp::app::execute_app_startup_resources(plan, services);
}

} // namespace pp::panopainter
