Option Strict On
Option Explicit On

Imports System
Imports SkiaSharp

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Immutable renderer-facing tile bitmap fact produced outside the tile draw loop.
    ''' </summary>
    ''' <remarks>
    ''' A snapshot never transfers ownership to the renderer. It records whether the
    ''' bitmap is borrowed or item-owned so paint can read prepared facts without invoking
    ''' providers or deciding lifecycle policy.
    ''' </remarks>
    Friend NotInheritable Class MASTileBitmapRenderSnapshot

        Friend Shared ReadOnly Empty As New MASTileBitmapRenderSnapshot(
            bitmap:=Nothing,
            ownershipKind:=MASTileBitmapOwnershipKind.None,
            iconId:=Guid.Empty,
            revision:=0,
            providerResolved:=False)

        Private ReadOnly _bitmap As SKBitmap
        Private ReadOnly _ownershipKind As MASTileBitmapOwnershipKind
        Private ReadOnly _iconId As Guid
        Private ReadOnly _revision As Integer
        Private ReadOnly _providerResolved As Boolean

        Private Sub New(bitmap As SKBitmap,
                        ownershipKind As MASTileBitmapOwnershipKind,
                        iconId As Guid,
                        revision As Integer,
                        providerResolved As Boolean)
            _bitmap = bitmap
            _ownershipKind = ownershipKind
            _iconId = iconId
            _revision = Math.Max(0, revision)
            _providerResolved = providerResolved
        End Sub

        Friend Shared Function Create(bitmap As SKBitmap,
                                      ownershipKind As MASTileBitmapOwnershipKind,
                                      iconId As Guid,
                                      revision As Integer,
                                      providerResolved As Boolean) As MASTileBitmapRenderSnapshot
            If bitmap Is Nothing Then Return Empty

            Dim normalizedOwnership As MASTileBitmapOwnershipKind = ownershipKind
            If normalizedOwnership = MASTileBitmapOwnershipKind.None Then
                normalizedOwnership = MASTileBitmapOwnershipKind.BorrowedDirect
            End If

            Return New MASTileBitmapRenderSnapshot(
                bitmap:=bitmap,
                ownershipKind:=normalizedOwnership,
                iconId:=iconId,
                revision:=revision,
                providerResolved:=providerResolved)
        End Function

        Friend Shared Function CreateBorrowed(bitmap As SKBitmap,
                                              ownershipKind As MASTileBitmapOwnershipKind,
                                              iconId As Guid,
                                              revision As Integer,
                                              providerResolved As Boolean) As MASTileBitmapRenderSnapshot
            Return Create(
                bitmap:=bitmap,
                ownershipKind:=ownershipKind,
                iconId:=iconId,
                revision:=revision,
                providerResolved:=providerResolved)
        End Function

        Friend ReadOnly Property OwnershipKind As MASTileBitmapOwnershipKind
            Get
                Return _ownershipKind
            End Get
        End Property

        Friend ReadOnly Property IconId As Guid
            Get
                Return _iconId
            End Get
        End Property

        Friend ReadOnly Property Revision As Integer
            Get
                Return _revision
            End Get
        End Property

        Friend ReadOnly Property ProviderResolved As Boolean
            Get
                Return _providerResolved
            End Get
        End Property

        Friend ReadOnly Property HasBitmap As Boolean
            Get
                Return _bitmap IsNot Nothing
            End Get
        End Property

        Friend Function TryGetDrawableBitmap() As SKBitmap
            If _bitmap Is Nothing Then Return Nothing

            Try
                Dim info As SKImageInfo = _bitmap.Info
                If info.Width <= 0 OrElse info.Height <= 0 Then Return Nothing
                Return _bitmap
            Catch masCaughtException As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowRenderFallback(
                    masCaughtException,
                    "MASTileBitmapRenderSnapshot.TryGetDrawableBitmap")
                Return Nothing
            End Try
        End Function

    End Class

End Namespace
