Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports Nexamas.UI.Components
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Theming

Namespace Nexamas.UI.Application

    ''' <summary>
    ''' Internal command section used by Nexamas UI-owned PanelShell surfaces.
    ''' Public callers choose actions through high-level gateway options; button order,
    ''' sizing, edge inset, gap, and alignment remain owned by Nexamas.UI.
    ''' </summary>
    Friend NotInheritable Class MASApplicationPanelShellCommandBar

        Private ReadOnly _actions As New List(Of MASApplicationPanelAction)()
        Private ReadOnly _defaultCloseOnClick As Boolean

        Friend Sub New(visible As Boolean,
                       defaultCloseOnClick As Boolean,
                       alignment As MASApplicationPanelShellAlignment,
                       heightPx As Single,
                       buttonWidthPx As Single,
                       buttonHeightPx As Single,
                       buttonGapPx As Single,
                       edgeInsetPx As Single,
                       contentGapPx As Single)

            Me.Visible = visible
            _defaultCloseOnClick = defaultCloseOnClick
            Me.Alignment = alignment
            Me.HeightPx = heightPx
            Me.ButtonWidthPx = buttonWidthPx
            Me.ButtonHeightPx = buttonHeightPx
            Me.ButtonGapPx = buttonGapPx
            Me.EdgeInsetPx = edgeInsetPx
            Me.ContentGapPx = contentGapPx

        End Sub

        ''' <summary>
        ''' Enables or disables this command section without clearing its actions.
        ''' </summary>
        Friend Property Visible As Boolean

        ''' <summary>
        ''' Horizontal placement of the command group inside the section.
        ''' </summary>
        Friend Property Alignment As MASApplicationPanelShellAlignment

        ''' <summary>
        ''' Reserved section height in pixels before DPI conversion.
        ''' </summary>
        Friend Property HeightPx As Single

        ''' <summary>
        ''' Default width for generated MASButton actions.
        ''' </summary>
        Friend Property ButtonWidthPx As Single

        ''' <summary>
        ''' Default visual height for generated MASButton actions.
        ''' </summary>
        Friend Property ButtonHeightPx As Single

        ''' <summary>
        ''' Horizontal gap between generated action buttons.
        ''' </summary>
        Friend Property ButtonGapPx As Single

        ''' <summary>
        ''' Distance from the aligned outer edge before the action group starts.
        ''' </summary>
        Friend Property EdgeInsetPx As Single

        ''' <summary>
        ''' Gap reserved between this section and the content body.
        ''' For Toolbar this is the gap below; for Footer this is the gap above.
        ''' </summary>
        Friend Property ContentGapPx As Single

        Friend ReadOnly Property Actions As IList(Of MASApplicationPanelAction)
            Get
                Return _actions
            End Get
        End Property

        ''' <summary>
        ''' Applies an owned Nexamas UI action-group metric preset. This keeps generated
        ''' PanelShell command sections aligned with MAS ActionGroup decisions without
        ''' exposing raw button geometry to public SDK consumers.
        ''' </summary>
        Friend Sub ConfigureActionMetrics(sectionHeightPx As Single,
                                          buttonWidthPx As Single,
                                          buttonHeightPx As Single,
                                          buttonGapPx As Single,
                                          contentGapPx As Single)

            HeightPx = SafeNonNegative(sectionHeightPx)
            ButtonWidthPx = SafeNonNegative(buttonWidthPx)
            ButtonHeightPx = SafeNonNegative(buttonHeightPx)
            ButtonGapPx = SafeNonNegative(buttonGapPx)
            ContentGapPx = SafeNonNegative(contentGapPx)

        End Sub

        Friend Function AddButton(text As String,
                                  commandId As String,
                                  Optional intent As MASButtonIntent = MASButtonIntent.Default) As MASApplicationPanelAction

            Return AddButton(text, commandId, intent, _defaultCloseOnClick)

        End Function

        Friend Function AddButton(text As String,
                                  commandId As String,
                                  intent As MASButtonIntent,
                                  closeOnClick As Boolean) As MASApplicationPanelAction

            Dim actionItem As MASApplicationPanelAction =
                MASApplicationPanelAction.Create(text, commandId, intent, closeOnClick)

            _actions.Add(actionItem)
            Return actionItem

        End Function

        Friend Function AddPrimary(text As String,
                                   commandId As String) As MASApplicationPanelAction

            Return AddButton(text, commandId, MASButtonIntent.Primary, _defaultCloseOnClick)

        End Function

        Friend Function AddSecondary(text As String,
                                     commandId As String) As MASApplicationPanelAction

            Return AddButton(text, commandId, MASButtonIntent.Subtle, _defaultCloseOnClick)

        End Function

        Friend Function AddDanger(text As String,
                                  commandId As String) As MASApplicationPanelAction

            Return AddButton(text, commandId, MASButtonIntent.Danger, _defaultCloseOnClick)

        End Function

        Friend Sub Clear()
            _actions.Clear()
        End Sub

        Private Shared Function SafeNonNegative(value As Single) As Single
            Return MASIntrinsicControlSizeMetrics.SanitizeLength(value)
        End Function

    End Class

End Namespace
