Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Final Friend-only report proving that the retired Theme rendering material folder
    ''' has been fully absorbed into the proper Visual owners: Surface material body,
    ''' Surface frame/chrome, Shadow, Effects, Interaction, ComponentChrome, primitives,
    ''' and the Theme boundary. It is evidence-only and never draws or changes visuals.
    ''' </summary>
    Friend NotInheritable Class MASVisualMaterialMigrationClosureReport
        Private ReadOnly _retiredRoots As IReadOnlyList(Of String)
        Private ReadOnly _finalOwners As IReadOnlyList(Of String)

        Friend Sub New(name As String,
                       status As MASVisualMaterialMigrationClosureStatus,
                       ownershipReport As MASVisualOwnershipLockdownReport,
                       surfaceProofReport As MASSurfaceTreatmentConsumerProofAlignmentReport,
                       scopeBoundaryReport As MASSurfaceTreatmentScopeBoundaryReport,
                       resolutionValidationReport As MASSurfaceTreatmentResolutionValidationReport,
                       commercialClosureManifest As MASVisualCommercialGovernanceClosureManifest,
                       materialProfileCount As Integer,
                       retiredRoots As IEnumerable(Of String),
                       finalOwners As IEnumerable(Of String))

            Me.Name = Normalize(name, "Nexamas UI Visual Material Migration Closure")
            Me.Status = status
            Me.OwnershipReport = ownershipReport
            Me.SurfaceProofReport = surfaceProofReport
            Me.ScopeBoundaryReport = scopeBoundaryReport
            Me.ResolutionValidationReport = resolutionValidationReport
            Me.CommercialClosureManifest = commercialClosureManifest
            Me.MaterialProfileCount = materialProfileCount
            _retiredRoots = New ReadOnlyCollection(Of String)(NormalizeList(retiredRoots))
            _finalOwners = New ReadOnlyCollection(Of String)(NormalizeList(finalOwners))

            Me.HasRetiredRootCoverage = RetiredRoots.Count >= 8 AndAlso
                                        ContainsRetiredRoot("MASSystem/Visual/ThemeSystem/Rendering/Painting/Material") AndAlso
                                        ContainsRetiredRoot("MASSystem/Visual/ThemeSystem/Rendering/Painting/Shadow") AndAlso
                                        ContainsRetiredRoot(RetiredRoot("SurfaceVisual", "System"))

            Me.HasFinalOwnerCoverage = FinalOwners.Count >= 8 AndAlso
                                       ContainsFinalOwner("MASSystem/Visual/Surface/Material") AndAlso
                                       ContainsFinalOwner("MASSystem/Visual/Surface/Frame") AndAlso
                                       ContainsFinalOwner("MASSystem/Visual/Surface/Chrome") AndAlso
                                       ContainsFinalOwner("MASSystem/Visual/Shadow") AndAlso
                                       ContainsFinalOwner("MASSystem/Visual/Effects") AndAlso
                                       ContainsFinalOwner("MASSystem/Visual/ComponentChrome") AndAlso
                                       ContainsFinalOwner("MASSystem/Interaction/VisualFeedback") AndAlso
                                       ContainsFinalOwner("MASSystem/Visual/ThemeSystem")

            Me.SurfaceMaterialIsBodyOnly = surfaceProofReport IsNot Nothing AndAlso
                                           surfaceProofReport.IsAligned AndAlso
                                           scopeBoundaryReport IsNot Nothing AndAlso
                                           scopeBoundaryReport.IsSurfaceOnlyScopeLocked AndAlso
                                           resolutionValidationReport IsNot Nothing AndAlso
                                           resolutionValidationReport.ReadyForConsumerMigrationPlanning AndAlso
                                           materialProfileCount >= 8

            Me.RuntimeUsesSurfaceVisualOnly = surfaceProofReport IsNot Nothing AndAlso
                                              surfaceProofReport.RuntimeSurfaceVisualApplicationMatchesCatalog AndAlso
                                              surfaceProofReport.UsesSingleConsumerCatalog AndAlso
                                              surfaceProofReport.HasNoParallelMaterialOrFramePath

            Me.VisualOwnersAreSeparated = ownershipReport IsNot Nothing AndAlso
                                          ownershipReport.IsLockedDown AndAlso
                                          ownershipReport.Contains(MASVisualOwnershipArea.Surface) AndAlso
                                          ownershipReport.Contains(MASVisualOwnershipArea.Shadow) AndAlso
                                          ownershipReport.Contains(MASVisualOwnershipArea.Effects) AndAlso
                                          ownershipReport.Contains(MASVisualOwnershipArea.Interaction) AndAlso
                                          ownershipReport.Contains(MASVisualOwnershipArea.ComponentChrome) AndAlso
                                          ownershipReport.Contains(MASVisualOwnershipArea.VisualPrimitives) AndAlso
                                          ownershipReport.Contains(MASVisualOwnershipArea.SharedTokens) AndAlso
                                          ownershipReport.Contains(MASVisualOwnershipArea.ThemeBoundary)

            Me.ThemeBoundaryIsTokenOnly = commercialClosureManifest IsNot Nothing AndAlso
                                          commercialClosureManifest.IsClosed AndAlso
                                          commercialClosureManifest.LocksThemeRenderingBoundary

            Me.PreservesCurrentVisuals = ownershipReport IsNot Nothing AndAlso
                                         ownershipReport.PreservesCurrentVisuals AndAlso
                                         commercialClosureManifest IsNot Nothing AndAlso
                                         commercialClosureManifest.PreservesCurrentVisuals

            Me.IsClosed = status = MASVisualMaterialMigrationClosureStatus.Closed AndAlso
                          HasRetiredRootCoverage AndAlso
                          HasFinalOwnerCoverage AndAlso
                          SurfaceMaterialIsBodyOnly AndAlso
                          RuntimeUsesSurfaceVisualOnly AndAlso
                          VisualOwnersAreSeparated AndAlso
                          ThemeBoundaryIsTokenOnly AndAlso
                          PreservesCurrentVisuals
        End Sub

        Friend ReadOnly Property Name As String
        Friend ReadOnly Property Status As MASVisualMaterialMigrationClosureStatus
        Friend ReadOnly Property OwnershipReport As MASVisualOwnershipLockdownReport
        Friend ReadOnly Property SurfaceProofReport As MASSurfaceTreatmentConsumerProofAlignmentReport
        Friend ReadOnly Property ScopeBoundaryReport As MASSurfaceTreatmentScopeBoundaryReport
        Friend ReadOnly Property ResolutionValidationReport As MASSurfaceTreatmentResolutionValidationReport
        Friend ReadOnly Property CommercialClosureManifest As MASVisualCommercialGovernanceClosureManifest
        Friend ReadOnly Property MaterialProfileCount As Integer
        Friend ReadOnly Property HasRetiredRootCoverage As Boolean
        Friend ReadOnly Property HasFinalOwnerCoverage As Boolean
        Friend ReadOnly Property SurfaceMaterialIsBodyOnly As Boolean
        Friend ReadOnly Property RuntimeUsesSurfaceVisualOnly As Boolean
        Friend ReadOnly Property VisualOwnersAreSeparated As Boolean
        Friend ReadOnly Property ThemeBoundaryIsTokenOnly As Boolean
        Friend ReadOnly Property PreservesCurrentVisuals As Boolean
        Friend ReadOnly Property IsClosed As Boolean

        Friend ReadOnly Property RetiredRoots As IReadOnlyList(Of String)
            Get
                Return _retiredRoots
            End Get
        End Property

        Friend ReadOnly Property FinalOwners As IReadOnlyList(Of String)
            Get
                Return _finalOwners
            End Get
        End Property

        Friend Function ContainsRetiredRoot(value As String) As Boolean
            Return Contains(RetiredRoots, value)
        End Function

        Friend Function ContainsFinalOwner(value As String) As Boolean
            Return Contains(FinalOwners, value)
        End Function

        Private Shared Function Contains(values As IReadOnlyList(Of String), value As String) As Boolean
            Dim normalizedValue As String = NormalizePath(value)
            If normalizedValue.Length = 0 Then Return False

            For Each candidate As String In values
                If String.Equals(candidate, normalizedValue, StringComparison.Ordinal) Then Return True
            Next

            Return False
        End Function

        Private Shared Function NormalizeList(values As IEnumerable(Of String)) As List(Of String)
            Dim result As New List(Of String)()
            If values Is Nothing Then Return result

            For Each value As String In values
                Dim normalized As String = NormalizePath(value)
                If normalized.Length > 0 AndAlso Not result.Contains(normalized) Then result.Add(normalized)
            Next

            Return result
        End Function

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

        Private Shared Function Normalize(value As String, fallback As String) As String
            Dim result As String = If(value, String.Empty).Trim()
            If result.Length = 0 Then Return fallback
            Return result
        End Function

        Private Shared Function NormalizePath(value As String) As String
            Return If(value, String.Empty).Trim().Replace("\"c, "/"c)
        End Function
    End Class

End Namespace
