Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Rendering
Imports SkiaSharp

Namespace Nexamas.UI.Application

    ''' <summary>
    ''' Internal options for showing Nexamas UI-owned content inside the reusable
    ''' MASPanelShell FloatRuntime surface.
    '''
    ''' This low-level contract intentionally remains inside the assembly. Public
    ''' SDK consumers must use high-level gateways such as ShowSelectionPanel so
    ''' Skia geometry, content hosts, command bars, and scroll placement do not leak
    ''' outside Nexamas.UI.
    ''' </summary>
    Friend NotInheritable Class MASApplicationPanelShellOptions

        Private ReadOnly _toolbar As MASApplicationPanelShellCommandBar
        Private ReadOnly _content As MASApplicationPanelShellContentSection
        Private ReadOnly _footer As MASApplicationPanelShellCommandBar
        Private _shellOptions As MASPanelShellDialogOptions

        Friend Sub New()

            _toolbar = New MASApplicationPanelShellCommandBar(
                visible:=True,
                defaultCloseOnClick:=False,
                alignment:=MASApplicationPanelShellAlignment.Start,
                heightPx:=MASApplicationSurfaceSizeTokens.PanelShellToolbarHeightDip,
                buttonWidthPx:=MASApplicationSurfaceSizeTokens.PanelShellToolbarButtonWidthDip,
                buttonHeightPx:=MASApplicationSurfaceSizeTokens.PanelShellToolbarButtonHeightDip,
                buttonGapPx:=MASApplicationSurfaceSizeTokens.PanelShellToolbarButtonGapDip,
                edgeInsetPx:=MASApplicationSurfaceSizeTokens.PanelShellSectionEdgeInsetDip,
                contentGapPx:=MASApplicationSurfaceSizeTokens.PanelShellToolbarContentGapDip)

            _content = New MASApplicationPanelShellContentSection()
            _shellOptions = MASPanelShellDialogOptions.SelectionDialog("MAS")

            _footer = New MASApplicationPanelShellCommandBar(
                visible:=True,
                defaultCloseOnClick:=True,
                alignment:=MASApplicationPanelShellAlignment.Start,
                heightPx:=MASApplicationSurfaceSizeTokens.PanelShellFooterSectionHeightDip,
                buttonWidthPx:=MASApplicationSurfaceSizeTokens.PanelShellFooterActionWidthDip,
                buttonHeightPx:=MASApplicationSurfaceSizeTokens.PanelShellFooterActionHeightDip,
                buttonGapPx:=MASApplicationSurfaceSizeTokens.PanelShellFooterActionGapDip,
                edgeInsetPx:=MASApplicationSurfaceSizeTokens.PanelShellSectionEdgeInsetDip,
                contentGapPx:=MASApplicationSurfaceSizeTokens.PanelShellFooterBodyGapDip)

        End Sub

        Friend Property Name As String = "MASApplicationPanelShell"
        Friend Property ShellOptions As MASPanelShellDialogOptions
            Get
                Return MASPanelShellDialogOptions.SafeClone(_shellOptions)
            End Get
            Set(value As MASPanelShellDialogOptions)
                _shellOptions = MASPanelShellDialogOptions.SafeClone(value)
            End Set
        End Property
        Friend Property AvailableBoundsPx As SKRect = SKRect.Empty
        Friend Property WidthRatio As Single = MASApplicationSurfaceSizeTokens.PanelShellDefaultWidthRatio
        Friend Property HeightRatio As Single = MASApplicationSurfaceSizeTokens.PanelShellDefaultHeightRatio
        Friend Property MinWidthPx As Single = MASApplicationSurfaceSizeTokens.PanelShellDefaultMinWidthDip
        Friend Property MinHeightPx As Single = MASApplicationSurfaceSizeTokens.PanelShellDefaultMinHeightDip
        Friend Property MaxWidthPx As Single = MASApplicationSurfaceSizeTokens.PanelShellDefaultMaxWidthDip
        Friend Property MaxHeightPx As Single = MASApplicationSurfaceSizeTokens.PanelShellDefaultMaxHeightDip
        Friend Property VisualPadPx As Single = 0.0F
        Friend Property TopVisualPadPx As Single = 0.0F


        Friend Property AcceptsTextInput As Boolean = False
        Friend Property CloseOnEscape As Boolean = True
        Friend Property CloseOnOutsidePointer As Boolean = False
        Friend Property SwallowOutsidePointer As Boolean = True
        Friend Property UserState As Object
        Friend Property ContentBuilder As Action(Of MASPanelShellContentHost, SKRect)
        Friend Property Completed As Action(Of MASApplicationPanelResult)

        ''' <summary>
        ''' Applies an owned PanelShell surface-size preset. Public SDK consumers never set
        ''' these pixel metrics directly; high-level application gateways pick semantic presets.
        ''' </summary>
        Friend Sub ConfigureSurfaceSize(minWidthPx As Single,
                                        minHeightPx As Single,
                                        maxWidthPx As Single,
                                        maxHeightPx As Single,
                                        widthRatio As Single,
                                        heightRatio As Single)

            Me.MinWidthPx = SafePositive(minWidthPx, MASApplicationSurfaceSizeTokens.PanelShellDefaultMinWidthDip)
            Me.MinHeightPx = SafePositive(minHeightPx, MASApplicationSurfaceSizeTokens.PanelShellDefaultMinHeightDip)
            Me.MaxWidthPx = Math.Max(Me.MinWidthPx, SafePositive(maxWidthPx, MASApplicationSurfaceSizeTokens.PanelShellDefaultMaxWidthDip))
            Me.MaxHeightPx = Math.Max(Me.MinHeightPx, SafePositive(maxHeightPx, MASApplicationSurfaceSizeTokens.PanelShellDefaultMaxHeightDip))
            Me.WidthRatio = SafeRatio(widthRatio, MASApplicationSurfaceSizeTokens.PanelShellDefaultWidthRatio)
            Me.HeightRatio = SafeRatio(heightRatio, MASApplicationSurfaceSizeTokens.PanelShellDefaultHeightRatio)

        End Sub

        ''' <summary>
        ''' Official top command section for tool/action buttons above the content body.
        ''' </summary>
        Friend ReadOnly Property Toolbar As MASApplicationPanelShellCommandBar
            Get
                Return _toolbar
            End Get
        End Property

        ''' <summary>
        ''' Official body section for caller-owned feature controls and body scroll placement.
        ''' </summary>
        Friend ReadOnly Property Content As MASApplicationPanelShellContentSection
            Get
                Return _content
            End Get
        End Property

        ''' <summary>
        ''' Official bottom command section for dialog actions.
        ''' </summary>
        Friend ReadOnly Property Footer As MASApplicationPanelShellCommandBar
            Get
                Return _footer
            End Get
        End Property

        Private Shared Function SafePositive(value As Single, fallback As Single) As Single
            If Single.IsNaN(value) OrElse Single.IsInfinity(value) OrElse value <= 0.0F Then Return fallback
            Return value
        End Function

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

    End Class

End Namespace
