Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports Nexamas.UI.General
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports Nexamas.UI.Architecture
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Composition
Imports SkiaSharp

Namespace Nexamas.UI.Controls


    Partial Public NotInheritable Class MASGroupBox
#Region "Size/Layout Contract"

        Friend Function GetPreferredLayoutSize(context As MASLayoutMeasureContext) As SKSize Implements IMASLayoutParticipant.GetPreferredLayoutSize
            Return MeasureIntrinsicSize(CreateSizeContext(context), SizeIntent).DesiredSize
        End Function

        Friend Function GetMinLayoutSize(context As MASLayoutMeasureContext) As SKSize Implements IMASLayoutParticipant.GetMinLayoutSize
            Return MeasureIntrinsicSize(CreateSizeContext(context), SizeIntent).MinSize
        End Function

        Friend Function MeasureIntrinsicSize(context As MASSizeContext, intent As MASSize) As MASSizeResult Implements IMASIntrinsicSizeContract.MeasureIntrinsicSize
            Return MeasureSurfaceLayout(context, ResolveAvailableSize(context), intent)
        End Function

        Friend Function MeasureSurfaceLayout(context As MASSizeContext,
                                             availableSize As SKSize,
                                             intent As MASSize) As MASSizeResult Implements IMASSurfaceLayoutContract.MeasureSurfaceLayout
            Dim safeContext As MASSizeContext = EnsureSizeContext(context, availableSize)
            Dim headerHeight As Single = ResolveHeaderHeightLogical()
            Dim titleWidth As Single = MeasureTitleWidth(safeContext)
            Dim existingSize As SKSize = ResolveExistingLayoutSize()

            Dim characteristics As New MASSurfaceLayoutCharacteristics(
                titleWidth:=titleWidth,
                headerHeight:=headerHeight,
                contentInset:=New MASLayoutInset(_paddingLeft, headerHeight + _paddingTop, _paddingRight, _paddingBottom),
                hasShadow:=_drawShadow,
                existingSize:=existingSize)

            Return MASSurfaceLayoutSizeResolver.Resolve(
                MASSurfaceLayoutKind.GroupBox,
                safeContext,
                availableSize,
                intent,
                characteristics)
        End Function

        Private Shared Function EnsureSizeContext(context As MASSizeContext, availableSize As SKSize) As MASSizeContext
            If context IsNot Nothing Then Return context
            Return New MASSizeContext(availableSize)
        End Function

        Private Shared Function ResolveAvailableSize(context As MASSizeContext) As SKSize
            If context Is Nothing Then Return New SKSize(Single.PositiveInfinity, Single.PositiveInfinity)
            Return context.AvailableSize
        End Function

        Private Shared Function CreateSizeContext(context As MASLayoutMeasureContext) As MASSizeContext
            If context Is Nothing Then Return New MASSizeContext(New SKSize(Single.PositiveInfinity, Single.PositiveInfinity))
            Return context.ToSizeContext(context.AvailableSize)
        End Function

        Private Function MeasureTitleWidth(context As MASSizeContext) As Single
            If String.IsNullOrWhiteSpace(_title) Then Return 0.0F

            Return MASLayoutTextMeasureGateway.Measure(
                context.IntegrationContext,
                _title,
                MASTypography.MASTextStyle.Button,
                Single.PositiveInfinity,
                allowWrap:=False).Size.Width
        End Function

        Private Function ResolveExistingLayoutSize() As SKSize
            If BoundsLogical.IsEmpty Then Return SKSize.Empty
            Return New SKSize(BoundsLogical.Width, BoundsLogical.Height)
        End Function

#End Region

    End Class

End Namespace
