Option Strict On
Option Explicit On

Imports System
Imports System.Globalization
Imports System.IO
Imports System.Windows.Forms
Imports Nexamas.UI.Architecture
Imports Nexamas.UI.Composition
Imports Nexamas.UI.Controls
Imports Nexamas.UI.General
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.TextRendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components

    Partial Public NotInheritable Class MASImageViewer
#Region "Image Lifecycle API"

        Public Sub SetImage(bitmap As SKBitmap,
                            Optional title As String = Nothing,
                            Optional subtitle As String = Nothing,
                            Optional resetView As Boolean = True,
                            Optional takeOwnership As Boolean = False)
            If Object.ReferenceEquals(_bitmap, bitmap) Then
                _ownsBitmap = takeOwnership
                If title IsNot Nothing Then _title = MASImageViewerVisualPolicy.NormalizeText(title, ImageViewerTokens.DefaultTitle)
                If subtitle IsNot Nothing Then _subtitle = MASImageViewerVisualPolicy.NormalizeText(subtitle, String.Empty)
                If resetView Then ResetViewCore(False)
                RaiseImageChangedSafe()
                InvalidateVisual()
                Return
            End If

            DisposeCurrentBitmap()
            _bitmap = bitmap
            _ownsBitmap = takeOwnership
            If title IsNot Nothing Then _title = MASImageViewerVisualPolicy.NormalizeText(title, ImageViewerTokens.DefaultTitle)
            If subtitle IsNot Nothing Then _subtitle = MASImageViewerVisualPolicy.NormalizeText(subtitle, String.Empty)
            If resetView Then ResetViewCore(False)
            RaiseImageChangedSafe()
            InvalidateVisual()
        End Sub

        Public Function WithImage(bitmap As SKBitmap,
                                  Optional title As String = Nothing,
                                  Optional subtitle As String = Nothing,
                                  Optional takeOwnership As Boolean = False) As MASImageViewer
            SetImage(bitmap, title, subtitle, True, takeOwnership)
            Return Me
        End Function

        Public Function ClearImage() As MASImageViewer
            If Not HasImage AndAlso _title = ImageViewerTokens.DefaultTitle AndAlso _subtitle.Length = 0 Then Return Me
            DisposeCurrentBitmap()
            _title = ImageViewerTokens.DefaultTitle
            _subtitle = String.Empty
            ResetViewCore(False)
            RaiseImageChangedSafe()
            InvalidateVisual()
            Return Me
        End Function

#End Region

#Region "Image Lifecycle Helpers"

        Private Sub DisposeCurrentBitmap()
            If _bitmap IsNot Nothing AndAlso _ownsBitmap Then _bitmap.Dispose()
            _bitmap = Nothing
            _ownsBitmap = False
        End Sub

        Protected Overrides Sub Dispose(disposing As Boolean)
            If _disposedLocal Then Return
            _disposedLocal = True

            If disposing Then
                DisposeCurrentBitmap()

                Try
                    _primitives.Dispose()
                Catch masCaughtException1 As Exception
                    Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDisposeCleanup(masCaughtException1)
                End Try
            End If

            MyBase.Dispose(disposing)
        End Sub

#End Region

    End Class

End Namespace
