Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Resolves button colors from the active media visual style and interaction state.
    ''' </summary>
    Friend NotInheritable Class MASMediaButtonPaletteResolver

        Private Sub New()
        End Sub

        Friend Shared Function Resolve(style As MASMediaVisualStyle,
                                       state As MASMediaButtonState) As MASMediaButtonPalette
            If style Is Nothing Then Return New MASMediaButtonPalette()

            Dim toolbar As MASMediaRegionTheme = style.Toolbar
            Dim control As MASMediaControlTheme = style.Control
            Dim text As MASMediaTextTheme = style.Text
            Dim accent As SKColor = toolbar.Accent

            Dim top As SKColor = ColorMath.Mix(toolbar.SurfaceTop, control.Fill, 0.36F)
            Dim bottom As SKColor = ColorMath.Mix(toolbar.SurfaceMid, control.Fill, 0.30F)
            Dim stroke As SKColor = ColorMath.Mix(toolbar.BorderOuter, accent, 0.16F)
            Dim inner As SKColor = ColorMath.Mix(toolbar.BorderInner, toolbar.Sheen, 0.48F)
            Dim icon As SKColor = ColorMath.Mix(text.Secondary, accent, 0.42F)
            Dim label As SKColor = ColorMath.Mix(text.Primary, text.Secondary, 0.30F)

            Dim fillTopAlpha As Byte = 226
            Dim fillBottomAlpha As Byte = 206
            Dim strokeAlpha As Byte = 96
            Dim innerAlpha As Byte = 54
            Dim shadowAlpha As Byte = 10
            Dim iconAlpha As Byte = 208
            Dim labelAlpha As Byte = 214

            If state.Selected Then
                top = ColorMath.Mix(top, accent, 0.035F)
                bottom = ColorMath.Mix(bottom, accent, 0.045F)
                stroke = ColorMath.Mix(stroke, accent, 0.18F)
                fillTopAlpha = 236
                fillBottomAlpha = 216
                strokeAlpha = 116
            End If

            If state.Hovered Then
                top = ColorMath.Mix(top, accent, 0.05F)
                bottom = ColorMath.Mix(bottom, accent, 0.06F)
                stroke = ColorMath.Mix(stroke, accent, 0.24F)
                icon = ColorMath.Mix(icon, accent, 0.08F)
                fillTopAlpha = 244
                fillBottomAlpha = 226
                strokeAlpha = 126
                innerAlpha = 68
                shadowAlpha = 12
                iconAlpha = 226
                labelAlpha = 226
            End If

            If state.Pressed Then
                top = ColorMath.Mix(toolbar.SurfaceMid, accent, 0.04F)
                bottom = ColorMath.Mix(toolbar.SurfaceBottom, accent, 0.06F)
                stroke = ColorMath.Mix(stroke, accent, 0.26F)
                fillTopAlpha = 204
                fillBottomAlpha = 188
                strokeAlpha = 104
                innerAlpha = 48
                shadowAlpha = 0
            End If

            If Not state.Enabled Then
                top = ColorMath.Mix(toolbar.SurfaceTop, toolbar.SurfaceMid, 0.55F)
                bottom = ColorMath.Mix(toolbar.SurfaceMid, toolbar.SurfaceBottom, 0.45F)
                stroke = toolbar.BorderOuter
                inner = toolbar.BorderInner
                icon = ColorMath.Mix(text.Disabled, accent, 0.16F)
                label = text.Disabled
                fillTopAlpha = 148
                fillBottomAlpha = 132
                strokeAlpha = 54
                innerAlpha = 28
                shadowAlpha = 0
                iconAlpha = 146
                labelAlpha = 150
            End If

            Return New MASMediaButtonPalette With {
                .TopFill = ColorMath.WithAlpha(top, fillTopAlpha),
                .BottomFill = ColorMath.WithAlpha(bottom, fillBottomAlpha),
                .Stroke = ColorMath.WithAlpha(stroke, strokeAlpha),
                .InnerStroke = ColorMath.WithAlpha(inner, innerAlpha),
                .Highlight = ColorMath.WithAlpha(toolbar.Sheen, If(state.Enabled, CByte(62), CByte(24))),
                .Shadow = ColorMath.WithAlpha(toolbar.Shadow, shadowAlpha),
                .BottomShade = ColorMath.WithAlpha(toolbar.Shadow, If(state.Enabled, CByte(12), CByte(0))),
                .SideRim = ColorMath.WithAlpha(toolbar.Sheen, If(state.Enabled, CByte(20), CByte(8))),
                .IconShadow = ColorMath.WithAlpha(toolbar.Shadow, CByte(0)),
                .LabelShadow = ColorMath.WithAlpha(toolbar.Shadow, CByte(0)),
                .Icon = ColorMath.WithAlpha(icon, iconAlpha),
                .Label = ColorMath.WithAlpha(label, labelAlpha)
            }
        End Function

    End Class

End Namespace
