Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Friend-only manifest that turns the limited public SurfaceMaterial/WithSurfaceMaterial API
    ''' into a protected Nexamas UI contract. The manifest records which public members are allowed.
    ''' It does not expose a material registry, renderer, frame painter, proof API, or runtime shortcut.
    ''' </summary>
    Friend NotInheritable Class MASPublicSurfaceMaterialGatewayManifest
        Private ReadOnly _entries As IReadOnlyList(Of MASPublicSurfaceMaterialGatewayEntry)

        Friend Sub New(entries As IEnumerable(Of MASPublicSurfaceMaterialGatewayEntry),
                       hasNoPublicRegistry As Boolean,
                       hasNoPublicRenderer As Boolean,
                       hasNoPublicFramePainter As Boolean,
                       hasNoPublicProofApi As Boolean,
                       notes As String)
            _entries = New ReadOnlyCollection(Of MASPublicSurfaceMaterialGatewayEntry)(NormalizeEntries(entries))
            Me.HasNoPublicRegistry = hasNoPublicRegistry
            Me.HasNoPublicRenderer = hasNoPublicRenderer
            Me.HasNoPublicFramePainter = hasNoPublicFramePainter
            Me.HasNoPublicProofApi = hasNoPublicProofApi
            Me.Notes = If(notes, String.Empty).Trim()
        End Sub

        Friend ReadOnly Property Entries As IReadOnlyList(Of MASPublicSurfaceMaterialGatewayEntry)
            Get
                Return _entries
            End Get
        End Property

        Friend ReadOnly Property HasNoPublicRegistry As Boolean
        Friend ReadOnly Property HasNoPublicRenderer As Boolean
        Friend ReadOnly Property HasNoPublicFramePainter As Boolean
        Friend ReadOnly Property HasNoPublicProofApi As Boolean
        Friend ReadOnly Property Notes As String

        Friend ReadOnly Property EntryCount As Integer
            Get
                Return Entries.Count
            End Get
        End Property

        Friend ReadOnly Property ReadyGatewayCount As Integer
            Get
                Dim count As Integer = 0
                For Each entry As MASPublicSurfaceMaterialGatewayEntry In Entries
                    If entry IsNot Nothing AndAlso entry.IsPublicSurfaceMaterialGateway Then count += 1
                Next
                Return count
            End Get
        End Property

        Friend ReadOnly Property HasLimitedPublicSurfaceMaterialGateways As Boolean
            Get
                Return EntryCount = ExpectedGatewayMemberCount AndAlso
                       ReadyGatewayCount = EntryCount AndAlso
                       HasNoPublicRegistry AndAlso
                       HasNoPublicRenderer AndAlso
                       HasNoPublicFramePainter AndAlso
                       HasNoPublicProofApi AndAlso
                       ContainsRequiredGatewayMembers()
            End Get
        End Property

        Friend ReadOnly Property HasRenderVerificationCatchUpDebt As Boolean
            Get
                For Each entry As MASPublicSurfaceMaterialGatewayEntry In Entries
                    If entry IsNot Nothing AndAlso Not entry.HasCurrentRenderVerificationProof Then Return True
                Next
                Return False
            End Get
        End Property

        Friend Shared ReadOnly Property ExpectedGatewayMemberCount As Integer
            Get
                Dim count As Integer = 0
                For Each entry As MASPublicSurfaceMaterialGatewayEntry In CreateEntries()
                    If entry IsNot Nothing Then count += 1
                Next
                Return count
            End Get
        End Property

        Friend Function ContainsGateway(ownerTypeName As String,
                                        memberName As String) As Boolean
            For Each entry As MASPublicSurfaceMaterialGatewayEntry In Entries
                If entry IsNot Nothing AndAlso entry.Matches(ownerTypeName, memberName) Then Return True
            Next
            Return False
        End Function

        Friend Function CountForConsumer(consumerKind As MASSurfaceTreatmentConsumerKind) As Integer
            Dim count As Integer = 0
            For Each entry As MASPublicSurfaceMaterialGatewayEntry In Entries
                If entry IsNot Nothing AndAlso entry.ConsumerKind = consumerKind Then count += 1
            Next
            Return count
        End Function

        Friend Shared Function CreateCurrent() As MASPublicSurfaceMaterialGatewayManifest
            Return New MASPublicSurfaceMaterialGatewayManifest(
                entries:=CreateEntries(),
                hasNoPublicRegistry:=True,
                hasNoPublicRenderer:=True,
                hasNoPublicFramePainter:=True,
                hasNoPublicProofApi:=True,
                notes:="Limited public surface material gateways are official. Registry, renderer, frame painter, proof reports, and runtime policy remain internal.")
        End Function

        Private Shared Function CreateEntries() As IEnumerable(Of MASPublicSurfaceMaterialGatewayEntry)
            Return New MASPublicSurfaceMaterialGatewayEntry() {
        Entry("Nexamas.UI.Application.MASApplicationWindowTooltips", "SurfaceMaterial", "Property", "Public Property SurfaceMaterial As String", MASSurfaceTreatmentConsumerKind.TooltipSurface, True),
        Entry("Nexamas.UI.Application.MASApplicationWindowTooltips", "WithSurfaceMaterial", "Function", "Public Function WithSurfaceMaterial(materialKey As String) As MASApplicationWindowTooltips", MASSurfaceTreatmentConsumerKind.TooltipSurface, True),
        Entry("Nexamas.UI.Controls.MASGroupBox", "SurfaceMaterial", "Property", "Public Property SurfaceMaterial As String", MASSurfaceTreatmentConsumerKind.GroupBoxSurface, True),
        Entry("Nexamas.UI.Controls.MASGroupBox", "WithSurfaceMaterial", "Function", "Public Function WithSurfaceMaterial(materialKey As String) As MASGroupBox", MASSurfaceTreatmentConsumerKind.GroupBoxSurface, True),
        Entry("Nexamas.UI.Controls.MASOperationProgressBox", "ContainerSurfaceMaterialKey", "Property", "Public Property ContainerSurfaceMaterialKey As String", MASSurfaceTreatmentConsumerKind.ProgressSurface, True),
        Entry("Nexamas.UI.Controls.MASOperationProgressBox", "WithContainerSurfaceMaterial", "Function", "Public Function WithContainerSurfaceMaterial(materialKey As String) As MASOperationProgressBox", MASSurfaceTreatmentConsumerKind.ProgressSurface, True),
        Entry("Nexamas.UI.Controls.MASProgressBar", "ContainerSurfaceMaterialKey", "Property", "Public Property ContainerSurfaceMaterialKey As String", MASSurfaceTreatmentConsumerKind.ProgressSurface, True),
        Entry("Nexamas.UI.Controls.MASProgressBar", "WithContainerSurfaceMaterial", "Function", "Public Function WithContainerSurfaceMaterial(materialKey As String) As MASProgressBar", MASSurfaceTreatmentConsumerKind.ProgressSurface, True),
        Entry("Nexamas.UI.Application.MASApplicationSurfaceLayoutBuilder", "WithCommandBackgroundSurfaceMaterial", "Function", "Public Function WithCommandBackgroundSurfaceMaterial(materialKey As String) As MASApplicationSurfaceLayoutBuilder", MASSurfaceTreatmentConsumerKind.BackgroundSurface, True),
        Entry("Nexamas.UI.Application.MASApplicationSurfaceLayoutBuilder", "WithRegionBackgroundSurfaceMaterial", "Function", "Public Function WithRegionBackgroundSurfaceMaterial(materialKey As String) As MASApplicationSurfaceLayoutBuilder", MASSurfaceTreatmentConsumerKind.CardSurface, True),
        Entry("Nexamas.UI.Application.MASApplicationWindow", "SurfaceMaterial", "Property", "Public Property SurfaceMaterial As String", MASSurfaceTreatmentConsumerKind.ApplicationChromeSurface, True),
        Entry("Nexamas.UI.Application.MASApplicationWindow", "WithSurfaceMaterial", "Function", "Public Function WithSurfaceMaterial(materialKey As String) As MASApplicationWindow", MASSurfaceTreatmentConsumerKind.ApplicationChromeSurface, True),
        Entry("Nexamas.UI.Components.MASToolbar", "SurfaceMaterial", "Property", "Public Property SurfaceMaterial As String", MASSurfaceTreatmentConsumerKind.ToolbarSurface, True),
        Entry("Nexamas.UI.Components.MASToolbar", "WithSurfaceMaterial", "Function", "Public Function WithSurfaceMaterial(materialKey As String) As MASToolbar", MASSurfaceTreatmentConsumerKind.ToolbarSurface, True),
        Entry("Nexamas.UI.Components.MASTopBarComponent", "SurfaceMaterial", "Property", "Public Property SurfaceMaterial As String", MASSurfaceTreatmentConsumerKind.TopBarSurface, True),
        Entry("Nexamas.UI.Components.MASTopBarComponent", "WithSurfaceMaterial", "Function", "Public Function WithSurfaceMaterial(materialKey As String) As MASTopBarComponent", MASSurfaceTreatmentConsumerKind.TopBarSurface, True),
        Entry("Nexamas.UI.Controls.MASCard", "SurfaceMaterial", "Property", "Public Property SurfaceMaterial As String", MASSurfaceTreatmentConsumerKind.CardSurface, True),
        Entry("Nexamas.UI.Controls.MASCard", "WithSurfaceMaterial", "Function", "Public Function WithSurfaceMaterial(materialKey As String) As MASCard", MASSurfaceTreatmentConsumerKind.CardSurface, True),
        Entry("Nexamas.UI.Controls.MASToastOptions", "SurfaceMaterial", "Property", "Public Property SurfaceMaterial As String = MASSurfaceMaterialIds.Auto", MASSurfaceTreatmentConsumerKind.ToastSurface, True),
        Entry("Nexamas.UI.Controls.MASToastOptions", "WithSurfaceMaterial", "Function", "Public Function WithSurfaceMaterial(materialKey As String) As MASToastOptions", MASSurfaceTreatmentConsumerKind.ToastSurface, True),
        Entry("Nexamas.UI.Application.MASApplicationSurfaceMaterialGateway", "GetSelectableMaterials", "Function", "Public Shared Function GetSelectableMaterials() As MASApplicationSurfaceMaterialOption()", MASSurfaceTreatmentConsumerKind.ApplicationChromeSurface, True),
        Entry("Nexamas.UI.Application.MASApplicationSurfaceMaterialGateway", "CreateScope", "Function", "Public Shared Function CreateScope(window As MASApplicationWindow) As MASApplicationSurfaceMaterialScope", MASSurfaceTreatmentConsumerKind.ApplicationChromeSurface, True),
        Entry("Nexamas.UI.Application.MASApplicationSurfaceMaterialScope", "Apply", "Function", "Public Function Apply(materialKey As String) As MASApplicationSurfaceMaterialScope", MASSurfaceTreatmentConsumerKind.ApplicationChromeSurface, True)
    }
        End Function
        Private Shared Function Entry(ownerTypeName As String,
                                      memberName As String,
                                      memberKind As String,
                                      publicSignature As String,
                                      consumerKind As MASSurfaceTreatmentConsumerKind,
                                      hasCurrentRenderVerificationProof As Boolean) As MASPublicSurfaceMaterialGatewayEntry
            Return New MASPublicSurfaceMaterialGatewayEntry(
                ownerTypeName:=ownerTypeName,
                memberName:=memberName,
                memberKind:=memberKind,
                publicSignature:=publicSignature,
                consumerKind:=consumerKind,
                preservesAutoDefaultPath:=True,
                usesStringMaterialKey:=True,
                hasCurrentRenderVerificationProof:=hasCurrentRenderVerificationProof,
                notes:="Auto preserves the existing default visual path. Explicit material keys resolve through the internal VisualSurface runtime route.")
        End Function

        Private Function ContainsRequiredGatewayMembers() As Boolean
            Return ContainsGateway("Nexamas.UI.Application.MASApplicationWindowTooltips", "SurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Application.MASApplicationWindowTooltips", "WithSurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Controls.MASGroupBox", "SurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Controls.MASGroupBox", "WithSurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Controls.MASOperationProgressBox", "ContainerSurfaceMaterialKey") AndAlso
                   ContainsGateway("Nexamas.UI.Controls.MASOperationProgressBox", "WithContainerSurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Controls.MASProgressBar", "ContainerSurfaceMaterialKey") AndAlso
                   ContainsGateway("Nexamas.UI.Controls.MASProgressBar", "WithContainerSurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Application.MASApplicationSurfaceLayoutBuilder", "WithCommandBackgroundSurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Application.MASApplicationSurfaceLayoutBuilder", "WithRegionBackgroundSurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Application.MASApplicationWindow", "SurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Application.MASApplicationWindow", "WithSurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Components.MASToolbar", "SurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Components.MASToolbar", "WithSurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Components.MASTopBarComponent", "SurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Components.MASTopBarComponent", "WithSurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Controls.MASCard", "SurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Controls.MASCard", "WithSurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Controls.MASToastOptions", "SurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Controls.MASToastOptions", "WithSurfaceMaterial") AndAlso
                   ContainsGateway("Nexamas.UI.Application.MASApplicationSurfaceMaterialGateway", "GetSelectableMaterials") AndAlso
                   ContainsGateway("Nexamas.UI.Application.MASApplicationSurfaceMaterialGateway", "CreateScope") AndAlso
                   ContainsGateway("Nexamas.UI.Application.MASApplicationSurfaceMaterialScope", "Apply")
        End Function

        Private Shared Function NormalizeEntries(entries As IEnumerable(Of MASPublicSurfaceMaterialGatewayEntry)) As List(Of MASPublicSurfaceMaterialGatewayEntry)
            Dim result As New List(Of MASPublicSurfaceMaterialGatewayEntry)()
            If entries Is Nothing Then Return result

            For Each entry As MASPublicSurfaceMaterialGatewayEntry In entries
                If entry IsNot Nothing Then result.Add(entry)
            Next

            Return result
        End Function
    End Class

End Namespace