Option Strict On
Option Explicit On

Imports System

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Friend-only immutable material binding for one component region. It stores only an
    ''' official material key and a source policy; it never stores colors, paints, element names,
    ''' or drawing recipes.
    ''' </summary>
    Friend NotInheritable Class MASComponentSurfaceMaterialBinding

        Friend Sub New(region As MASComponentSurfaceRegion,
                       sourceKind As MASComponentSurfaceMaterialSourceKind,
                       materialKey As String)
            Me.Region = NormalizeRegion(region)
            Me.SourceKind = sourceKind
            Me.MaterialKey = NormalizeMaterialKey(materialKey, sourceKind)
        End Sub

        Friend ReadOnly Property Region As MASComponentSurfaceRegion
        Friend ReadOnly Property SourceKind As MASComponentSurfaceMaterialSourceKind
        Friend ReadOnly Property MaterialKey As String

        Friend ReadOnly Property IsAuthored As Boolean
            Get
                Return SourceKind = MASComponentSurfaceMaterialSourceKind.AuthoredMaterial
            End Get
        End Property

        Friend ReadOnly Property HasExplicitMaterialOverride As Boolean
            Get
                Return IsAuthored
            End Get
        End Property

        Friend Shared Function InheritApplication(region As MASComponentSurfaceRegion) As MASComponentSurfaceMaterialBinding
            Return New MASComponentSurfaceMaterialBinding(region, MASComponentSurfaceMaterialSourceKind.InheritApplication, MASSurfaceMaterialIds.PlatformDefault)
        End Function

        Friend Shared Function Authored(region As MASComponentSurfaceRegion,
                                        materialKey As String) As MASComponentSurfaceMaterialBinding
            Return New MASComponentSurfaceMaterialBinding(region, MASComponentSurfaceMaterialSourceKind.AuthoredMaterial, materialKey)
        End Function

        Private Shared Function NormalizeRegion(value As MASComponentSurfaceRegion) As MASComponentSurfaceRegion
            If System.Enum.IsDefined(GetType(MASComponentSurfaceRegion), value) Then Return value
            Return MASComponentSurfaceRegion.Root
        End Function

        Private Shared Function NormalizeMaterialKey(value As String,
                                                     sourceKind As MASComponentSurfaceMaterialSourceKind) As String
            If sourceKind = MASComponentSurfaceMaterialSourceKind.InheritApplication Then
                Return MASSurfaceMaterialIds.PlatformDefault
            End If

            Dim normalized As String = MASSurfaceMaterialRegistry.Canonicalize(
                MASSurfaceTreatmentMaterialKeyNormalizer.NormalizeForStorage(value))

            If MASSurfaceMaterialRuntimeSwitch.IsSwitchableDefaultMaterialKey(normalized) Then
                Return MASSurfaceMaterialIds.PlatformDefault
            End If

            If Not MASSurfaceMaterialRegistry.Contains(normalized) Then
                Return MASSurfaceMaterialIds.PlatformDefault
            End If

            Return normalized
        End Function

    End Class

End Namespace
