Option Strict On
Option Explicit On

Imports System
Imports System.Globalization
Imports System.Windows.Forms
Imports Nexamas.UI.Values

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-owned visual and interaction policy for MASTimeline. It centralizes item text
    ''' hygiene, state normalization, selected-index clamping, compact row planning, keyboard
    ''' selection direction, RTL detection, and large-list ceiling facts while keeping audit
    ''' persistence, workflow engines, schedulers, live feeds, and history services outside the
    ''' control.
    ''' </summary>
    Friend NotInheritable Class MASTimelineVisualPolicy
        Private Sub New()
        End Sub

        Friend Shared Function NormalizeText(value As String,
                                             fallback As String) As String
            Dim text As String = If(value, String.Empty).Trim()
            If text.Length = 0 Then text = If(fallback, String.Empty)
            If text.Length > 180 Then text = text.Substring(0, 180)
            Return text
        End Function

        Friend Shared Function NormalizeState(value As MASTimelineItemState) As MASTimelineItemState
            Select Case value
                Case MASTimelineItemState.Normal,
                     MASTimelineItemState.Completed,
                     MASTimelineItemState.Current,
                     MASTimelineItemState.Warning,
                     MASTimelineItemState.[Error],
                     MASTimelineItemState.Disabled
                    Return value
                Case Else
                    Return MASTimelineItemState.Normal
            End Select
        End Function

        Friend Shared Function NormalizeSelectedIndex(value As Integer,
                                                      itemCount As Integer) As Integer
            If itemCount <= 0 Then Return -1
            If value < 0 Then Return -1
            If value >= itemCount Then Return itemCount - 1
            Return value
        End Function

        Friend Shared Function NormalizeMaxItems(itemCount As Integer) As Integer
            If itemCount < 0 Then Return 0
            Return Math.Min(itemCount, TimelineTokens.MaxItems)
        End Function

        Friend Shared Function ResolveRowHeightDip(compact As Boolean) As Single
            If compact Then Return TimelineTokens.DenseRowHeightDip
            Return TimelineTokens.RowHeightDip
        End Function

        Friend Shared Function ShouldDrawActiveMarkerRing(state As MASTimelineItemState,
                                                         selected As Boolean) As Boolean
            Return selected OrElse state = MASTimelineItemState.Current
        End Function

        Friend Shared Function ResolveActiveMarkerRingSizeDip(markerSizeDip As Single) As Single
            Dim markerSize As Single = Math.Max(1.0F, markerSizeDip)
            Return Math.Max(TimelineTokens.ActiveMarkerRingSizeDip, markerSize + 8.0F)
        End Function

        Friend Shared Function ResolveKeyboardSelectionDelta(keyCode As Keys,
                                                             isRtl As Boolean) As Integer
            Select Case keyCode
                Case Keys.Down
                    Return 1
                Case Keys.Up
                    Return -1
                Case Keys.Right
                    Return If(isRtl, -1, 1)
                Case Keys.Left
                    Return If(isRtl, 1, -1)
                Case Else
                    Return 0
            End Select
        End Function

        Friend Shared Function ResolveTextIsRtl(text As String) As Boolean
            If Nexamas.UI.Localization.MASLocalization.IsCurrentRightToLeft() Then Return True

            Dim value As String = If(text, String.Empty)
            For Each ch As Char In value
                Dim code As Integer = AscW(ch)
                If code >= &H590 AndAlso code <= &H8FF Then Return True
            Next
            Return False
        End Function

        Friend Shared ReadOnly Property HasNoParallelHistoryPath As Boolean
            Get
                Return True
            End Get
        End Property

        Friend Shared ReadOnly Property HasNoRunnerOrSchedulerPath As Boolean
            Get
                Return True
            End Get
        End Property

        Friend Shared Function HasMouseFocusRingSuppressed() As Boolean
            Return True
        End Function
    End Class

End Namespace
