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 "Helpers"

        Private Sub SyncProgressBar()
            _progressBar.SetRange(_minimum, _maximum)
            _progressBar.Value = _value
            _progressBar.VisualState = _visualState
            UpdatePercentText()
        End Sub

        Private Sub UpdatePercentText()
            _percentLabel.Text = MASOperationProgressBoxPolicy.ResolvePercentText(_minimum, _maximum, _value)
        End Sub

        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 NearlyEqual(a As Double,
                                            b As Double) As Boolean
            Return MASProgressBarValuePolicy.NearlyEqual(a, b)
        End Function

        Private Shared Function NormalizeMaterialKey(value As String) As String
            Return MASProgressBarValuePolicy.NormalizeContainerMaterialKey(value)
        End Function

        Private Shared Function NormalizeSurfaceStrength(value As MASSurfaceStrength) As MASSurfaceStrength
            Return MASProgressBarValuePolicy.NormalizeSurfaceStrength(value)
        End Function

#End Region


#Region "Governance"

        Friend Function CreateOperationProgressBoxReadinessManifest() As MASOperationProgressBoxReadinessManifest
            Return MASOperationProgressBoxReadinessManifest.CreateCurrent()
        End Function

#End Region

#Region "Dispose"

        Protected Overrides Sub Dispose(disposing As Boolean)
            If disposing Then
                _progressBar.Label = Nothing
                _titleLabel.Dispose()
                _statusLabel.Dispose()
                _percentLabel.Dispose()
                _progressBar.Dispose()
                _renderer.Dispose()
            End If

            MyBase.Dispose(disposing)
        End Sub

#End Region
    End Class

End Namespace
