Option Strict On
Option Explicit On

Namespace Nexamas.UI.Rendering

    Friend NotInheritable Class MASSurfaceMaterialAdoptionAudit

        Private Sub New()
        End Sub

        Friend Shared Function Build(componentExplicitRuntimeBypassCount As Integer) As MASSurfaceMaterialAdoptionReport
            Dim expectedMappedConsumers As Integer = 11
            Dim mappedConsumers As Integer = CountKnownConsumerMappings()
            Dim hasConsumerSlotCatalog As Boolean = mappedConsumers >= expectedMappedConsumers
            Dim hasExplicitSlotRuntimeGateway As Boolean = HasExplicitSlotGatewayProbe()

            Dim status As MASSurfaceMaterialAdoptionStatus = MASSurfaceMaterialAdoptionStatus.SlotAdoptionReady
            If Not hasConsumerSlotCatalog Then status = MASSurfaceMaterialAdoptionStatus.MissingConsumerSlotCatalog
            If Not hasExplicitSlotRuntimeGateway Then status = MASSurfaceMaterialAdoptionStatus.BlockedByMissingSlotRuntimeGateway
            If mappedConsumers < expectedMappedConsumers Then status = MASSurfaceMaterialAdoptionStatus.BlockedByConsumerSlotMappingGap
            If componentExplicitRuntimeBypassCount > 0 Then status = MASSurfaceMaterialAdoptionStatus.BlockedByComponentExplicitRuntimeBypass

            Return New MASSurfaceMaterialAdoptionReport(
                name:="Surface Material Adoption",
                status:=status,
                mappedConsumerCount:=mappedConsumers,
                expectedMappedConsumerCount:=expectedMappedConsumers,
                hasConsumerSlotCatalog:=hasConsumerSlotCatalog,
                hasExplicitSlotRuntimeGateway:=hasExplicitSlotRuntimeGateway,
                componentBypassCount:=componentExplicitRuntimeBypassCount)
        End Function

        Private Shared Function CountKnownConsumerMappings() As Integer
            Dim count As Integer = 0
            Dim consumers As MASSurfaceTreatmentConsumerKind() = New MASSurfaceTreatmentConsumerKind() {
                MASSurfaceTreatmentConsumerKind.ApplicationChromeSurface,
                MASSurfaceTreatmentConsumerKind.PanelShellSurface,
                MASSurfaceTreatmentConsumerKind.FloatingOverlaySurface,
                MASSurfaceTreatmentConsumerKind.CardSurface,
                MASSurfaceTreatmentConsumerKind.ToastSurface,
                MASSurfaceTreatmentConsumerKind.TooltipSurface,
                MASSurfaceTreatmentConsumerKind.ProgressSurface,
                MASSurfaceTreatmentConsumerKind.BackgroundSurface,
                MASSurfaceTreatmentConsumerKind.TopBarSurface,
                MASSurfaceTreatmentConsumerKind.ToolbarSurface,
                MASSurfaceTreatmentConsumerKind.GroupBoxSurface
            }

            For Each consumer As MASSurfaceTreatmentConsumerKind In consumers
                If MASSurfaceMaterialConsumerSlotCatalog.IsKnownBroadSurfaceConsumer(consumer) AndAlso
                   MASSurfaceMaterialConsumerSlotCatalog.ResolveDefaultSlot(consumer) <> MASSurfaceSlot.Unknown Then
                    count += 1
                End If
            Next

            Return count
        End Function

        Private Shared Function HasExplicitSlotGatewayProbe() As Boolean
            Dim route As MASSurfaceMaterialSlotRoute = MASSurfaceMaterialSlotResolver.ResolveMaterialKey(
                consumerKind:=MASSurfaceTreatmentConsumerKind.TopBarSurface,
                materialKey:="frosted glass",
                slot:=MASSurfaceMaterialConsumerSlotCatalog.ResolveDefaultSlot(MASSurfaceTreatmentConsumerKind.TopBarSurface),
                fallbackRole:=MASSurfaceRole.ApplicationChrome,
                frameStrength:=MASSurfaceFrameStrength.Level4)

            Return route IsNot Nothing AndAlso
                   String.Equals(route.MaterialKey, MASSurfaceMaterialIds.FrostedGlass, System.StringComparison.OrdinalIgnoreCase) AndAlso
                   route.Slot = MASSurfaceSlot.Header
        End Function

    End Class

End Namespace
