Option Strict On
Option Explicit On

Imports Nexamas.UI.Architecture
Imports Nexamas.UI.Controls
Imports Nexamas.UI.General
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components


    Partial Friend NotInheritable Class ButtonSurfacePolicy
        Private Shared Function ResolveDangerBaseSurface(app As IMASAppTheme,
                                                         personality As MASButtonPersonality,
                                                         state As MASInteractionState) As MASButtonSurfaceTheme

            If MASAppThemeContracts.Foundation(app).Semantics Is Nothing OrElse MASAppThemeContracts.Foundation(app).TextColors Is Nothing Then
                Return ButtonPolicyDefaults.TransparentSurface()
            End If

            Dim danger As SKColor = MASAppThemeContracts.Foundation(app).Semantics.Danger
            Dim textDark As SKColor = MASAppThemeContracts.Foundation(app).TextColors.PrimaryText
            Dim textLight As SKColor = MASAppThemeContracts.Foundation(app).TextColors.InverseText

            Dim fillTop As SKColor =
                ColorMath.Mix(danger, textLight, If(personality = MASButtonPersonality.Tal, 0.1F, 0.07F))

            Dim fillBottom As SKColor =
                ColorMath.Mix(danger, textDark, If(personality = MASButtonPersonality.Tal, 0.34F, 0.42F))

            Dim result As New MASButtonSurfaceTheme(
                fillTop:=fillTop,
                fillBottom:=fillBottom,
                borderOuter:=ColorMath.Mix(ColorMath.Mix(danger, textDark, 0.28F), danger, 0.32F).WithAlpha(215),
                borderInner:=ColorMath.Mix(danger, textLight, 0.34F).WithAlpha(155),
                innerHighlight:=textLight.WithAlpha(If(personality = MASButtonPersonality.Tal, CByte(98), CByte(118))),
                innerShade:=textDark.WithAlpha(If(personality = MASButtonPersonality.Tal, CByte(64), CByte(92))),
                materialWash:=danger.WithAlpha(If(personality = MASButtonPersonality.Tal, CByte(32), CByte(44))),
                bodyGlow:=ColorMath.Mix(danger, textLight, 0.16F).WithAlpha(If(personality = MASButtonPersonality.Tal, CByte(18), CByte(34))),
                topSheen:=textLight.WithAlpha(If(personality = MASButtonPersonality.Tal, CByte(32), CByte(48))))

            If ButtonPolicyDefaults.Has(state, MASInteractionState.Disabled) Then
                result = BuildDisabledDangerSurface(result, app)
            End If

            Return result

        End Function




        Private Shared Function ApplyDangerStateSurface(surface As MASButtonSurfaceTheme,
                                                        app As IMASAppTheme,
                                                        personality As MASButtonPersonality,
                                                        state As MASInteractionState) As MASButtonSurfaceTheme

            If MASAppThemeContracts.Foundation(app).Semantics Is Nothing OrElse MASAppThemeContracts.Foundation(app).TextColors Is Nothing Then
                Return surface
            End If

            Dim light As SKColor = MASAppThemeContracts.Foundation(app).TextColors.InverseText
            Dim dark As SKColor = MASAppThemeContracts.Foundation(app).TextColors.PrimaryText
            Dim danger As SKColor = MASAppThemeContracts.Foundation(app).Semantics.Danger

            Dim top As SKColor = surface.FillTop
            Dim bottom As SKColor = surface.FillBottom
            Dim outer As SKColor = surface.BorderOuter
            Dim inner As SKColor = surface.BorderInner
            Dim highlight As SKColor = surface.InnerHighlight
            Dim shade As SKColor = surface.InnerShade
            Dim wash As SKColor = surface.MaterialWash
            Dim glow As SKColor = surface.BodyGlow
            Dim sheen As SKColor = surface.TopSheen

            Select Case state

                Case MASInteractionState.Hovered
                    top = ColorMath.Mix(top, light, 0.14F)
                    bottom = ColorMath.Mix(bottom, light, 0.04F)
                    outer = ColorMath.Mix(outer, light, 0.12F).WithAlpha(235)
                    inner = ColorMath.Mix(inner, light, 0.22F).WithAlpha(205)
                    highlight = light.WithAlpha(165)
                    wash = ColorMath.Mix(wash, light, 0.18F).WithAlpha(72)
                    glow = ColorMath.Mix(glow, light, 0.32F).WithAlpha(76)
                    sheen = light.WithAlpha(92)

                Case MASInteractionState.Pressed
                    top = ColorMath.Mix(top, dark, 0.16F)
                    bottom = ColorMath.Mix(bottom, dark, 0.24F)
                    outer = ColorMath.Mix(outer, dark, 0.24F).WithAlpha(235)
                    inner = ColorMath.Mix(inner, danger, 0.18F).WithAlpha(135)
                    highlight = highlight.WithAlpha(72)
                    shade = ColorMath.Mix(shade, dark, 0.25F).WithAlpha(130)
                    wash = ColorMath.Mix(wash, dark, 0.18F).WithAlpha(42)
                    glow = glow.WithAlpha(18)
                    sheen = sheen.WithAlpha(26)

                Case MASInteractionState.Selected
                    top = ColorMath.Mix(top, light, 0.05F)
                    bottom = ColorMath.Mix(bottom, danger, 0.18F)
                    outer = ColorMath.Mix(outer, danger, 0.32F).WithAlpha(235)
                    inner = ColorMath.Mix(inner, light, 0.16F).WithAlpha(185)
                    highlight = highlight.WithAlpha(132)
                    wash = ColorMath.Mix(wash, danger, 0.26F).WithAlpha(62)
                    glow = ColorMath.Mix(glow, light, 0.18F).WithAlpha(52)
                    sheen = sheen.WithAlpha(72)

                Case MASInteractionState.Focused
                    top = ColorMath.Mix(top, light, 0.04F)
                    bottom = ColorMath.Mix(bottom, danger, 0.04F)
                    outer = ColorMath.Mix(outer, MASAppThemeContracts.Foundation(app).Semantics.FocusStrong, 0.14F).WithAlpha(205)
                    inner = ColorMath.Mix(inner, light, 0.08F).WithAlpha(150)
                    highlight = highlight.WithAlpha(100)
                    glow = ColorMath.Mix(glow, MASAppThemeContracts.Foundation(app).Semantics.FocusStrong, 0.08F).WithAlpha(34)
                    sheen = sheen.WithAlpha(34)

                Case MASInteractionState.Disabled
                    Return BuildDisabledDangerSurface(surface, app)

            End Select

            Return New MASButtonSurfaceTheme(
                fillTop:=top,
                fillBottom:=bottom,
                borderOuter:=outer,
                borderInner:=inner,
                innerHighlight:=highlight,
                innerShade:=shade,
                materialWash:=wash,
                bodyGlow:=glow,
                topSheen:=sheen)

        End Function




        Private Shared Function BuildDisabledDangerSurface(surface As MASButtonSurfaceTheme,
                                                           app As IMASAppTheme) As MASButtonSurfaceTheme

            If MASAppThemeContracts.Foundation(app).Neutrals Is Nothing OrElse MASAppThemeContracts.Foundation(app).Semantics Is Nothing Then
                Return surface
            End If

            Dim neutral As SKColor = MASAppThemeContracts.Foundation(app).Neutrals.Surface2
            Dim disabled As SKColor = MASAppThemeContracts.Foundation(app).Semantics.Disabled

            Return New MASButtonSurfaceTheme(
                fillTop:=ColorMath.Mix(surface.FillTop, neutral, 0.72F),
                fillBottom:=ColorMath.Mix(surface.FillBottom, neutral, 0.76F),
                borderOuter:=ColorMath.Mix(surface.BorderOuter, disabled, 0.7F).WithAlpha(150),
                borderInner:=ColorMath.Mix(surface.BorderInner, neutral, 0.72F).WithAlpha(120),
                innerHighlight:=surface.InnerHighlight.WithAlpha(40),
                innerShade:=surface.InnerShade.WithAlpha(28),
                materialWash:=surface.MaterialWash.WithAlpha(18),
                bodyGlow:=surface.BodyGlow.WithAlpha(10),
                topSheen:=surface.TopSheen.WithAlpha(16))

        End Function

    End Class

End Namespace
