Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Rendering
Imports SkiaSharp

Namespace Nexamas.UI.Application

    ''' <summary>
    ''' Internal Application-layer facade for drawing MASPanelShell chrome inside Nexamas UI-owned layered scenes.
    ''' It keeps raw PanelShell render contracts inside the assembly while exposing only stable
    ''' high-level gateways such as SelectionPanel and StartupShell services to external projects.
    ''' </summary>
    <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Advanced)>
    Friend NotInheritable Class MASApplicationPanelShellChrome

        Private Sub New()
        End Sub

        Friend Shared Function Measure(dpi As Single,
                                       spec As MASPanelShellSpec) As MASApplicationPanelShellChromeResult

            If spec Is Nothing Then Return MASApplicationPanelShellChromeResult.Empty

            Dim coreResult As MASPanelShellResult =
                MASPanelShellRenderer.Measure(
                    dpi:=NormalizeDpi(dpi),
                    spec:=spec)

            Return FromCoreResult(coreResult)

        End Function

        Friend Shared Function Draw(canvas As SKCanvas,
                                    context As MASApplicationLayeredSceneContext,
                                    spec As MASPanelShellSpec) As MASApplicationPanelShellChromeResult

            If canvas Is Nothing Then Return MASApplicationPanelShellChromeResult.Empty
            If context Is Nothing Then Return MASApplicationPanelShellChromeResult.Empty
            If spec Is Nothing Then Return MASApplicationPanelShellChromeResult.Empty

            Dim coreResult As MASPanelShellResult =
                MASPanelShellRenderer.Draw(
                    canvas:=canvas,
                    ctx:=context.ThemeContextCore,
                    dpi:=NormalizeDpi(context.Dpi),
                    spec:=spec)

            Return FromCoreResult(coreResult)

        End Function

        Private Shared Function FromCoreResult(coreResult As MASPanelShellResult) As MASApplicationPanelShellChromeResult

            If coreResult Is Nothing Then Return MASApplicationPanelShellChromeResult.Empty

            Return New MASApplicationPanelShellChromeResult(
                baseRectPx:=coreResult.BaseRectPx,
                headerRectPx:=coreResult.HeaderRectPx,
                contentRectPx:=coreResult.ContentRectPx,
                closeButtonRectPx:=coreResult.HeaderCloseRectPx,
                radiusPx:=coreResult.RadiusPx,
                hasHeader:=coreResult.HasHeader)

        End Function

        Private Shared Function NormalizeDpi(value As Single) As Single
            If value <= 0.0F OrElse Single.IsNaN(value) OrElse Single.IsInfinity(value) Then Return 1.0F
            Return Math.Max(0.75F, value)
        End Function

    End Class

End Namespace
