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 navigation policy for MASPagination. It centralizes page-count clamping,
    ''' current-page clamping, odd page-window policy, visible-page planning, keyboard movement,
    ''' RTL label selection, and same-surface data-pager boundaries without adding a data source,
    ''' query service, virtualization owner, router, popup, wrapper, or adapter.
    ''' </summary>
    Friend NotInheritable Class MASPaginationNavigationPolicy
        Private Sub New()
        End Sub

        Friend Shared Function NormalizeTotalPages(value As Integer) As Integer
            If value < 1 Then Return 1
            If value > PaginationTokens.MaximumTotalPages Then Return PaginationTokens.MaximumTotalPages
            Return value
        End Function

        Friend Shared Function NormalizeCurrentPage(value As Integer,
                                                    totalPages As Integer) As Integer
            Dim maxPage As Integer = NormalizeTotalPages(totalPages)
            If value < 1 Then Return 1
            If value > maxPage Then Return maxPage
            Return value
        End Function

        Friend Shared Function NormalizePageWindow(value As Integer) As Integer
            Dim normalized As Integer = value
            If normalized < PaginationTokens.MinimumPageWindow Then normalized = PaginationTokens.MinimumPageWindow
            If normalized > PaginationTokens.MaximumPageWindow Then normalized = PaginationTokens.MaximumPageWindow
            If normalized Mod 2 = 0 Then normalized += 1
            If normalized > PaginationTokens.MaximumPageWindow Then normalized = PaginationTokens.MaximumPageWindow
            Return normalized
        End Function

        Friend Shared Function ResolveKeyboardPage(keyCode As Keys,
                                                   currentPage As Integer,
                                                   totalPages As Integer,
                                                   isRtl As Boolean) As Integer
            Select Case keyCode
                Case Keys.Left
                    Return If(isRtl, NormalizeCurrentPage(currentPage + 1, totalPages), NormalizeCurrentPage(currentPage - 1, totalPages))
                Case Keys.Right
                    Return If(isRtl, NormalizeCurrentPage(currentPage - 1, totalPages), NormalizeCurrentPage(currentPage + 1, totalPages))
                Case Keys.Home
                    Return 1
                Case Keys.End
                    Return NormalizeTotalPages(totalPages)
                Case Keys.PageUp
                    Return NormalizeCurrentPage(currentPage - PaginationTokens.JumpStep, totalPages)
                Case Keys.PageDown
                    Return NormalizeCurrentPage(currentPage + PaginationTokens.JumpStep, totalPages)
                Case Else
                    Return currentPage
            End Select
        End Function

        Friend Shared Function ResolvePreviousText(isRtl As Boolean) As String
            If isRtl Then Return PaginationTokens.RtlPreviousText
            Return PaginationTokens.PreviousText
        End Function

        Friend Shared Function ResolveNextText(isRtl As Boolean) As String
            If isRtl Then Return PaginationTokens.RtlNextText
            Return PaginationTokens.NextText
        End Function

        Friend Shared Function ResolveRightToLeft() As Boolean
            Return Nexamas.UI.Localization.MASLocalization.IsCurrentRightToLeft()
        End Function

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

        Friend Shared ReadOnly Property HasNoVirtualizationOwner As Boolean
            Get
                Return True
            End Get
        End Property
    End Class

End Namespace
