Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Rendering

    Friend Structure MASCardSurfaceVisualPalette
        Friend SurfaceTop As SKColor
        Friend SurfaceMid As SKColor
        Friend SurfaceBot As SKColor

        Friend Sheen As SKColor
        Friend InnerShade As SKColor
        Friend NoiseTint As SKColor

        Friend BorderOuter As SKColor
        Friend BorderInner As SKColor

        Friend ColdTint As SKColor

        Friend AccentTint As SKColor
        Friend AccentTopT As Single
        Friend AccentMidT As Single
        Friend AccentBotT As Single
    End Structure

    ''' <summary>
    ''' Resolves the shared card visual palette used by card surface fill and card frame rendering.
    ''' </summary>
    ''' <remarks>
    ''' The palette contains color inputs only. Elevation/shadow is deliberately resolved by ShadowSystem.
    ''' </remarks>
    Friend NotInheritable Class MASCardSurfaceVisualPaletteResolver

        Private Sub New()
        End Sub

        Friend Shared Function Resolve(ctx As MASThemeContext,
                                       visualStyle As MASCardVisualStyle) As MASCardSurfaceVisualPalette

            Dim p As New MASCardSurfaceVisualPalette

            If TryGetAppPalette(ctx, visualStyle, p) Then
                Return p
            End If

            Dim surfaceTheme As IMASFoundationSurfaceTheme = Nothing
            If ctx IsNot Nothing AndAlso ctx.Theme IsNot Nothing Then
                surfaceTheme = MASAppThemeContracts.Foundation(ctx.Theme).Surface
            End If

            If surfaceTheme Is Nothing Then
                p.SurfaceTop = Values.Color.Surfaces.Type.Default.SurfaceTop
                p.SurfaceMid = Values.Color.Surfaces.Type.Default.SurfaceMid
                p.SurfaceBot = Values.Color.Surfaces.Type.Default.SurfaceBot
                p.BorderOuter = ColorMath.WithAlpha(Values.Color.Core.Ink, 52)
                p.BorderInner = ColorMath.WithAlpha(SKColors.White, 36)
                p.Sheen = SKColors.White.WithAlpha(30)
                p.InnerShade = ColorMath.WithAlpha(SKColors.Black, 18)
                p.NoiseTint = SKColors.White.WithAlpha(10)
                p.ColdTint = Values.Color.Surfaces.Type.Default.SurfaceTop
                p.AccentTint = SKColors.Transparent
                p.AccentTopT = 0.0F
                p.AccentMidT = 0.0F
                p.AccentBotT = 0.0F
            Else
                p.SurfaceTop = surfaceTheme.SurfaceTop
                p.SurfaceMid = surfaceTheme.SurfaceMid
                p.SurfaceBot = surfaceTheme.SurfaceBot

                p.BorderOuter =
                    If(surfaceTheme.BorderOuter.Alpha = 0,
                       ColorMath.WithAlpha(Values.Color.Core.Ink, 52),
                       surfaceTheme.BorderOuter)

                p.BorderInner =
                    If(surfaceTheme.BorderInner.Alpha = 0,
                       ColorMath.WithAlpha(SKColors.White, 36),
                       surfaceTheme.BorderInner)

                p.Sheen = SKColors.White.WithAlpha(28)
                p.InnerShade = ColorMath.WithAlpha(SKColors.Black, 18)
                p.NoiseTint = SKColors.White.WithAlpha(10)
                p.ColdTint = surfaceTheme.BackgroundColdTint
                p.AccentTint = SKColors.Transparent
                p.AccentTopT = 0.0F
                p.AccentMidT = 0.0F
                p.AccentBotT = 0.0F
            End If

            Return p
        End Function

        Private Shared Function TryGetAppPalette(ctx As MASThemeContext,
                                                 visualStyle As MASCardVisualStyle,
                                                 ByRef outPal As MASCardSurfaceVisualPalette) As Boolean
            If ctx Is Nothing Then Return False

            Dim cards As IMASCardsTheme = ctx.CardsTheme
            If cards Is Nothing Then Return False

            Dim visuals As MASCardsVisualSet = cards.Visuals
            Dim pal As CardVisualPalette =
                If(visualStyle = MASCardVisualStyle.Secondary, visuals.SecondaryPalette, visuals.DefaultPalette)

            outPal = New MASCardSurfaceVisualPalette With {
                .SurfaceTop = pal.SurfaceTop,
                .SurfaceMid = pal.SurfaceMid,
                .SurfaceBot = pal.SurfaceBot,
                .Sheen = pal.Sheen,
                .InnerShade = pal.InnerShade,
                .NoiseTint = pal.NoiseTint,
                .BorderOuter = pal.BorderOuter,
                .BorderInner = pal.BorderInner,
                .ColdTint = pal.ColdTint,
                .AccentTint = pal.AccentTint,
                .AccentTopT = pal.AccentTopT,
                .AccentMidT = pal.AccentMidT,
                .AccentBotT = pal.AccentBotT
            }

            Return True
        End Function

    End Class

End Namespace
