#include "presentation/runtime/Viewport.h"

#include <algorithm>

#include "application/messaging/PresentationState.h"
#include "foundation/diagnostics/OnceLogger.h"

namespace ta::presentation::runtime
{
    void setup(ta::application::DisplayMetrics metrics) noexcept
    {
        if (!metrics.valid())
        {
            ta::foundation::diagnostics::OnceLogger::error(
                "UI", "invalid-display-metrics", "Viewport setup failed: invalid display metrics.");
            return;
        }

        const float width_scale = metrics.width / 1920.0f;
        const float height_scale = metrics.height / 1080.0f;
        float scale = (std::min)(width_scale, height_scale);
        scale = (std::clamp)(scale, 0.75f, 1.60f);
        ta::application::presentation_state::viewport.publish({metrics, scale});
    }

    float width() noexcept
    {
        return ta::application::presentation_state::viewport.acquire().metrics.width;
    }

    float height() noexcept
    {
        return ta::application::presentation_state::viewport.acquire().metrics.height;
    }

    float scale_factor() noexcept
    {
        return ta::application::presentation_state::viewport.acquire().scale_factor;
    }
}
