Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports Nexamas.UI.Values

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Single catalog for reusable platform surface visual styles.
    ''' It does not draw; it resolves a style key into the canonical material/frame/chrome composition.
    ''' </summary>
    Friend NotInheritable Class MASSurfaceVisualStyleRegistry

        Private Shared ReadOnly _styles As Dictionary(Of String, MASSurfaceVisualStyle) = CreateDefaultStyles()

        Private Sub New()
        End Sub

        Friend Shared Function Resolve(styleKey As String) As MASSurfaceVisualStyle
            Dim key As String = NormalizeKey(styleKey)

            If String.Equals(key, MASSurfaceVisualStyleIds.Auto, StringComparison.OrdinalIgnoreCase) Then
                key = MASSurfaceVisualStyleIds.PlatformDialogFrame
            End If

            Dim style As MASSurfaceVisualStyle = Nothing
            If _styles.TryGetValue(key, style) Then Return style

            Return _styles(MASSurfaceVisualStyleIds.PlatformDialogFrame)
        End Function

        ''' <summary>
        ''' Returns the built-in surface visual styles for architecture validation.
        ''' Runtime consumers still resolve by key; validators use this to ensure every
        ''' style role has a legal architectural SurfaceFamily before startup.
        ''' </summary>
        Friend Shared Function RegisteredStyles() As MASSurfaceVisualStyle()
            Dim result As New List(Of MASSurfaceVisualStyle)()

            For Each style As MASSurfaceVisualStyle In _styles.Values
                If style IsNot Nothing Then result.Add(style)
            Next

            Return result.ToArray()
        End Function

        ''' <summary>
        ''' Maps SurfaceVisualStyle roles back to architecture SurfaceFamily coverage.
        ''' This is validation-only glue: rendering still consumes MASSurfaceRole and
        ''' materials directly through the Surface Visual System.
        ''' </summary>
        Friend Shared Function ResolveArchitectureSurfaceFamily(surfaceRole As MASSurfaceRole) As CommonEnums.SurfaceFamily
            Select Case surfaceRole
                Case MASSurfaceRole.PanelShell,
                     MASSurfaceRole.Dialog,
                     MASSurfaceRole.FilePicker
                    Return CommonEnums.SurfaceFamily.PanelShell

                Case MASSurfaceRole.Toast
                    Return CommonEnums.SurfaceFamily.Toast

                Case MASSurfaceRole.DropDown
                    Return CommonEnums.SurfaceFamily.DropDown

                Case MASSurfaceRole.Popup
                    Return CommonEnums.SurfaceFamily.Fenster

                Case MASSurfaceRole.Card
                    Return CommonEnums.SurfaceFamily.Card

                Case MASSurfaceRole.ApplicationChrome
                    Return CommonEnums.SurfaceFamily.TopBar

                Case MASSurfaceRole.Control
                    Return CommonEnums.SurfaceFamily.Card

                Case Else
                    Return CommonEnums.SurfaceFamily.Generic
            End Select
        End Function

        Private Shared Function CreateDefaultStyles() As Dictionary(Of String, MASSurfaceVisualStyle)
            Dim result As New Dictionary(Of String, MASSurfaceVisualStyle)(StringComparer.OrdinalIgnoreCase)

            Add(result, New MASSurfaceVisualStyle(
                id:=MASSurfaceVisualStyleIds.PlatformDialogFrame,
                surfaceMaterialKey:=MASSurfaceMaterialIds.PlatformDefault,
                surfaceFrameKey:=MASSurfaceFrameIds.PlatformDefault,
                surfaceChromeKey:=MASSurfaceChromeIds.PlatformDefault,
                surfaceRole:=MASSurfaceRole.PanelShell,
                features:=MASSurfaceVisualStyleFeatures.PlatformDialogFrame))


            Return result
        End Function

        Private Shared Sub Add(target As Dictionary(Of String, MASSurfaceVisualStyle),
                              style As MASSurfaceVisualStyle)
            If target Is Nothing OrElse style Is Nothing Then Return
            target(style.Id) = style
        End Sub

        Private Shared Function NormalizeKey(value As String) As String
            Dim key As String = If(value, String.Empty).Trim()
            If key.Length = 0 Then Return MASSurfaceVisualStyleIds.Auto
            Return key
        End Function

    End Class

End Namespace
