Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.ThemeStudio

    ''' <summary>
    ''' Internal builder for the official Theme Studio snapshot.
    ''' </summary>
    ''' <remarks>
    ''' The builder is the only Phase 5 code that reads ThemeManager and the reusable Surface
    ''' Visual registries. That preserves clean ownership: UI and public SDK callers receive
    ''' immutable design data, never internal theme/runtime/rendering objects.
    ''' </remarks>
    Friend NotInheritable Class MASThemeStudioSnapshotBuilder
        Private Sub New()
        End Sub

        Friend Shared Function Build() As MASThemeStudioSnapshot
            ThemeManager.EnsureInitializedCore()

            Dim currentTheme As IMASAppTheme = ThemeManager.CurrentCore
            Dim themes As New List(Of MASThemeStudioThemeEntry)()
            Dim contrastChecks As New List(Of MASThemeStudioContrastCheck)()

            For Each theme As IMASAppTheme In ThemeManager.GetAllThemesCore()
                If theme Is Nothing Then Continue For

                themes.Add(New MASThemeStudioThemeEntry(
                    id:=theme.Id,
                    displayName:=theme.DisplayName,
                    version:=theme.Version,
                    isCurrent:=Object.ReferenceEquals(theme, currentTheme),
                    foundationGroupCount:=CountFoundationGroups(theme),
                    familyGroupCount:=CountFamilyGroups(theme)))

                contrastChecks.AddRange(MASThemeStudioContrastAnalyzer.Analyze(theme))
            Next

            Return New MASThemeStudioSnapshot(
                contractVersion:=MASThemeStudioContractVersion.Current,
                currentThemeId:=If(currentTheme Is Nothing, String.Empty, currentTheme.Id),
                themes:=themes,
                materials:=MASSurfaceDesignerCatalog.CreateMaterialEntries(),
                materialProfiles:=MASSurfaceDesignerCatalog.CreateMaterialProfileEntries(),
                slots:=MASSurfaceDesignerCatalog.CreateSlotEntries(),
                roles:=MASSurfaceDesignerCatalog.CreateRoleEntries(),
                visualStyles:=MASSurfaceDesignerCatalog.CreateVisualStyleEntries(),
                contrastChecks:=contrastChecks)
        End Function

        Private Shared Function CountFoundationGroups(theme As IMASAppTheme) As Integer
            If theme Is Nothing Then Return 0

            Dim count As Integer = 0
            If MASAppThemeContracts.Foundation(theme).Brand IsNot Nothing Then count += 1
            If MASAppThemeContracts.Foundation(theme).Neutrals IsNot Nothing Then count += 1
            If MASAppThemeContracts.Foundation(theme).Semantics IsNot Nothing Then count += 1
            If MASAppThemeContracts.Foundation(theme).TextColors IsNot Nothing Then count += 1
            If MASAppThemeContracts.Foundation(theme).Interaction IsNot Nothing Then count += 1
            If MASAppThemeContracts.Foundation(theme).Surface IsNot Nothing Then count += 1
            If MASAppThemeContracts.Foundation(theme).Background IsNot Nothing Then count += 1
            Return count
        End Function

        Private Shared Function CountFamilyGroups(theme As IMASAppTheme) As Integer
            If theme Is Nothing Then Return 0

            Dim count As Integer = 0
            If MASAppThemeContracts.Families(theme).SelectorItem IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).SegmentedControl IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).List IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).Input IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).Tile IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).Buttons IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).Cards IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).Menu IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).Scroll IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).TopBar IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).Progress IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).Toast IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).Tooltip IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).CheckBox IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).RadioButton IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).ToggleSwitch IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).ColorPicker IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).Tabs IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).PanelShell IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).FrameSurface IsNot Nothing Then count += 1
            If MASAppThemeContracts.Families(theme).GroupBox IsNot Nothing Then count += 1
            Return count
        End Function

    End Class

End Namespace
