Option Strict On
Option Explicit On

Imports System
Imports System.ComponentModel
Imports System.IO
Imports SkiaSharp

Namespace Nexamas.UI.Verification

    ''' <summary>
    ''' Immutable release-evidence entry for one render-verification product proof asset row.
    ''' The entry exposes approved/current/diff image paths and safe bitmap loading helpers while keeping
    ''' capture handlers, baseline stores, scenario objects, and comparison internals owned by Nexamas.UI.
    ''' Passed means an approved baseline matched; BaselineSeeded means review is still required.
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Advanced)>
    Public NotInheritable Class MASRenderVerificationEntry
        Private ReadOnly _baselineImagePath As String
        Private ReadOnly _currentImagePath As String
        Private ReadOnly _differenceImagePath As String

        Friend Sub New(targetId As String,
                       targetName As String,
                       scenarioId As String,
                       displayName As String,
                       componentKey As String,
                       stateKey As String,
                       themeKey As String,
                       dpiScale As Single,
                       widthPx As Integer,
                       heightPx As Integer,
                       baselineImagePath As String,
                       currentImagePath As String,
                       differenceImagePath As String,
                       outcome As MASRenderVerificationOutcome,
                       pixelCount As Integer,
                       differentPixelCount As Integer,
                       maxChannelDelta As Integer,
                       differenceRatio As Double,
                       message As String)
            Me.TargetId = If(targetId, String.Empty)
            Me.TargetName = If(targetName, String.Empty)
            Me.ScenarioId = If(scenarioId, String.Empty)
            Me.DisplayName = If(displayName, String.Empty)
            Me.ComponentKey = If(componentKey, String.Empty)
            Me.StateKey = If(stateKey, String.Empty)
            Me.ThemeKey = If(themeKey, String.Empty)
            Me.DpiScale = dpiScale
            Me.WidthPx = widthPx
            Me.HeightPx = heightPx
            _baselineImagePath = If(baselineImagePath, String.Empty)
            _currentImagePath = If(currentImagePath, String.Empty)
            _differenceImagePath = If(differenceImagePath, String.Empty)
            Me.Outcome = outcome
            Me.PixelCount = pixelCount
            Me.DifferentPixelCount = differentPixelCount
            Me.MaxChannelDelta = maxChannelDelta
            Me.DifferenceRatio = differenceRatio
            Me.Message = If(message, String.Empty)
        End Sub

        Public ReadOnly Property TargetId As String
        Public ReadOnly Property TargetName As String
        Public ReadOnly Property ScenarioId As String
        Public ReadOnly Property DisplayName As String
        Public ReadOnly Property ComponentKey As String
        Public ReadOnly Property StateKey As String
        Public ReadOnly Property ThemeKey As String
        Public ReadOnly Property DpiScale As Single
        Public ReadOnly Property WidthPx As Integer
        Public ReadOnly Property HeightPx As Integer
        Public ReadOnly Property Outcome As MASRenderVerificationOutcome
        Public ReadOnly Property PixelCount As Integer
        Public ReadOnly Property DifferentPixelCount As Integer
        Public ReadOnly Property MaxChannelDelta As Integer
        Public ReadOnly Property DifferenceRatio As Double
        Public ReadOnly Property Message As String

        Public ReadOnly Property BaselineImagePath As String
            Get
                Return _baselineImagePath
            End Get
        End Property

        Public ReadOnly Property CurrentImagePath As String
            Get
                Return _currentImagePath
            End Get
        End Property

        Public ReadOnly Property DifferenceImagePath As String
            Get
                Return _differenceImagePath
            End Get
        End Property

        Public ReadOnly Property Passed As Boolean
            Get
                Return Outcome = MASRenderVerificationOutcome.Passed
            End Get
        End Property

        Public ReadOnly Property RequiresApproval As Boolean
            Get
                Return Outcome = MASRenderVerificationOutcome.BaselineSeeded
            End Get
        End Property

        Public ReadOnly Property Invalid As Boolean
            Get
                Return Outcome = MASRenderVerificationOutcome.Invalid
            End Get
        End Property

        Public ReadOnly Property HasBaselineImage As Boolean
            Get
                Return File.Exists(_baselineImagePath)
            End Get
        End Property

        Public ReadOnly Property HasCurrentImage As Boolean
            Get
                Return File.Exists(_currentImagePath)
            End Get
        End Property

        Public ReadOnly Property HasDifferenceImage As Boolean
            Get
                Return File.Exists(_differenceImagePath)
            End Get
        End Property

        ''' <summary>
        ''' Loads a copy of the approved baseline bitmap for preview controls. The caller owns the returned bitmap.
        ''' </summary>
        Public Function CreateBaselineBitmap() As SKBitmap
            Return DecodeBitmap(_baselineImagePath)
        End Function

        ''' <summary>
        ''' Loads a copy of the current captured bitmap for preview controls. The caller owns the returned bitmap.
        ''' </summary>
        Public Function CreateCurrentBitmap() As SKBitmap
            Return DecodeBitmap(_currentImagePath)
        End Function

        ''' <summary>
        ''' Loads a copy of the difference bitmap for preview controls. The caller owns the returned bitmap.
        ''' </summary>
        Public Function CreateDifferenceBitmap() As SKBitmap
            Return DecodeBitmap(_differenceImagePath)
        End Function

        Private Shared Function DecodeBitmap(path As String) As SKBitmap
            If String.IsNullOrWhiteSpace(path) OrElse Not File.Exists(path) Then Return Nothing
            Return SKBitmap.Decode(path)
        End Function
    End Class

End Namespace
