Option Strict On
Option Explicit On

Imports System
Imports System.Globalization
Imports System.Windows.Forms
Imports Nexamas.UI.Architecture
Imports Nexamas.UI.Components.ListPopup
Imports Nexamas.UI.Controls
Imports Nexamas.UI.FloatRuntime
Imports Nexamas.UI.General
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.TextRendering
Imports Nexamas.UI.Composition
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components

    Partial Public NotInheritable Class MASTimePicker
#Region "Time Value And Interaction State"

        Private Sub SetSelectedTimeInternal(value As Nullable(Of TimeSpan),
                                            raiseChanged As Boolean)
            Dim normalized As Nullable(Of TimeSpan) = NormalizeNullableTime(value, _minuteStep)
            If normalized.HasValue Then normalized = ClampToRange(normalized.Value)

            If NullableTimesEqual(_selectedTime, normalized) Then Return

            _selectedTime = normalized

            InvalidateLayoutSlotInternal()
            InvalidateVisual()

            If raiseChanged Then RaiseSelectedTimeChangedSafe()
        End Sub

        Private Sub AdjustSelectedTime(deltaMinutes As Integer)
            Dim basis As TimeSpan = If(_selectedTime.HasValue, _selectedTime.Value, ResolveWorkingTimeForOpen())
            SetSelectedTimeInternal(ClampToRange(NormalizeTime(basis.Add(TimeSpan.FromMinutes(deltaMinutes)), _minuteStep)), True)
        End Sub


        Private Sub MoveSelectedTimeToBoundary(preferMinimum As Boolean)
            Dim target As TimeSpan
            If preferMinimum AndAlso _minimumTime.HasValue Then
                target = _minimumTime.Value
            ElseIf (Not preferMinimum) AndAlso _maximumTime.HasValue Then
                target = _maximumTime.Value
            Else
                target = ResolveWorkingTimeForOpen()
            End If

            SetSelectedTimeInternal(ClampToRange(target), True)
        End Sub

        Private Function ResolveInteractionState() As MASInteractionState
            If Not Me.Enabled Then Return MASInteractionState.Disabled
            If _pressed Then Return MASInteractionState.Pressed
            If _isClockOpen Then Return MASInteractionState.Focused
            If HasKeyboardFocus Then Return MASInteractionState.Focused
            If _hover Then Return MASInteractionState.Hovered
            Return MASInteractionState.Enabled
        End Function

        Private Function ResolveWorkingTimeForOpen() As TimeSpan
            If _selectedTime.HasValue Then Return _selectedTime.Value

            Dim nowTime As TimeSpan = ResolveCurrentTimeOfDay()
            If IsTimeAllowed(nowTime) Then Return nowTime
            If _minimumTime.HasValue Then Return _minimumTime.Value
            If _maximumTime.HasValue Then Return _maximumTime.Value
            Return nowTime
        End Function

        Private Sub ResetInputAll()
            _hover = False
            _pressed = False
        End Sub

        Private Function IsTimeAllowed(value As TimeSpan) As Boolean
            Dim timeValue As TimeSpan = NormalizeTime(value, _minuteStep)
            If _minimumTime.HasValue AndAlso timeValue < _minimumTime.Value Then Return False
            If _maximumTime.HasValue AndAlso timeValue > _maximumTime.Value Then Return False
            Return True
        End Function

        Friend Function ClampToRange(value As TimeSpan) As TimeSpan
            Dim timeValue As TimeSpan = NormalizeTime(value, _minuteStep)
            If _minimumTime.HasValue AndAlso timeValue < _minimumTime.Value Then timeValue = _minimumTime.Value
            If _maximumTime.HasValue AndAlso timeValue > _maximumTime.Value Then timeValue = _maximumTime.Value
            Return timeValue
        End Function

#End Region

    End Class

End Namespace
