Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports Nexamas.UI.Components
Imports Nexamas.UI.General
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Scroll
Imports Nexamas.UI.SkiaScroll
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Layout
Imports SkiaSharp

Namespace Nexamas.UI.FloatRuntime

    Partial Friend NotInheritable Class MASMessageBoxFloatContent
#Region "Render"

        Friend Sub RenderFloat(canvas As SKCanvas,
                               ctx As MASThemeContext,
                               boundsPx As SKRect) Implements IMASFloatRenderableContent.RenderFloat

            If _disposed Then Return
            If canvas Is Nothing Then Return
            If ctx Is Nothing Then Return
            If boundsPx.IsEmpty OrElse boundsPx.Width <= 0.0F OrElse boundsPx.Height <= 0.0F Then Return

            Dim dpi As Single = PixelSnap.SafeDpi(ctx.Dpi)

            Dim metrics As MessageBoxMetrics =
                If(_request.MetricsOverride, MessageBoxMetricsDefaults.Create())

            Dim measure As MessageBoxMeasureResult =
                MessageBoxMeasureEngine.Measure(
                    dpi:=dpi,
                    request:=_request,
                    ctx:=ctx,
                    metrics:=metrics)

            Dim panelRectPx As SKRect =
                ResolvePanelRect(
                    hostBoundsPx:=boundsPx,
                    measure:=measure,
                    dpi:=dpi,
                    metrics:=metrics)

            _panelRectPx = panelRectPx

            _layout =
    MessageBoxLayoutEngine.Compute(
        panelRectPx:=panelRectPx,
        dpi:=dpi,
        request:=_request,
        measure:=measure,
        metrics:=metrics)

            EnsureScrollForContext(ctx)
            UpdateContentScrollHost(dpi)
            EnsureButtonStates(GetButtonCount())
            ClampScrollOffset()

            _renderer.Render(
                canvas:=canvas,
                ctx:=ctx,
                panelRectPx:=panelRectPx,
                request:=_request,
                state:=_state,
                layout:=_layout)

            DrawContentScrollbar(canvas)

        End Sub

        Private Shared Function ResolvePanelRect(hostBoundsPx As SKRect,
                                                 measure As MessageBoxMeasureResult,
                                                 dpi As Single,
                                                 metrics As MessageBoxMetrics) As SKRect

            If hostBoundsPx.IsEmpty Then Return SKRect.Empty

            Dim w As Single = 0.0F
            Dim h As Single = 0.0F

            If measure IsNot Nothing Then
                w = measure.PanelWidth
                h = measure.PanelHeight
            End If

            If w <= 1.0F Then
                w = If(metrics Is Nothing, 450.0F, metrics.FixedPanelWidth) * dpi
            End If

            If h <= 1.0F Then
                h = If(metrics Is Nothing, 170.0F, metrics.FixedPanelHeight) * dpi
            End If

            w = Math.Min(Math.Max(1.0F, w), Math.Max(1.0F, hostBoundsPx.Width))
            h = Math.Min(Math.Max(1.0F, h), Math.Max(1.0F, hostBoundsPx.Height))

            Dim left As Single =
                hostBoundsPx.Left + ((hostBoundsPx.Width - w) * 0.5F)

            Dim top As Single =
                hostBoundsPx.Top + ((hostBoundsPx.Height - h) * 0.5F)

            Return PixelSnap.SnapRectHalf(New SKRect(left, top, left + w, top + h))

        End Function

#End Region
    End Class

End Namespace
