Option Strict On
Option Explicit On

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Current visual-surface treatment inventory report.
    ''' It is intentionally static evidence and must not instantiate controls or render bitmaps.
    ''' </summary>
    Friend NotInheritable Class MASSurfaceTreatmentOwnershipReport

        Private ReadOnly _entries As MASSurfaceTreatmentInventoryEntry()
        Private ReadOnly _materialProfiles As MASSurfaceTreatmentMaterialProfile()

        Friend ReadOnly Property Name As String
        Friend ReadOnly Property ExistingMaterialRegistryName As String
        Friend ReadOnly Property OfficialFrameOwnerName As String
        Friend ReadOnly Property EntryCount As Integer
        Friend ReadOnly Property SurfaceVisualOwnedCount As Integer
        Friend ReadOnly Property LegacyPainterSurfaceCount As Integer
        Friend ReadOnly Property CapturedMaterialRequiredCount As Integer
        Friend ReadOnly Property FramePairingRequiredCount As Integer
        Friend ReadOnly Property IdentityPreservingMaterialCount As Integer
        Friend ReadOnly Property CompatibilityAliasCount As Integer
        Friend ReadOnly Property HasParallelSurfaceTreatmentSystemBlocker As Boolean
        Friend ReadOnly Property HasCompleteOwnershipInventory As Boolean
        Friend ReadOnly Property ExcludesInteractiveControlConsumers As Boolean

        Friend Sub New(name As String,
                       entries As MASSurfaceTreatmentInventoryEntry(),
                       materialProfiles As MASSurfaceTreatmentMaterialProfile())

            Me.Name = Normalize(name, "Surface Treatment Ownership Inventory")
            _entries = If(entries, New MASSurfaceTreatmentInventoryEntry() {})
            _materialProfiles = If(materialProfiles, New MASSurfaceTreatmentMaterialProfile() {})
            ExistingMaterialRegistryName = NameOf(MASSurfaceMaterialRegistry)
            OfficialFrameOwnerName = "VisualSurfaceFrame"

            EntryCount = _entries.Length
            SurfaceVisualOwnedCount = CountEntries(MASSurfaceTreatmentOwnershipStatus.SurfaceVisualOwned)
            LegacyPainterSurfaceCount = CountEntries(MASSurfaceTreatmentOwnershipStatus.DocumentedLegacyPainterSurface) + CountEntries(MASSurfaceTreatmentOwnershipStatus.CurrentVisualCaptureRequired)
            CapturedMaterialRequiredCount = CountCapturedEntries()
            FramePairingRequiredCount = CountFramePairingEntries()
            IdentityPreservingMaterialCount = CountIdentityPreservingProfiles()
            CompatibilityAliasCount = CountCompatibilityAliasProfiles()
            HasParallelSurfaceTreatmentSystemBlocker = CountEntries(MASSurfaceTreatmentOwnershipStatus.BlockedByParallelPath) > 0
            ExcludesInteractiveControlConsumers = ExcludesInteractiveControlConsumersFromEntries()
            HasCompleteOwnershipInventory = EntryCount = MASSurfaceTreatmentConsumerCatalog.CurrentOwnershipConsumerCount AndAlso
                                            SurfaceVisualOwnedCount = MASSurfaceTreatmentConsumerCatalog.CurrentOwnershipConsumerCount AndAlso
                                            ContainsAllCatalogConsumers() AndAlso
                                            ExcludesInteractiveControlConsumers AndAlso
                                            Not HasParallelSurfaceTreatmentSystemBlocker
        End Sub

        Friend ReadOnly Property Entries As MASSurfaceTreatmentInventoryEntry()
            Get
                Return DirectCast(_entries.Clone(), MASSurfaceTreatmentInventoryEntry())
            End Get
        End Property

        Friend ReadOnly Property MaterialProfiles As MASSurfaceTreatmentMaterialProfile()
            Get
                Return DirectCast(_materialProfiles.Clone(), MASSurfaceTreatmentMaterialProfile())
            End Get
        End Property

        Private Function ExcludesInteractiveControlConsumersFromEntries() As Boolean
            For Each entry As MASSurfaceTreatmentInventoryEntry In _entries
                If entry Is Nothing Then Return False
                If Not MASSurfaceTreatmentConsumerCatalog.ExcludesInteractiveControlConsumer(entry.ConsumerKind) Then Return False
            Next
            Return True
        End Function

        Friend Function ContainsConsumerKind(kind As MASSurfaceTreatmentConsumerKind) As Boolean
            For Each entry As MASSurfaceTreatmentInventoryEntry In _entries
                If entry IsNot Nothing AndAlso entry.ConsumerKind = kind Then Return True
            Next

            Return False
        End Function


        Private Function ContainsAllCatalogConsumers() As Boolean
            For Each consumerKind As MASSurfaceTreatmentConsumerKind In MASSurfaceTreatmentConsumerCatalog.CurrentOwnershipConsumers()
                If Not ContainsConsumerKind(consumerKind) Then Return False
            Next
            Return True
        End Function
        Private Function CountEntries(status As MASSurfaceTreatmentOwnershipStatus) As Integer
            Dim count As Integer = 0

            For Each entry As MASSurfaceTreatmentInventoryEntry In _entries
                If entry IsNot Nothing AndAlso entry.Status = status Then count += 1
            Next

            Return count
        End Function

        Private Function CountCapturedEntries() As Integer
            Dim count As Integer = 0

            For Each entry As MASSurfaceTreatmentInventoryEntry In _entries
                If entry IsNot Nothing AndAlso entry.RequiresCapturedMaterial Then count += 1
            Next

            Return count
        End Function

        Private Function CountFramePairingEntries() As Integer
            Dim count As Integer = 0

            For Each entry As MASSurfaceTreatmentInventoryEntry In _entries
                If entry IsNot Nothing AndAlso entry.RequiresMaterialFramePairing Then count += 1
            Next

            Return count
        End Function

        Private Function CountIdentityPreservingProfiles() As Integer
            Dim count As Integer = 0

            For Each profile As MASSurfaceTreatmentMaterialProfile In _materialProfiles
                If profile IsNot Nothing AndAlso profile.ThemeInfluenceMode = MASSurfaceTreatmentThemeInfluenceMode.IdentityPreserving Then count += 1
            Next

            Return count
        End Function

        Private Function CountCompatibilityAliasProfiles() As Integer
            Dim count As Integer = 0

            For Each profile As MASSurfaceTreatmentMaterialProfile In _materialProfiles
                If profile IsNot Nothing AndAlso profile.IsCompatibilityAlias Then count += 1
            Next

            Return count
        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

    End Class

End Namespace
