Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.General
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Pure layout math for reusable media buttons. It contains no drawing and no
    ''' ImageViewer-specific command logic.
    ''' </summary>
    Friend NotInheritable Class MASMediaButtonLayoutResolver

        Private Sub New()
        End Sub

        Friend Shared Function ResolveRadius(rect As SKRect,
                                             dpi As Single,
                                             compact As Boolean) As Single
            Dim safeDpi As Single = PixelSnap.SafeDpi(dpi)
            Dim target As Single
            If compact Then
                target = Math.Max(12.0F * safeDpi, rect.Height * 0.36F)
            Else
                target = Math.Min(15.5F * safeDpi, rect.Height * 0.215F)
            End If
            Return PixelSnap.SafeRadius(Math.Min(target, Math.Min(rect.Width, rect.Height) * 0.5F))
        End Function

        Friend Shared Function ResolveContentProfile(rect As SKRect,
                                                     dpi As Single) As MASMediaButtonContentProfile
            Dim safeDpi As Single = PixelSnap.SafeDpi(dpi)

            Dim iconSize As Single = Math.Min(26.5F * safeDpi, Math.Min(rect.Width * 0.34F, rect.Height * 0.315F))
            iconSize = Math.Max(23.5F * safeDpi, iconSize)

            Dim labelTextSize As Single = Math.Max(13.2F * safeDpi, Math.Min(14.0F * safeDpi, rect.Height * 0.168F))
            Dim labelHeight As Single = Math.Max(19.0F * safeDpi, labelTextSize * 1.52F)
            Dim gap As Single = Math.Max(5.2F * safeDpi, rect.Height * 0.066F)

            Dim contentHeight As Single = iconSize + gap + labelHeight
            Dim groupTop As Single = rect.Top + (rect.Height - contentHeight) * 0.5F

            ' Slightly lower optical center: the reference tile reads as a single
            ' centered icon+label group, not as an icon pinned to the top edge.
            groupTop += Math.Max(0.4F * safeDpi, rect.Height * 0.008F)

            Dim iconTop As Single = groupTop
            Dim labelTop As Single = iconTop + iconSize + gap
            Dim labelBottom As Single = labelTop + labelHeight

            Dim minTop As Single = rect.Top + 9.0F * safeDpi
            If iconTop < minTop Then
                Dim delta As Single = minTop - iconTop
                iconTop += delta
                labelTop += delta
                labelBottom += delta
            End If

            Dim maxBottom As Single = rect.Bottom - 8.0F * safeDpi
            If labelBottom > maxBottom Then
                Dim delta As Single = labelBottom - maxBottom
                iconTop -= delta
                labelTop -= delta
                labelBottom -= delta
            End If

            Return New MASMediaButtonContentProfile With {
                .IconSize = iconSize,
                .IconTop = iconTop,
                .LabelTop = labelTop,
                .LabelBottom = labelBottom,
                .LabelSideInset = Math.Max(5.0F * safeDpi, rect.Width * 0.09F),
                .LabelTextSize = labelTextSize,
                .OpticalOffsetY = 0.0F
            }
        End Function

        Friend Shared Function OffsetRect(rect As SKRect,
                                          dx As Single,
                                          dy As Single) As SKRect
            Return New SKRect(rect.Left + dx, rect.Top + dy, rect.Right + dx, rect.Bottom + dy)
        End Function

    End Class

End Namespace
