Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.General
Imports SkiaSharp
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Rendering
Namespace Nexamas.UI.Components

    Friend NotInheritable Class MASBackground
        Implements IDisposable

        Private ReadOnly _painter As New BackgroundPainter()
        Private _surfaceMaterialKey As String = MASSurfaceMaterialIds.Auto
        Private _disposed As Boolean

        Friend ReadOnly Property Painter As BackgroundPainter
            Get
                Return _painter
            End Get
        End Property
        Friend Property Palette As MASBackgroundPalette
            Get
                Return _painter.Palette
            End Get
            Set(value As MASBackgroundPalette)
                If _disposed Then Return
                If _painter.Palette = value Then Return

                _painter.Palette = value
                _painter.InvalidateCache()
            End Set
        End Property

        Friend Property SurfaceMaterial As String
            Get
                Return _surfaceMaterialKey
            End Get
            Set(value As String)
                If _disposed Then Return
                Dim key As String = MASSurfaceTreatmentMaterialKeyNormalizer.NormalizeForStorage(value)
                If String.Equals(_surfaceMaterialKey, key, StringComparison.OrdinalIgnoreCase) Then Return
                _surfaceMaterialKey = key
                _painter.InvalidateCache()
            End Set
        End Property
#Region "SDK Fluent API"

        Friend Shared Function Create() As MASBackground
            Return New MASBackground()
        End Function

        Friend Shared Function [Default]() As MASBackground
            Return Create().
                WithPalette(MASBackgroundPalette.Default)
        End Function

        Friend Shared Function WithDefaultPalette() As MASBackground
            Return [Default]()
        End Function

        Friend Function WithPalette(palette As MASBackgroundPalette) As MASBackground
            Me.Palette = palette
            Return Me
        End Function

        Friend Function WithSurfaceMaterial(materialKey As String) As MASBackground
            Me.SurfaceMaterial = materialKey
            Return Me
        End Function

        Friend Function Refresh() As MASBackground
            InvalidateCache()
            Return Me
        End Function

#End Region
        Friend Sub Render(canvas As SKCanvas,
                          ctx As MASThemeContext,
                          surfaceWidthPx As Integer,
                          surfaceHeightPx As Integer)

            If _disposed Then Return
            If canvas Is Nothing OrElse ctx Is Nothing Then Return

            Dim w As Integer = Math.Max(1, surfaceWidthPx)
            Dim h As Integer = Math.Max(1, surfaceHeightPx)

            Dim dpi As Single = PixelSnap.SafeDpi(ctx.Dpi)
            Dim rectPx As New SKRect(0.0F, 0.0F, CSng(w), CSng(h))

            If MASSurfaceMaterialComponentGateway.TryDrawBackgroundSurface(
                canvas:=canvas,
                ctx:=ctx,
                boundsPx:=rectPx,
                materialKey:=_surfaceMaterialKey,
                dpi:=dpi,
                surfaceStrengthLevel:=3,
                borderStrengthLevel:=1) Then
                Return
            End If

            _painter.Render(canvas, ctx, rectPx, dpi)
        End Sub

        Friend Sub Render(canvas As SKCanvas,
                          ctx As MASThemeContext,
                          rectPx As SKRect)

            If _disposed Then Return
            If canvas Is Nothing OrElse ctx Is Nothing Then Return
            If rectPx.Width <= 1.0F OrElse rectPx.Height <= 1.0F Then Return

            Dim dpi As Single = PixelSnap.SafeDpi(ctx.Dpi)
            If MASSurfaceMaterialComponentGateway.TryDrawBackgroundSurface(
                canvas:=canvas,
                ctx:=ctx,
                boundsPx:=rectPx,
                materialKey:=_surfaceMaterialKey,
                dpi:=dpi,
                surfaceStrengthLevel:=3,
                borderStrengthLevel:=1) Then
                Return
            End If

            _painter.Render(canvas, ctx, rectPx, dpi)
        End Sub

        Friend Sub InvalidateCache()
            If _disposed Then Return
            _painter.InvalidateCache()
        End Sub

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

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

    End Class

End Namespace
