Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' First broad component-surface material adoption audit. It intentionally names only
    ''' broad surfaces, cards, shells, bars, and containers; rows, glyphs, buttons, list items,
    ''' input fields, and interaction overlays remain outside the Material system.
    ''' </summary>
    Friend NotInheritable Class MASVisualComponentSurfaceMaterialAdoptionAudit
        Private Sub New()
        End Sub

        Friend Shared Function BuildCurrentComponentSurfaceMaterialAdoption() As MASVisualComponentSurfaceMaterialAdoptionReport
            Dim publicGatewayManifest As MASPublicSurfaceMaterialGatewayManifest = MASPublicSurfaceMaterialGatewayManifest.CreateCurrent()

            Dim hasCardPrimitiveRuntimeRoute As Boolean = MASSurfaceTreatmentConsumerCatalog.IsRuntimeSurfaceVisualConsumer(MASSurfaceTreatmentConsumerKind.CardSurface)
            Dim hasDefaultSurfaceConsumerRuntimeRoute As Boolean = publicGatewayManifest IsNot Nothing AndAlso
                                                                   publicGatewayManifest.CountForConsumer(MASSurfaceTreatmentConsumerKind.CardSurface) > 0 AndAlso
                                                                   publicGatewayManifest.CountForConsumer(MASSurfaceTreatmentConsumerKind.ToastSurface) > 0 AndAlso
                                                                   publicGatewayManifest.CountForConsumer(MASSurfaceTreatmentConsumerKind.TooltipSurface) > 0 AndAlso
                                                                   publicGatewayManifest.CountForConsumer(MASSurfaceTreatmentConsumerKind.TopBarSurface) > 0 AndAlso
                                                                   publicGatewayManifest.CountForConsumer(MASSurfaceTreatmentConsumerKind.ToolbarSurface) > 0 AndAlso
                                                                   publicGatewayManifest.CountForConsumer(MASSurfaceTreatmentConsumerKind.GroupBoxSurface) > 0
            Dim hasDirectContainerRuntimeRoute As Boolean = MASSurfaceTreatmentConsumerCatalog.IsRuntimeSurfaceVisualConsumer(MASSurfaceTreatmentConsumerKind.PanelShellSurface) AndAlso
                                                           MASSurfaceTreatmentConsumerCatalog.IsRuntimeSurfaceVisualConsumer(MASSurfaceTreatmentConsumerKind.ProgressSurface) AndAlso
                                                           MASSurfaceTreatmentConsumerCatalog.IsRuntimeSurfaceVisualConsumer(MASSurfaceTreatmentConsumerKind.ApplicationChromeSurface)
            Dim hasFrameBodyMaterialRoute As Boolean = MASSurfaceTreatmentConsumerCatalog.FrameRuntimeProofConsumerCount >= 3
            Dim hasNoExplicitOnlySurfaceGate As Boolean = ProbeExplicitMaterialOverrideProtection()
            Dim interactiveInternalsRemainOutOfMaterialScope As Boolean = MASSurfaceTreatmentConsumerCatalog.InteractiveControlsAreOutOfScope

            Dim status As MASVisualComponentSurfaceMaterialAdoptionStatus = DetermineStatus(
                hasCardPrimitiveRuntimeRoute,
                hasDefaultSurfaceConsumerRuntimeRoute,
                hasDirectContainerRuntimeRoute,
                hasFrameBodyMaterialRoute,
                hasNoExplicitOnlySurfaceGate,
                interactiveInternalsRemainOutOfMaterialScope)

            Return New MASVisualComponentSurfaceMaterialAdoptionReport(
                name:="Nexamas UI Component Surface Material Adoption",
                status:=status,
                hasCardPrimitiveRuntimeRoute:=hasCardPrimitiveRuntimeRoute,
                hasDefaultSurfaceConsumerRuntimeRoute:=hasDefaultSurfaceConsumerRuntimeRoute,
                hasDirectContainerRuntimeRoute:=hasDirectContainerRuntimeRoute,
                hasFrameBodyMaterialRoute:=hasFrameBodyMaterialRoute,
                hasNoExplicitOnlySurfaceGate:=hasNoExplicitOnlySurfaceGate,
                interactiveInternalsRemainOutOfMaterialScope:=interactiveInternalsRemainOutOfMaterialScope,
                primitiveConsumers:=PrimitiveConsumers(),
                defaultSurfaceConsumers:=DefaultSurfaceConsumers(),
                directContainerConsumers:=DirectContainerConsumers(),
                frameBackedConsumers:=FrameBackedConsumers())
        End Function

        Private Shared Function DetermineStatus(hasCardPrimitiveRuntimeRoute As Boolean,
                                                hasDefaultSurfaceConsumerRuntimeRoute As Boolean,
                                                hasDirectContainerRuntimeRoute As Boolean,
                                                hasFrameBodyMaterialRoute As Boolean,
                                                hasNoExplicitOnlySurfaceGate As Boolean,
                                                interactiveInternalsRemainOutOfMaterialScope As Boolean) As MASVisualComponentSurfaceMaterialAdoptionStatus
            If Not hasCardPrimitiveRuntimeRoute Then Return MASVisualComponentSurfaceMaterialAdoptionStatus.BlockedByCardPrimitiveRoute
            If Not hasDefaultSurfaceConsumerRuntimeRoute OrElse Not hasNoExplicitOnlySurfaceGate Then Return MASVisualComponentSurfaceMaterialAdoptionStatus.BlockedByDefaultSurfaceConsumerRoute
            If Not hasDirectContainerRuntimeRoute Then Return MASVisualComponentSurfaceMaterialAdoptionStatus.BlockedByDirectContainerRoute
            If Not hasFrameBodyMaterialRoute Then Return MASVisualComponentSurfaceMaterialAdoptionStatus.BlockedByFrameBodyMaterialRoute
            If Not interactiveInternalsRemainOutOfMaterialScope Then Return MASVisualComponentSurfaceMaterialAdoptionStatus.BlockedByInteractiveScopeLeak
            Return MASVisualComponentSurfaceMaterialAdoptionStatus.Closed
        End Function

        Private Shared Function ProbeExplicitMaterialOverrideProtection() As Boolean
            Dim scope As IDisposable = MASSurfaceMaterialRuntimeSwitch.BeginMaterialScope(MASSurfaceMaterialIds.CrystalIce)

            Try
                Dim resolvedKey As String = MASSurfaceMaterialRuntimeSwitch.ResolveRuntimeMaterialKey(
                    consumerKind:=MASSurfaceTreatmentConsumerKind.CardSurface,
                    requestedMaterialKey:=MASSurfaceMaterialIds.SoftMist,
                    hasExplicitMaterialOverride:=True,
                    slot:=MASSurfaceSlot.Root)

                Return String.Equals(resolvedKey, MASSurfaceMaterialIds.SoftMist, StringComparison.OrdinalIgnoreCase)
            Finally
                If scope IsNot Nothing Then scope.Dispose()
            End Try
        End Function

        Private Shared Function PrimitiveConsumers() As IReadOnlyList(Of String)
            Return New ReadOnlyCollection(Of String)(New List(Of String) From {
                "MASAppLayout",
                "MASSplitView",
                "MASAgendaView",
                "MASAppliedFiltersBar",
                "MASChartLegend",
                "MASDashboardGrid",
                "MASFilterBuilder",
                "MASKanbanBoard",
                "MASMasterDetailView",
                "MASMetricCard",
                "MASPivotTable",
                "MASTreeGrid",
                "MASCallout",
                "MASEmptyState",
                "MASFormValidationSummary",
                "MASLoadingSkeleton",
                "MASNotificationPanel",
                "MASRating",
                "MASStatusBanner",
                "MASCalendarView",
                "MASImageViewer",
                "MASContentCarousel",
                "MASFileDropZone",
                "MASNavigationRail",
                "MASWizard",
                "MASTransferList",
                "MASCard"
            })
        End Function

        Private Shared Function DefaultSurfaceConsumers() As IReadOnlyList(Of String)
            Return New ReadOnlyCollection(Of String)(New List(Of String) From {
                "MASBackground",
                "MASCard",
                "MASGroupBox",
                "MASToast",
                "MASTooltip",
                "MASTopBar",
                "MASToolbar"
            })
        End Function

        Private Shared Function DirectContainerConsumers() As IReadOnlyList(Of String)
            Return New ReadOnlyCollection(Of String)(New List(Of String) From {
                "MASExpander",
                "MASMenuBar",
                "MASCommandPalette",
                "MASDataForm",
                "MASPropertyGrid"
            })
        End Function

        Private Shared Function FrameBackedConsumers() As IReadOnlyList(Of String)
            Return New ReadOnlyCollection(Of String)(New List(Of String) From {
                "MASListBox",
                "MASListView",
                "MASTreeRenderer",
                "MASDropDownListFloatContent",
                "DropDownMenuRenderer",
                "MASToastRenderer"
            })
        End Function
    End Class

End Namespace
