Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports Nexamas.UI.Theming

Namespace Nexamas.UI.Verification

    ''' <summary>
    ''' Resolves render-verification scenario theme keys into immutable theme instances without depending on
    ''' the process-wide current theme at capture time.
    ''' </summary>
    ''' <remarks>
    ''' Render Verification baselines must be repeatable across machines and Showcase runs. This resolver keeps
    ''' scenario theme keys small and explicit, then binds them to registered Nexamas UI themes before ThemeHost
    ''' creates the per-capture ThemeContext.
    ''' </remarks>
    Friend NotInheritable Class MASRenderVerificationThemeResolver
        Friend Const DefaultThemeKey As String = "default"
        Friend Const SignatureThemeKey As String = "MASSignature"
        Friend Const SignatureThemeId As String = "premium-office-mas-signature"

        Private Shared ReadOnly KnownThemeKeys As HashSet(Of String) = New HashSet(Of String)(StringComparer.OrdinalIgnoreCase) From {
            DefaultThemeKey,
            SignatureThemeKey,
            SignatureThemeId
        }

        Private Sub New()
        End Sub

        ''' <summary>
        ''' Returns whether a scenario theme key is part of the approved deterministic capture matrix.
        ''' </summary>
        Friend Shared Function IsKnownThemeKey(themeKey As String) As Boolean
            Dim normalized As String = If(themeKey, String.Empty).Trim()
            If normalized.Length = 0 Then Return False
            If KnownThemeKeys.Contains(normalized) Then Return True

            Return ThemeManager.GetByIdCore(normalized) IsNot Nothing
        End Function

        ''' <summary>
        ''' Resolves a scenario theme key to a concrete registered Nexamas UI theme.
        ''' </summary>
        Friend Shared Function Resolve(themeKey As String) As IMASAppTheme
            Dim normalized As String = If(themeKey, String.Empty).Trim()
            Dim themeId As String

            If String.Equals(normalized, DefaultThemeKey, StringComparison.OrdinalIgnoreCase) OrElse
               String.Equals(normalized, SignatureThemeKey, StringComparison.OrdinalIgnoreCase) Then
                themeId = SignatureThemeId
            Else
                themeId = normalized
            End If

            If Not String.IsNullOrWhiteSpace(themeId) Then
                Dim registeredTheme As IMASAppTheme = ThemeManager.GetByIdCore(themeId)
                If registeredTheme IsNot Nothing Then
                    Return registeredTheme
                End If
            End If

            If String.IsNullOrWhiteSpace(themeId) OrElse Not IsKnownThemeKey(themeId) Then
                Throw New InvalidOperationException("Render verification theme key '" & normalized & "' is not part of the deterministic capture matrix.")
            End If

            Throw New InvalidOperationException("Render verification theme '" & themeId & "' is not registered in Nexamas UI ThemeManager.")
        End Function
    End Class

End Namespace
