Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Theming
Imports SkiaSharp

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Resolves reusable header chrome colors from MASSurfaceThemeProfile.Chrome and,
    ''' when a non-default material is used, applies the same material-derived header
    ''' treatment that the previous PanelShell header painter used.
    ''' </summary>
    Friend NotInheritable Class MASSurfaceHeaderChromePlanner

        Private Sub New()
        End Sub

        Friend Shared Function Resolve(context As MASSurfaceChromeContext) As MASSurfaceHeaderChromePlan
            If context Is Nothing OrElse context.SurfaceTheme Is Nothing Then
                Dim safeThemeContext As MASThemeContext = Nothing
                Dim safeMaterialId As String = MASSurfaceMaterialIds.Auto
                Dim safeBounds As SKRect = SKRect.Empty
                Dim safeBaseBounds As SKRect = SKRect.Empty
                Dim safeRadius As Single = 0.0F
                Dim safeBorderStrength As Integer = 3
                Dim safeSurfaceStrength As Integer = 3
                Dim safeDpi As Single = 1.0F
                Dim safeRole As MASSurfaceRole = MASSurfaceRole.Unknown
                Dim safeState As MASSurfaceVisualState = MASSurfaceVisualState.Normal

                If context IsNot Nothing Then
                    safeThemeContext = context.ThemeContext
                    safeMaterialId = context.MaterialId
                    safeBounds = context.BoundsPx
                    safeBaseBounds = context.BaseBoundsPx
                    safeRadius = context.CornerRadiusPx
                    safeBorderStrength = context.BorderStrengthLevel
                    safeSurfaceStrength = context.SurfaceStrengthLevel
                    safeDpi = context.Dpi
                    safeRole = context.SurfaceRole
                    safeState = context.VisualState
                End If

                Dim fallbackTheme As MASSurfaceThemeProfile =
                    MASSurfaceThemeProfileResolver.Resolve(safeThemeContext)

                Return Resolve(New MASSurfaceChromeContext(
                    themeContext:=safeThemeContext,
                    surfaceTheme:=fallbackTheme,
                    materialId:=safeMaterialId,
                    boundsPx:=safeBounds,
                    baseBoundsPx:=safeBaseBounds,
                    cornerRadiusPx:=safeRadius,
                    borderStrengthLevel:=safeBorderStrength,
                    surfaceStrengthLevel:=safeSurfaceStrength,
                    dpi:=safeDpi,
                    surfaceRole:=safeRole,
                    visualState:=safeState))
            End If

            Dim chrome As MASSurfaceChromeThemeProfile = context.SurfaceTheme.Chrome

            Dim top As SKColor = chrome.HeaderTop
            Dim mid As SKColor = chrome.HeaderMid
            Dim bottom As SKColor = chrome.HeaderBottom
            Dim sheen As SKColor = chrome.HeaderSheen
            Dim separator As SKColor = chrome.HeaderSeparator

            If UsesMaterialDrivenHeader(context.MaterialId) Then
                Dim materialContext As New MASSurfaceMaterialContext(
                    themeContext:=context.ThemeContext,
                    surfaceTheme:=context.SurfaceTheme,
                    surfaceStrengthLevel:=context.SurfaceStrengthLevel,
                    dpi:=context.Dpi,
                    boundsPx:=context.BoundsPx,
                    cornerRadiusPx:=context.CornerRadiusPx,
                    visualState:=context.VisualState,
                    surfaceRole:=context.SurfaceRole)

                Dim palette As MASSurfaceMaterialPalette =
                    MASSurfaceMaterialThemeResolver.Resolve(context.MaterialId, materialContext)

                top = MASSurfaceMaterialPaintUtils.Mix(palette.Top, SKColors.White, 0.16F, palette.Top.Alpha)
                mid = MASSurfaceMaterialPaintUtils.Mix(palette.Mid, palette.Accent, 0.045F, palette.Mid.Alpha)
                bottom = MASSurfaceMaterialPaintUtils.Mix(palette.Bottom, SKColors.Black, 0.035F, palette.Bottom.Alpha)
                sheen = palette.Glow
                separator = palette.Line
            End If

            Return New MASSurfaceHeaderChromePlan(
                headerTop:=top,
                headerMid:=mid,
                headerBottom:=bottom,
                headerSheen:=sheen,
                headerSeparator:=separator,
                textTitle:=chrome.TextTitle,
                textSubtitle:=chrome.TextSubtitle,
                textIcon:=chrome.TextIcon,
                textAccent:=chrome.TextAccent,
                shadowBase:=chrome.ShadowBase,
                shadowAmbient:=chrome.ShadowAmbient,
                shadowContact:=chrome.ShadowContact,
                dividerLight:=chrome.DividerLight,
                dividerDark:=chrome.DividerDark,
                glow:=chrome.Glow)
        End Function

        Private Shared Function UsesMaterialDrivenHeader(materialId As String) As Boolean
            Dim safeId As String = MASSurfaceTreatmentMaterialKeyNormalizer.NormalizeForStorage(materialId)
            Return safeId.Length > 0 AndAlso
                   Not String.Equals(safeId, MASSurfaceMaterialIds.Auto, StringComparison.OrdinalIgnoreCase) AndAlso
                   Not String.Equals(safeId, MASSurfaceMaterialIds.PlatformDefault, StringComparison.OrdinalIgnoreCase)
        End Function
    End Class

End Namespace
