Option Strict On
Option Explicit On

Imports System
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 MASProgressBar
#Region "Helpers"

        Private Function NormalizeRange() As Boolean
            Dim previousValue As Double = _value
            Dim snapshot As MASProgressBarRangeSnapshot = MASProgressBarValuePolicy.NormalizeRange(_minimum, _maximum, _value)
            _minimum = snapshot.Minimum
            _maximum = snapshot.Maximum
            _value = snapshot.Value
            Return Not NearlyEqual(previousValue, _value)
        End Function

        Private Shared Function NormalizeRangeEndpoint(value As Double,
                                                       fallback As Double) As Double
            Return MASProgressBarValuePolicy.NormalizeRangeEndpoint(value, fallback)
        End Function

        Private Shared Function NormalizePercent(value As Double) As Double
            Return MASProgressBarValuePolicy.NormalizePercent(value)
        End Function

        Private Shared Function ResolveProgressFraction(minimum As Double,
                                                        maximum As Double,
                                                        value As Double) As Single
            Return MASProgressBarValuePolicy.ResolveProgressFraction(minimum, maximum, value)
        End Function

        Private Shared Function Clamp(v As Double, mn As Double, mx As Double) As Double
            Return MASProgressBarValuePolicy.Clamp(v, mn, mx)
        End Function

        Private Shared Function SafeNonNegative(value As Single) As Single
            Return MASProgressBarValuePolicy.NormalizeLogicalExtent(value)
        End Function

        Private Shared Function NearlyEqual(a As Single, b As Single) As Boolean
            Return MASProgressBarValuePolicy.NearlyEqual(a, b)
        End Function

        Private Shared Function NearlyEqual(a As Double, b As Double) As Boolean
            Return MASProgressBarValuePolicy.NearlyEqual(a, b)
        End Function

#End Region


#Region "Governance"

        Friend Function CreateProgressBarReadinessManifest() As MASProgressBarReadinessManifest
            Return MASProgressBarReadinessManifest.CreateCurrent()
        End Function

#End Region

#Region "Dispose"

        Protected Overrides Sub Dispose(disposing As Boolean)
            If disposing Then
                StopValueMotion(syncToSemanticValue:=True)

                If _label IsNot Nothing Then
                    RemoveHandler _label.TextChanged, AddressOf OnLabelContentChanged
                    _label = Nothing
                End If

                _renderer.Dispose()
            End If

            MyBase.Dispose(disposing)
        End Sub

#End Region
    End Class

End Namespace
