Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Final material-migration closure audit. It composes existing Visual/Surface evidence
    ''' and verifies that material is now only a material-body owner while shadow, frame,
    ''' chrome, effects, interaction and component chrome have their own final owners.
    ''' </summary>
    Friend NotInheritable Class MASVisualMaterialMigrationClosureAudit

        Private Sub New()
        End Sub

        Friend Shared Function BuildFinalMaterialMigrationClosure() As MASVisualMaterialMigrationClosureReport
            Dim ownership As MASVisualOwnershipLockdownReport = MASVisualOwnershipLockdownAudit.BuildCurrentVisualOwnershipLockdown()
            Dim surfaceProof As MASSurfaceTreatmentConsumerProofAlignmentReport = MASSurfaceTreatmentConsumerProofAlignmentAudit.BuildFinalConsumerProofAlignment()
            Dim scope As MASSurfaceTreatmentScopeBoundaryReport = MASSurfaceTreatmentScopeBoundaryAudit.BuildSurfaceOnlyScopeBoundary()
            Dim resolution As MASSurfaceTreatmentResolutionValidationReport = MASSurfaceTreatmentResolutionValidationBuilder.BuildCurrentCompatibilityValidation()
            Dim commercial As MASVisualCommercialGovernanceClosureManifest = MASVisualCommercialGovernanceClosureManifest.CreateCurrent()
            Dim materialProfiles As MASSurfaceTreatmentMaterialProfile() = MASSurfaceTreatmentMaterialProfileCatalog.RegisteredProfiles()
            Dim materialProfileCount As Integer = If(materialProfiles Is Nothing, 0, materialProfiles.Length)

            Dim surfaceReady As Boolean = surfaceProof IsNot Nothing AndAlso
                                          surfaceProof.IsAligned AndAlso
                                          scope IsNot Nothing AndAlso
                                          scope.IsSurfaceOnlyScopeLocked
            Dim runtimeReady As Boolean = surfaceProof IsNot Nothing AndAlso
                                          surfaceProof.RuntimeSurfaceVisualApplicationMatchesCatalog AndAlso
                                          surfaceProof.HasNoParallelMaterialOrFramePath AndAlso
                                          resolution IsNot Nothing AndAlso
                                          resolution.ReadyForConsumerMigrationPlanning AndAlso
                                          materialProfileCount >= 8
            Dim themeReady As Boolean = commercial IsNot Nothing AndAlso
                                        commercial.IsClosed AndAlso
                                        commercial.LocksThemeRenderingBoundary
            Dim ownershipReady As Boolean = ownership IsNot Nothing AndAlso ownership.IsLockedDown

            Dim status As MASVisualMaterialMigrationClosureStatus = DetermineStatus(
                surfaceReady,
                runtimeReady,
                themeReady,
                ownershipReady)

            Return New MASVisualMaterialMigrationClosureReport(
                name:="Nexamas UI Visual Material Migration Closure",
                status:=status,
                ownershipReport:=ownership,
                surfaceProofReport:=surfaceProof,
                scopeBoundaryReport:=scope,
                resolutionValidationReport:=resolution,
                commercialClosureManifest:=commercial,
                materialProfileCount:=materialProfileCount,
                retiredRoots:=RetiredRoots(),
                finalOwners:=FinalOwners())
        End Function

        Private Shared Function DetermineStatus(surfaceReady As Boolean,
                                                runtimeReady As Boolean,
                                                themeReady As Boolean,
                                                ownershipReady As Boolean) As MASVisualMaterialMigrationClosureStatus
            If Not surfaceReady Then Return MASVisualMaterialMigrationClosureStatus.BlockedBySurfaceAlignment
            If Not runtimeReady Then Return MASVisualMaterialMigrationClosureStatus.BlockedByMaterialRuntime
            If Not themeReady Then Return MASVisualMaterialMigrationClosureStatus.BlockedByThemeBoundary
            If Not ownershipReady Then Return MASVisualMaterialMigrationClosureStatus.BlockedByOwnershipGap
            Return MASVisualMaterialMigrationClosureStatus.Closed
        End Function

        Private Shared Function RetiredRoots() As IReadOnlyList(Of String)
            Return New ReadOnlyCollection(Of String)(New List(Of String) From {
                RetiredRoot("SurfaceVisual", "System"),
                "MASSystem/Visual/ThemeSystem/Rendering/Painting",
                "MASSystem/Visual/ThemeSystem/Rendering/Painting/Material",
                "MASSystem/Visual/ThemeSystem/Rendering/Painting/Shadow",
                "MASSystem/Visual/ThemeSystem/Interaction",
                "MASSystem/Visual/ThemeSystem/Surface",
                "MASSystem/ArchitectureSystem/Shadow",
                RetiredSurfaceRoot("Material", "System"),
                RetiredSurfaceRoot("SurfaceChrome", "System"),
                RetiredSurfaceRoot("SurfaceFrame", "System"),
                RetiredSurfaceRoot("SurfaceTheme", "System"),
                RetiredSurfaceRoot("SurfaceVisualStyle", "System")
            })
        End Function

        Private Shared Function RetiredRoot(firstNamePart As String, secondNamePart As String) As String
            Return "MASSystem/" & firstNamePart & secondNamePart
        End Function

        Private Shared Function RetiredSurfaceRoot(firstNamePart As String, secondNamePart As String) As String
            Return "MASSystem/Visual/Surface/" & firstNamePart & secondNamePart
        End Function

        Private Shared Function FinalOwners() As IReadOnlyList(Of String)
            Return New ReadOnlyCollection(Of String)(New List(Of String) From {
                "MASSystem/Visual/Surface/Material",
                "MASSystem/Visual/Surface/Frame",
                "MASSystem/Visual/Surface/Chrome",
                "MASSystem/Visual/Surface/Runtime",
                "MASSystem/Visual/Shadow",
                "MASSystem/Visual/Effects",
                "MASSystem/Interaction/VisualFeedback",
                "MASSystem/Visual/ComponentChrome",
                "MASSystem/Visual/Primitives",
                "MASSystem/Visual/SharedTokens",
                "MASSystem/Visual/ThemeSystem"
            })
        End Function

    End Class

End Namespace
