Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Resolves the platform surface visual theme from the current ThemeContext.
    ''' VisualSurface consumes the ThemeSystem-owned FrameSurface and SurfaceChrome
    ''' families only; it must not read PanelShell-specific theme contracts.
    ''' </summary>
    Friend NotInheritable Class MASSurfaceThemeProfileResolver

        Private Sub New()
        End Sub

        Friend Shared Function Resolve(themeContext As MASThemeContext) As MASSurfaceThemeProfile

            Dim frameSource As IMASFrameSurfaceTheme = Nothing
            Dim chromeSource As IMASSurfaceChromeTheme = Nothing

            If themeContext IsNot Nothing AndAlso themeContext.Theme IsNot Nothing Then
                Dim families As IMASThemeFamilies = MASAppThemeContracts.Families(themeContext.Theme)
                frameSource = families.FrameSurface
                chromeSource = families.SurfaceChrome
            End If

            Dim frame As MASSurfaceFrameThemeProfile = ResolveFrame(frameSource)
            Dim chrome As MASSurfaceChromeThemeProfile = ResolveChrome(chromeSource, frame)
            Dim material As MASSurfaceMaterialThemeProfile = ResolveMaterial(frame, chrome)

            Return New MASSurfaceThemeProfile(
                material:=material,
                frame:=frame,
                chrome:=chrome)
        End Function

        Private Shared Function ResolveFrame(frameSource As IMASFrameSurfaceTheme) As MASSurfaceFrameThemeProfile

            If frameSource IsNot Nothing Then
                Return New MASSurfaceFrameThemeProfile(
                    fallbackFill:=frameSource.FallbackFill,
                    borderHairline:=frameSource.BorderHairline,
                    separatorHairline:=frameSource.SeparatorHairline,
                    surfaceTop:=frameSource.SurfaceTop,
                    surfaceMid:=frameSource.SurfaceMid,
                    surfaceBottom:=frameSource.SurfaceBot,
                    materialWash:=frameSource.MaterialWash,
                    coldTint:=frameSource.ColdTint,
                    borderOuter:=frameSource.BorderOuter,
                    borderInner:=frameSource.BorderInner,
                    shadowBase:=frameSource.ShadowBase,
                    borderAccent:=frameSource.BorderAccent,
                    topLift:=frameSource.TopLift,
                    bottomAnchor:=frameSource.BottomAnchor)
            End If

            Return New MASSurfaceFrameThemeProfile(
                fallbackFill:=New SKColor(225, 234, 242, 244),
                borderHairline:=New SKColor(255, 255, 255, 32),
                separatorHairline:=New SKColor(255, 255, 255, 36),
                surfaceTop:=New SKColor(238, 244, 249, 245),
                surfaceMid:=New SKColor(225, 234, 242, 244),
                surfaceBottom:=New SKColor(211, 223, 234, 242),
                materialWash:=New SKColor(255, 255, 255, 24),
                coldTint:=New SKColor(180, 220, 255, 28),
                borderOuter:=New SKColor(86, 155, 226, 96),
                borderInner:=New SKColor(255, 255, 255, 72),
                shadowBase:=Nexamas.UI.Values.Color.Core.ShadowBase,
                borderAccent:=New SKColor(86, 155, 226, 255),
                topLift:=New SKColor(255, 255, 255, 70),
                bottomAnchor:=New SKColor(0, 0, 0, 28))
        End Function

        Private Shared Function ResolveChrome(chromeSource As IMASSurfaceChromeTheme,
                                              frame As MASSurfaceFrameThemeProfile) As MASSurfaceChromeThemeProfile

            If chromeSource IsNot Nothing Then
                Return New MASSurfaceChromeThemeProfile(
                    headerTop:=chromeSource.HeaderTop,
                    headerMid:=chromeSource.HeaderMid,
                    headerBottom:=chromeSource.HeaderBottom,
                    headerSheen:=chromeSource.HeaderSheen,
                    headerSeparator:=chromeSource.HeaderSeparator,
                    textTitle:=chromeSource.TextTitle,
                    textSubtitle:=chromeSource.TextSubtitle,
                    textIcon:=chromeSource.TextIcon,
                    textDetail:=chromeSource.TextDetail,
                    textAccent:=chromeSource.TextAccent,
                    shadowBase:=chromeSource.ShadowBase,
                    shadowAmbient:=chromeSource.ShadowAmbient,
                    shadowContact:=chromeSource.ShadowContact,
                    defaultBorderStrengthLevel:=chromeSource.DefaultBorderStrength,
                    defaultSurfaceStrengthLevel:=chromeSource.DefaultSurfaceStrength,
                    dividerLight:=chromeSource.DividerLight,
                    dividerDark:=chromeSource.DividerDark,
                    glow:=chromeSource.Glow)
            End If

            Return New MASSurfaceChromeThemeProfile(
                headerTop:=frame.SurfaceTop,
                headerMid:=frame.SurfaceMid,
                headerBottom:=frame.SurfaceBottom,
                headerSheen:=frame.TopLift,
                headerSeparator:=frame.SeparatorHairline,
                textTitle:=New SKColor(28, 36, 46, 255),
                textSubtitle:=New SKColor(79, 92, 108, 255),
                textIcon:=New SKColor(50, 64, 82, 255),
                textDetail:=New SKColor(95, 108, 124, 255),
                textAccent:=frame.BorderAccent,
                shadowBase:=frame.ShadowBase,
                shadowAmbient:=frame.ShadowBase,
                shadowContact:=frame.ShadowBase,
                defaultBorderStrengthLevel:=3,
                defaultSurfaceStrengthLevel:=3,
                dividerLight:=frame.SeparatorHairline,
                dividerDark:=frame.BottomAnchor,
                glow:=frame.TopLift)
        End Function

        Private Shared Function ResolveMaterial(frame As MASSurfaceFrameThemeProfile,
                                                chrome As MASSurfaceChromeThemeProfile) As MASSurfaceMaterialThemeProfile

            ' Preserve the existing visible platform default by taking the surface body
            ' and frame accents from FrameSurface. Chrome/header values are still
            ' represented separately by the neutral SurfaceChrome family.
            Return New MASSurfaceMaterialThemeProfile(
                top:=frame.SurfaceTop,
                mid:=frame.SurfaceMid,
                bottom:=frame.SurfaceBottom,
                wash:=frame.MaterialWash,
                coldTint:=frame.ColdTint,
                accent:=frame.BorderAccent,
                line:=frame.SeparatorHairline,
                glow:=frame.TopLift,
                headerInnerRim:=frame.BorderInner,
                shadowBase:=frame.ShadowBase)
        End Function

    End Class

End Namespace
