Option Strict On
Option Explicit On

Imports System
Imports System.ComponentModel
Imports System.Globalization
Imports Nexamas.UI.Architecture
Imports Nexamas.UI.Components
Imports Nexamas.UI.Composition
Imports Nexamas.UI.General
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Controls

    Partial Public NotInheritable Class MASOperationProgressBox
#Region "Layout"

        ''' <summary>
        ''' Arranges the internal child controls only when the operation box geometry or text state changes.
        ''' It must not be called from Render; child placement mutates layout-owned bounds and can request
        ''' host invalidation, which would create repaint loops in layered startup/dialog surfaces.
        ''' </summary>
        Private Sub ArrangeChildren()
            Dim layout As MASOperationProgressBoxLayoutPlan = ResolveLayout(BoundsLogical)

            ApplyChildBoundsIfChanged(_titleLabel, layout.TitleBounds)
            ApplyChildBoundsIfChanged(_progressBar, layout.ProgressBounds)
            ApplyChildBoundsIfChanged(_statusLabel, layout.StatusBounds)

            _progressBar.RefreshLabelLayout()
        End Sub

        Private Shared Sub ApplyChildBoundsIfChanged(control As MASControlBase, bounds As SKRect)
            If control Is Nothing Then Return
            If SameRect(control.BoundsLogical, bounds) Then Return

            control.SetLocalLayoutBoundsInternal(bounds)
        End Sub

        Private Shared Function SameRect(left As SKRect, right As SKRect) As Boolean
            Const Epsilon As Single = 0.01F

            Return Math.Abs(left.Left - right.Left) <= Epsilon AndAlso
                   Math.Abs(left.Top - right.Top) <= Epsilon AndAlso
                   Math.Abs(left.Right - right.Right) <= Epsilon AndAlso
                   Math.Abs(left.Bottom - right.Bottom) <= Epsilon
        End Function

        Private Shared Function ResolveLayout(owner As SKRect) As MASOperationProgressBoxLayoutPlan
            Return MASOperationProgressBoxPolicy.BuildLayoutPlan(owner)
        End Function



#End Region
    End Class

End Namespace
