Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports Nexamas.UI.Architecture
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 SegmentedControlPainter
#Region "Indicator material"

        Private Sub DrawIndicatorMaterialPerCorner(canvas As SKCanvas,
                                                          r As SKRect,
                                                          dpi As Single,
                                                          radiusPx As Single,
                                                          segmentIndex As Integer,
                                                          segmentCount As Integer,
                                                          fillTop As SKColor,
                                                          fillBottom As SKColor,
                                                          fxSpec As MASVisualPrimitivesPainter.ChipFxSpec,
                                                          hairW As Single,
                                                          app As IMASAppTheme)

            If canvas Is Nothing Then Return
            If app Is Nothing Then Return
            If r.Width <= 1.0F OrElse r.Height <= 1.0F Then Return

            Dim tl As Single, tr As Single, br As Single, bl As Single
            ResolveIndicatorCornerRadii(
                segmentIndex:=segmentIndex,
                segmentCount:=segmentCount,
                radiusPx:=radiusPx,
                tl:=tl,
                tr:=tr,
                br:=br,
                bl:=bl
            )

            Using rr As SKRoundRect = BuildPerCornerRoundRect(r, tl, tr, br, bl)

                Using sh As SKShader = SKShader.CreateLinearGradient(
                    New SKPoint(r.Left, r.Top),
                    New SKPoint(r.Left, r.Bottom),
                    New SKColor() {
                        ForceOpaque(fillTop).WithAlpha(ResolveBaseAlpha(fillTop)),
                        ForceOpaque(fillBottom).WithAlpha(ResolveBaseAlpha(fillBottom))
                    },
                    New Single() {0.0F, 1.0F},
                    SKShaderTileMode.Clamp)

                    PrepareIndicatorFillPaint()
                    _indicatorFillPaint.Shader = sh
                    canvas.DrawRoundRect(rr, _indicatorFillPaint)
                    _indicatorFillPaint.Shader = Nothing
                End Using

                If fxSpec.WashAlpha > 0 Then
                    PrepareIndicatorFxPaint(SKBlendMode.SrcOver)
                    _indicatorFxPaint.Color = fxSpec.WashColor.WithAlpha(fxSpec.WashAlpha)
                    canvas.DrawRoundRect(rr, _indicatorFxPaint)
                End If

                If fxSpec.InnerGlossAlpha > 0 Then
                    Dim inset As Single = Math.Max(1.0F, r.Height * Math.Max(0.0F, fxSpec.InnerInsetMul))
                    Dim innerR As SKRect = r
                    innerR.Inflate(-inset, -inset)

                    If innerR.Width > 2.0F AndAlso innerR.Height > 2.0F Then
                        Dim tl2 As Single = CornerRadii.ResolveInsetRadiusPx(tl, inset)
                        Dim tr2 As Single = CornerRadii.ResolveInsetRadiusPx(tr, inset)
                        Dim br2 As Single = CornerRadii.ResolveInsetRadiusPx(br, inset)
                        Dim bl2 As Single = CornerRadii.ResolveInsetRadiusPx(bl, inset)

                        Using rrGloss As SKRoundRect = BuildPerCornerRoundRect(innerR, tl2, tr2, br2, bl2)
                            Dim glossFade As Single = Math.Max(0.05F, Math.Min(0.95F, fxSpec.InnerGlossFadeT))
                            Dim glossBottom As Single = innerR.Top + (innerR.Height * glossFade)

                            Using glossShader As SKShader = SKShader.CreateLinearGradient(
                                New SKPoint(innerR.Left, innerR.Top),
                                New SKPoint(innerR.Left, glossBottom),
                                New SKColor() {
                                    fxSpec.InnerGlossColor.WithAlpha(fxSpec.InnerGlossAlpha),
                                    fxSpec.InnerGlossColor.WithAlpha(0)
                                },
                                New Single() {0.0F, 1.0F},
                                SKShaderTileMode.Clamp)

                                PrepareIndicatorFxPaint()
                                _indicatorFxPaint.Shader = glossShader
                                canvas.DrawRoundRect(rrGloss, _indicatorFxPaint)
                                _indicatorFxPaint.Shader = Nothing
                            End Using
                        End Using
                    End If
                End If

                Dim anchorTop As Single = r.Top + (r.Height * 0.46F)

                Using anchorShader As SKShader = SKShader.CreateLinearGradient(
                    New SKPoint(r.Left, anchorTop),
                    New SKPoint(r.Left, r.Bottom),
                    New SKColor() {
                        MASAppThemeContracts.Foundation(app).Surface.ShadowBase.WithAlpha(0),
                        MASAppThemeContracts.Foundation(app).Surface.ShadowBase.WithAlpha(18),
                        MASAppThemeContracts.Foundation(app).Surface.ShadowBase.WithAlpha(34)
                    },
                    New Single() {0.0F, 0.58F, 1.0F},
                    SKShaderTileMode.Clamp)

                    PrepareIndicatorFxPaint(SKBlendMode.Multiply)
                    _indicatorFxPaint.Shader = anchorShader
                    canvas.DrawRoundRect(rr, _indicatorFxPaint)
                    _indicatorFxPaint.Shader = Nothing
                    _indicatorFxPaint.BlendMode = SKBlendMode.SrcOver
                End Using

                Dim glowCx As Single = r.MidX
                Dim glowCy As Single = r.Top + (r.Height * 0.38F)
                Dim glowRadius As Single = Math.Max(r.Width * 0.55F, r.Height * 0.82F)

                Using glowShader As SKShader = SKShader.CreateRadialGradient(
                    New SKPoint(glowCx, glowCy),
                    glowRadius,
                    New SKColor() {
                        ColorMath.Mix(MASAppThemeContracts.Foundation(app).Brand.BrandSilverSoft, SKColors.White, 0.18F).WithAlpha(18),
                        ColorMath.Mix(MASAppThemeContracts.Foundation(app).Brand.BrandHighlight, MASAppThemeContracts.Foundation(app).Surface.MaterialWash, 0.32F).WithAlpha(6),
                        SKColors.Transparent
                    },
                    New Single() {0.0F, 0.58F, 1.0F},
                    SKShaderTileMode.Clamp)

                    PrepareIndicatorFxPaint(SKBlendMode.SrcOver)
                    _indicatorFxPaint.Shader = glowShader
                    canvas.DrawRoundRect(rr, _indicatorFxPaint)
                    _indicatorFxPaint.Shader = Nothing
                End Using

                Dim rim As SKColor =
                    ColorMath.Mix(MASAppThemeContracts.Foundation(app).Brand.BrandHighlight, SKColors.White, 0.14F)

                PrepareIndicatorStrokePaint(
                    strokeWidth:=Math.Max(hairW, SurfaceTokens.FxRingMinStrokePx),
                    color:=rim.WithAlpha(92))
                canvas.DrawRoundRect(rr, _indicatorStrokePaint)

                Dim innerRimCol As SKColor = ColorMath.Mix(rim, SKColors.White, 0.22F)
                Dim innerRimAlpha As Byte = CByte(Math.Min(255, 72))
                Dim innerRimStroke As Single = Math.Max(hairW * 0.75F, 1.0F)

                PrepareIndicatorStrokePaint(
                    strokeWidth:=innerRimStroke,
                    color:=innerRimCol.WithAlpha(innerRimAlpha))
                canvas.DrawRoundRect(rr, _indicatorStrokePaint)
            End Using
        End Sub

#End Region

    End Class

End Namespace
