Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.General
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components

    Partial Public NotInheritable Class MASImageViewer
#Region "Rendering Color Helpers"

        Private Shared Function ResolvePrimaryTextColor(ctx As MASThemeContext) As SKColor
            If ctx IsNot Nothing AndAlso ctx.MediaTheme IsNot Nothing Then Return ctx.MediaTheme.Text.Primary
            If ctx IsNot Nothing AndAlso ctx.TextColorTheme IsNot Nothing Then Return ctx.TextColorTheme.PrimaryText
            Return Nexamas.UI.Values.Color.Text.Primary
        End Function

        Private Shared Function ResolveMutedTextColor(ctx As MASThemeContext) As SKColor
            If ctx IsNot Nothing AndAlso ctx.MediaTheme IsNot Nothing Then Return ctx.MediaTheme.Text.Muted
            If ctx IsNot Nothing AndAlso ctx.TextColorTheme IsNot Nothing Then Return ctx.TextColorTheme.MutedText
            Return SKColors.Gray
        End Function

        Private Shared Function ResolveAccentColor(ctx As MASThemeContext) As SKColor
            If ctx IsNot Nothing AndAlso ctx.MediaTheme IsNot Nothing Then Return ctx.MediaTheme.Toolbar.Accent
            If ctx IsNot Nothing AndAlso ctx.BrandTheme IsNot Nothing Then Return ctx.BrandTheme.BrandPrimary
            If ctx IsNot Nothing AndAlso ctx.SemanticTheme IsNot Nothing Then Return ctx.SemanticTheme.Info
            Return SKColors.DodgerBlue
        End Function

        Private Shared Function ResolveMediaStyle(ctx As MASThemeContext,
                                                  dpi As Single) As MASMediaVisualStyle
            Return MASMediaVisualStyleResolver.Resolve(ctx, dpi)
        End Function

#End Region

#Region "Rendering Rail Clip Helpers"

        Private Shared Function ResolvePanelShellRailCornerRadius(shellResult As MASPanelShellResult,
                                                                  contentSurface As SKRect,
                                                                  dpi As Single) As Single
            If shellResult Is Nothing OrElse shellResult.BaseRectPx.Width <= 1.0F OrElse contentSurface.Width <= 1.0F Then Return 0.0F
            dpi = PixelSnap.SafeDpi(dpi)

            Dim leftInset As Single = Math.Max(0.0F, contentSurface.Left - shellResult.BaseRectPx.Left)
            Dim rightInset As Single = Math.Max(0.0F, shellResult.BaseRectPx.Right - contentSurface.Right)
            Dim topInset As Single = Math.Max(0.0F, contentSurface.Top - shellResult.BaseRectPx.Top)
            Dim bottomInset As Single = Math.Max(0.0F, shellResult.BaseRectPx.Bottom - contentSurface.Bottom)
            Dim inset As Single = Math.Max(Math.Max(leftInset, rightInset), Math.Max(topInset, bottomInset))

            Return PixelSnap.SafeRadius(Math.Max(0.0F, shellResult.RadiusPx - inset))
        End Function

        Private Shared Sub ClipToPanelShellTopRail(canvas As SKCanvas,
                                                   railRect As SKRect,
                                                   radiusPx As Single)
            ClipToPanelShellEdgeRail(canvas, railRect, radiusPx, True)
        End Sub

        Private Shared Sub ClipToPanelShellBottomRail(canvas As SKCanvas,
                                                      railRect As SKRect,
                                                      radiusPx As Single)
            ClipToPanelShellEdgeRail(canvas, railRect, radiusPx, False)
        End Sub

        Private Shared Sub ClipToPanelShellEdgeRail(canvas As SKCanvas,
                                                    railRect As SKRect,
                                                    radiusPx As Single,
                                                    topRail As Boolean)
            If canvas Is Nothing OrElse railRect.Width <= 1.0F OrElse railRect.Height <= 1.0F Then Return
            radiusPx = Math.Max(0.0F, Math.Min(radiusPx, Math.Min(railRect.Width, railRect.Height) * 0.5F))
            If radiusPx <= 0.0F Then
                canvas.ClipRect(railRect, SKClipOperation.Intersect, True)
                Return
            End If

            Using clipPath As New SKPath()
                Dim rr As New SKRoundRect()
                Dim zero As New SKPoint(0.0F, 0.0F)
                Dim r As New SKPoint(radiusPx, radiusPx)
                If topRail Then
                    rr.SetRectRadii(railRect, New SKPoint() {r, r, zero, zero})
                Else
                    rr.SetRectRadii(railRect, New SKPoint() {zero, zero, r, r})
                End If
                clipPath.AddRoundRect(rr)
                canvas.ClipPath(clipPath, SKClipOperation.Intersect, True)
            End Using
        End Sub

#End Region

    End Class

End Namespace
