Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Draws lightweight decorative content for MASFilePickerControl.
    ''' </summary>
    ''' <remarks>
    ''' This renderer is host-neutral.
    '''
    ''' It does not know about Overlay.
    ''' It does not know about Float.
    ''' It does not own input, focus, close behavior, modal behavior, or shield behavior.
    '''
    ''' All layout rectangles are expected to be physical pixels.
    ''' </remarks>
    Friend NotInheritable Class MASFilePickerContentRenderer
        Implements IDisposable

#Region "Fields"

        Private ReadOnly _labelPaint As SKPaint
        Private _disposed As Boolean

#End Region

#Region "Ctor"

        Friend Sub New()
            _labelPaint = New SKPaint With {
                .IsAntialias = True,
                .IsDither = True,
                .Style = SKPaintStyle.Fill,
                .Color = SKColors.White,
                .TextSize = 11.5F,
                .Typeface = SKTypeface.Default
            }
        End Sub

#End Region

#Region "Footer"

        Friend Sub DrawFooterRegion(canvas As SKCanvas,
                                     ctx As MASThemeContext,
                                     layout As FilePickerLayoutInfo,
                                     dpi As Single)

            If _disposed Then Return
            If canvas Is Nothing OrElse ctx Is Nothing Then Return
            If layout.FooterVisualRect.IsEmpty Then Return

            Dim safeDpi As Single = PixelSnap.SafeDpi(dpi)

            Dim footerRect As SKRect = PixelSnap.SnapRectHalf(layout.FooterVisualRect)

            If footerRect.Width <= 1.0F OrElse footerRect.Height <= 1.0F Then Return

            Dim y As Single = PixelSnap.SnapHalf(footerRect.Top - (4.0F * safeDpi))

            DrawFooterGlowLine(canvas, ctx, footerRect, y, safeDpi)
            DrawFooterDividerLine(canvas, ctx, footerRect, y, safeDpi)
        End Sub

        Private Shared Sub DrawFooterGlowLine(canvas As SKCanvas,
                                               ctx As MASThemeContext,
                                               footerRect As SKRect,
                                               y As Single,
                                               dpi As Single)

            If canvas Is Nothing OrElse ctx Is Nothing Then Return
            If footerRect.IsEmpty Then Return

            Dim safeDpi As Single = PixelSnap.SafeDpi(dpi)

            Using glow As New SKPaint With {
                .IsAntialias = True,
                .IsDither = True,
                .Style = SKPaintStyle.Stroke,
                .StrokeWidth = Math.Max(1.0F, safeDpi),
                .Color = ctx.SurfaceTheme.BorderInner.WithAlpha(12)
            }
                canvas.DrawLine(
                    footerRect.Left + (2.0F * safeDpi),
                    y - (1.0F * safeDpi),
                    footerRect.Right - (2.0F * safeDpi),
                    y - (1.0F * safeDpi),
                    glow
                )
            End Using
        End Sub

        Private Shared Sub DrawFooterDividerLine(canvas As SKCanvas,
                                                  ctx As MASThemeContext,
                                                  footerRect As SKRect,
                                                  y As Single,
                                                  dpi As Single)

            If canvas Is Nothing OrElse ctx Is Nothing Then Return
            If footerRect.IsEmpty Then Return

            Dim safeDpi As Single = PixelSnap.SafeDpi(dpi)

            Using linePaint As New SKPaint With {
                .IsAntialias = True,
                .IsDither = True,
                .Style = SKPaintStyle.Stroke,
                .StrokeWidth = Math.Max(1.0F, safeDpi),
                .Color = ctx.SurfaceTheme.BorderOuter.WithAlpha(32)
            }
                canvas.DrawLine(
                    footerRect.Left,
                    y,
                    footerRect.Right,
                    y,
                    linePaint
                )
            End Using
        End Sub

#End Region

#Region "Save file label"

        Friend Sub DrawSaveFileNameLabel(canvas As SKCanvas,
                                         ctx As MASThemeContext,
                                         layout As FilePickerLayoutInfo,
                                         isSaveMode As Boolean,
                                         dpi As Single)

            If _disposed Then Return
            If canvas Is Nothing OrElse ctx Is Nothing Then Return
            If Not isSaveMode Then Return
            If layout.FileNameLabelRect.IsEmpty Then Return

            Dim safeDpi As Single = PixelSnap.SafeDpi(dpi)

            Dim labelRect As SKRect = PixelSnap.SnapRectHalf(layout.FileNameLabelRect)

            If labelRect.Width <= 1.0F OrElse labelRect.Height <= 1.0F Then Return

            _labelPaint.TextSize = 11.5F * safeDpi
            _labelPaint.Color = ResolveLabelColor(ctx)

            Dim baseline As Single =
                PixelSnap.SnapHalf(labelRect.Bottom - (3.0F * safeDpi))

            canvas.DrawText("File name", labelRect.Left, baseline, _labelPaint)
        End Sub

        Private Shared Function ResolveLabelColor(ctx As MASThemeContext) As SKColor
            If ctx Is Nothing Then Return SKColors.White.WithAlpha(185)

            If ctx.InputTheme IsNot Nothing Then
                Return ctx.InputTheme.Text.Normal.WithAlpha(185)
            End If

            Return ctx.TextColorTheme.SecondaryText.WithAlpha(185)
        End Function

#End Region

#Region "Dispose"

        Public Sub Dispose() Implements IDisposable.Dispose
            If _disposed Then Return

            _disposed = True

            Try
                _labelPaint.Dispose()
            Catch masCaughtException1 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDisposeCleanup(masCaughtException1)
            End Try
        End Sub

#End Region

    End Class

End Namespace