Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Builds the first planned pairing between existing materials and material-specific frame ids.
    ''' This remains governance/planning only: it does not build frame specs, call frame painters, or rewire renderers.
    ''' </summary>
    Friend NotInheritable Class MASSurfaceTreatmentFramePairingPlanner

        Private Sub New()
        End Sub

        Friend Shared Function BuildLowRiskMaterialFramePairingPlan() As MASSurfaceTreatmentFramePairingReport
            Dim entries As New List(Of MASSurfaceTreatmentFramePairingEntry)()

            For Each profile As MASSurfaceTreatmentMaterialProfile In MASSurfaceTreatmentMaterialProfileCatalog.RegisteredProfiles()
                If profile IsNot Nothing Then entries.Add(BuildEntry(profile))
            Next

            Return New MASSurfaceTreatmentFramePairingReport(
                name:="Surface Treatment Low-Risk Material/Frame Pairing Plan",
                entries:=entries.ToArray())
        End Function

        Private Shared Function BuildEntry(profile As MASSurfaceTreatmentMaterialProfile) As MASSurfaceTreatmentFramePairingEntry
            Dim frameKnown As Boolean = MASSurfaceFrameIds.IsKnownFrameKey(profile.RecommendedFrameKey)
            Dim status As MASSurfaceTreatmentFramePairingStatus = DetermineStatus(profile, frameKnown)

            Return New MASSurfaceTreatmentFramePairingEntry(
                materialKey:=profile.MaterialKey,
                recommendedFrameKey:=profile.RecommendedFrameKey,
                themeInfluenceMode:=profile.ThemeInfluenceMode,
                status:=status,
                isCompatibilityAlias:=profile.IsCompatibilityAlias,
                preservesMaterialIdentity:=profile.PreservesMaterialIdentity,
                usesOfficialSurfaceFrame:=frameKnown,
                keepsSingleFrameStrengthModel:=True,
                defersRuntimeFrameRepaint:=True,
                notes:=BuildNotes(profile, frameKnown))
        End Function

        Private Shared Function DetermineStatus(profile As MASSurfaceTreatmentMaterialProfile,
                                                frameKnown As Boolean) As MASSurfaceTreatmentFramePairingStatus
            If profile Is Nothing OrElse Not frameKnown Then Return MASSurfaceTreatmentFramePairingStatus.BlockedByMissingSurfaceFrameId
            If profile.IsCompatibilityAlias Then Return MASSurfaceTreatmentFramePairingStatus.CompatibilityAliasUsesCanonicalFrame

            Select Case profile.ThemeInfluenceMode
                Case MASSurfaceTreatmentThemeInfluenceMode.IdentityPreserving,
                     MASSurfaceTreatmentThemeInfluenceMode.AccentOnly
                    Return MASSurfaceTreatmentFramePairingStatus.PlannedIdentityPreservingMaterialFramePairing

                Case MASSurfaceTreatmentThemeInfluenceMode.ThemeTinted
                    Return MASSurfaceTreatmentFramePairingStatus.PlannedThemeTintedMaterialFramePairing

                Case Else
                    Return MASSurfaceTreatmentFramePairingStatus.PlannedPlatformDefaultFramePairing
            End Select
        End Function

        Private Shared Function BuildNotes(profile As MASSurfaceTreatmentMaterialProfile,
                                           frameKnown As Boolean) As String
            If profile Is Nothing Then Return "Missing profile; pairing is blocked."
            If Not frameKnown Then Return "Recommended frame key is not declared by the existing VisualSurfaceFrame registry."
            If profile.IsCompatibilityAlias Then Return "Compatibility alias uses the canonical material frame instead of duplicating a native frame identity."
            If String.Equals(profile.RecommendedFrameKey, MASSurfaceFrameIds.PlatformDefault, StringComparison.OrdinalIgnoreCase) Then Return "Platform default keeps the current premium platform frame; no repaint is introduced."
            Return "Material-specific frame id is planned inside VisualSurfaceFrame; Level1-Level6 remains the single strength model and runtime frame repaint remains deferred."
        End Function

    End Class

End Namespace
