Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-owned ImageViewer visual and transformation policy. It centralizes text hygiene,
    ''' zoom normalization, right-angle rotation normalization, safe export quality, keyboard
    ''' navigation, and format mapping while keeping file picking, storage prompts, galleries,
    ''' thumbnails, metadata services, and background jobs outside the control.
    ''' </summary>
    Friend NotInheritable Class MASImageViewerVisualPolicy
        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 > 220 Then text = text.Substring(0, 220)
            Return text
        End Function

        Friend Shared Function NormalizeRotation(degrees As Integer) As Integer
            Dim value As Integer = degrees Mod 360
            If value < 0 Then value += 360
            value = CInt(Math.Round(value / 90.0R, MidpointRounding.AwayFromZero)) * 90
            value = value Mod 360
            If value < 0 Then value += 360
            Return value
        End Function

        Friend Shared Function NormalizeZoom(value As Single,
                                             minimum As Single,
                                             maximum As Single) As Single
            If Single.IsNaN(value) OrElse Single.IsInfinity(value) Then value = ImageViewerTokens.DefaultZoom
            Dim minValue As Single = Math.Max(ImageViewerTokens.MinUsefulScale, minimum)
            Dim maxValue As Single = Math.Max(minValue, Math.Min(maximum, ImageViewerTokens.MaxZoom))
            Return Math.Max(minValue, Math.Min(maxValue, value))
        End Function

        Friend Shared Function NormalizeExportQuality(value As Integer) As Integer
            If value < 1 Then Return 1
            If value > 100 Then Return 100
            Return value
        End Function

        Friend Shared Function MapExportFormat(value As MASImageExportFormat) As SKEncodedImageFormat
            Select Case value
                Case MASImageExportFormat.Jpeg
                    Return SKEncodedImageFormat.Jpeg
                Case MASImageExportFormat.Webp
                    Return SKEncodedImageFormat.Webp
                Case Else
                    Return SKEncodedImageFormat.Png
            End Select
        End Function

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

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

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

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

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


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

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

End Namespace
