Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports Nexamas.UI.Controls
Imports Nexamas.UI.Host
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Theming
Imports SkiaSharp

Namespace Nexamas.UI.Application


    ''' <summary>
    ''' Public stack builder for advanced page regions that still consume the official MASLayout engine.
    ''' </summary>
    Public NotInheritable Class MASApplicationLayoutStackBuilder
        Private ReadOnly _children As New List(Of MASApplicationLayoutControlEntry)()
        Private ReadOnly _orientation As MASLayoutOrientation
        Private _spacing As Single = MASApplicationLayoutSizeTokens.StackSpacing
        Private _padding As MASLayoutInset = MASLayoutInset.Empty
        Private _alignment As MASLayoutCrossAlignment = MASLayoutCrossAlignment.Stretch
        Private _groupSizingMode As MASLayoutGroupSizingMode = MASLayoutGroupSizingMode.Intrinsic

        Friend Sub New(orientation As MASLayoutOrientation)
            _orientation = orientation
        End Sub

        <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Advanced)>
        Friend Function Spacing(value As Single) As MASApplicationLayoutStackBuilder
            _spacing = MASApplicationLayoutPageBuilder.SafeNonNegative(value)
            Return Me
        End Function

        Public Function Spacing(value As MASLayoutSpacing) As MASApplicationLayoutStackBuilder
            _spacing = MASLayoutSpacingResolver.ResolveBase(value)
            Return Me
        End Function

        <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Advanced)>
        Friend Function Padding(all As Single) As MASApplicationLayoutStackBuilder
            _padding = New MASLayoutInset(all)
            Return Me
        End Function

        Public Function Padding(value As MASLayoutSpacing) As MASApplicationLayoutStackBuilder
            _padding = MASLayoutSpacingResolver.ResolveInsetBase(value)
            Return Me
        End Function

        Public Function Stretch() As MASApplicationLayoutStackBuilder
            _alignment = MASLayoutCrossAlignment.Stretch
            Return Me
        End Function

        Public Function HugContent() As MASApplicationLayoutStackBuilder
            _alignment = MASLayoutCrossAlignment.Start
            Return Me
        End Function

        ''' <summary>
        ''' Makes every item in this stack use the largest measured item width, without hard-coded widths.
        ''' </summary>
        Public Function EqualItemWidth() As MASApplicationLayoutStackBuilder
            _groupSizingMode = MASLayoutGroupSizingMode.EqualWidth
            Return Me
        End Function

        ''' <summary>
        ''' Makes every item in this stack use the largest measured width and height from the group.
        ''' </summary>
        Public Function EqualItemSize() As MASApplicationLayoutStackBuilder
            _groupSizingMode = MASLayoutGroupSizingMode.EqualSize
            Return Me
        End Function

        ''' <summary>
        ''' Restores per-control intrinsic item sizing for this stack.
        ''' </summary>
        Public Function IntrinsicItemSize() As MASApplicationLayoutStackBuilder
            _groupSizingMode = MASLayoutGroupSizingMode.Intrinsic
            Return Me
        End Function

        Public Function Add(control As MASControlBase,
                            Optional sizeIntent As MASSize = Nothing) As MASApplicationLayoutStackBuilder
            If control IsNot Nothing Then
                ' Stack alignment is a group-level decision owned by MASStackLayout.
                ' The child node must not capture the builder's current alignment at Add-time,
                ' otherwise a later Stretch/HugContent call would leave earlier children with stale
                ' alignment facts.  A stretch-capable child inside a hug-content cross budget still
                ' resolves to the measured cross width, so this remains correct for both modes.
                _children.Add(New MASApplicationLayoutControlEntry(control, sizeIntent, MASLayoutCrossAlignment.Stretch))
            End If

            Return Me
        End Function

        Friend Function BuildNode(Optional sizeProfile As MASSizeProfile = Nothing) As MASLayoutNode
            Dim profile As MASSizeProfile = MASSizeProfile.Normalize(sizeProfile)
            Dim nodes As New List(Of MASLayoutNode)()
            For Each child As MASApplicationLayoutControlEntry In _children
                nodes.Add(child.BuildNode())
            Next

            Dim scaledSpacing As Single = MASLayoutSpacingResolver.ScaleLength(_spacing, profile)
            Dim scaledPadding As MASLayoutInset = MASLayoutSpacingResolver.ScaleInset(_padding, profile)

            If _orientation = MASLayoutOrientation.Horizontal Then
                Return MASLayoutBuilder.StackHorizontal(nodes, scaledSpacing, scaledPadding, _alignment, _groupSizingMode)
            End If

            Return MASLayoutBuilder.StackVertical(nodes, scaledSpacing, scaledPadding, _alignment, _groupSizingMode)
        End Function

        Friend Sub CollectControls(target As IList(Of MASControlBase))
            If target Is Nothing Then Return
            For Each child As MASApplicationLayoutControlEntry In _children
                child.CollectControls(target)
            Next
        End Sub
    End Class

End Namespace
