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 Parsing And Formatting"

        Friend Shared Function NormalizeNullableTime(value As Nullable(Of TimeSpan),
                                                     minuteStep As Integer) As Nullable(Of TimeSpan)
            If Not value.HasValue Then Return Nothing
            Return NormalizeTime(value.Value, minuteStep)
        End Function

        Friend Shared Function NormalizeTime(value As TimeSpan,
                                             minuteStep As Integer) As TimeSpan
            Dim normalizedStep As Integer = NormalizeMinuteStep(minuteStep)
            If normalizedStep < 1 Then normalizedStep = 1
            Dim minutesPerDay As Integer = 24 * 60
            Dim totalMinutes As Integer = CInt(Math.Floor(value.TotalMinutes))
            totalMinutes = ((totalMinutes Mod minutesPerDay) + minutesPerDay) Mod minutesPerDay
            Return TimeSpan.FromMinutes(totalMinutes)
        End Function

        Friend Shared Function ResolveCurrentTimeOfDay() As TimeSpan
            Return NormalizeTime(Nexamas.UI.Timing.MASInteractionClock.LocalNow().TimeOfDay, 1)
        End Function

        Friend Shared Function NormalizeMinuteStep(value As Integer) As Integer
            If value < 1 Then Return 1
            If value > 60 Then Return 60
            Return value
        End Function

        Friend Shared Function TryParseTimeText(text As String,
                                                culture As CultureInfo,
                                                ByRef value As TimeSpan) As Boolean
            value = TimeSpan.Zero
            Dim raw As String = If(text, String.Empty).Trim()
            If raw.Length = 0 Then Return False

            Dim effectiveCulture As CultureInfo = If(culture, CultureInfo.CurrentCulture)
            Dim parsedTime As TimeSpan
            If TimeSpan.TryParse(raw, effectiveCulture, parsedTime) Then
                value = NormalizeTime(parsedTime, 1)
                Return True
            End If

            Dim parsedDate As DateTime
            If DateTime.TryParse(raw, effectiveCulture, DateTimeStyles.AllowWhiteSpaces, parsedDate) Then
                value = NormalizeTime(parsedDate.TimeOfDay, 1)
                Return True
            End If

            Return False
        End Function

        Friend Shared Function FormatTime(value As TimeSpan) As String
            Dim normalized As TimeSpan = NormalizeTime(value, 1)
            Dim displayDate As DateTime = DateTime.Today.Add(normalized)
            Return displayDate.ToString("t", CultureInfo.CurrentCulture)
        End Function

        Private Shared Function NullableTimesEqual(a As Nullable(Of TimeSpan),
                                                   b As Nullable(Of TimeSpan)) As Boolean
            If a.HasValue <> b.HasValue Then Return False
            If Not a.HasValue Then Return True
            Return a.Value = b.Value
        End Function

#End Region

    End Class

End Namespace
