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
        Private Shared Function BuildIndicatorVisualRect(r As SKRect,
                                                         segmentIndex As Integer,
                                                         segmentCount As Integer,
                                                         dpi As Single,
                                                         hairW As Single,
                                                         rm As MASSurfaceInteractionRing.Metrics) As SKRect

            If r.Width <= 1.0F OrElse r.Height <= 1.0F Then Return SKRect.Empty

            Dim rr As SKRect = r

            Dim insetY As Single = Math.Max(0.75F * dpi, Math.Max(0.6F, hairW))
            Dim insetOuterX As Single = Math.Max(0.55F * dpi, 0.6F)
            Dim insetInnerX As Single = Math.Max(0.85F * dpi, Math.Max(0.8F, hairW + 0.25F))

            rr.Top += insetY
            rr.Bottom -= insetY

            If segmentCount <= 1 Then
                rr.Left += insetOuterX
                rr.Right -= insetOuterX
            ElseIf segmentIndex <= 0 Then
                rr.Left += insetOuterX
                rr.Right -= insetInnerX
            ElseIf segmentIndex >= segmentCount - 1 Then
                rr.Left += insetInnerX
                rr.Right -= insetOuterX
            Else
                rr.Left += insetInnerX
                rr.Right -= insetInnerX
            End If

            rr = MASSurfaceInteractionRing.SnapBaseRect(rr, rm)

            If rr.Width <= 1.0F OrElse rr.Height <= 1.0F Then
                Return SKRect.Empty
            End If

            Return rr
        End Function




        Private Shared Function BuildSegmentedControlPillSpec(sw As IMASSegmentedControlTheme) As MASVisualPrimitivesPainter.ChipBaseSpec
            Return MASVisualPrimitivesPainter.BuildChipBaseSpec(
                top:=ForceOpaque(sw.Track.FillTop),
                bot:=ForceOpaque(sw.Track.FillBottom),
                midT:=PillMidT,
                topA:=ResolveBaseAlpha(sw.Track.FillTop),
                midA:=ResolveMidAlpha(sw.Track.FillTop, sw.Track.FillBottom),
                botA:=ResolveBaseAlpha(sw.Track.FillBottom)
            )
        End Function




        Private Shared Function BuildSegmentedControlIndicatorSpec(sw As IMASSegmentedControlTheme) As MASVisualPrimitivesPainter.ChipBaseSpec
            Return MASVisualPrimitivesPainter.BuildChipBaseSpec(
                top:=ForceOpaque(sw.Indicator.FillTop),
                bot:=ForceOpaque(sw.Indicator.FillBottom),
                midT:=IndicatorMidT,
                topA:=ResolveBaseAlpha(sw.Indicator.FillTop),
                midA:=ResolveMidAlpha(sw.Indicator.FillTop, sw.Indicator.FillBottom),
                botA:=ResolveBaseAlpha(sw.Indicator.FillBottom)
            )
        End Function




        Private Shared Function BuildSegmentedControlPillFxSpec(track As Theming.MASSegmentedControlTrackTheme) As MASVisualPrimitivesPainter.ChipFxSpec
            Return New MASVisualPrimitivesPainter.ChipFxSpec With {
                .WashColor = track.WashColor,
                .WashAlpha = track.WashAlpha,
                .InnerGlossColor = track.InnerGlossColor,
                .InnerGlossAlpha = track.InnerGlossAlpha,
                .InnerGlossFadeT = track.InnerGlossFadeT,
                .InnerInsetMul = track.InnerInsetMul,
                .InnerRadMul = track.InnerRadMul
            }
        End Function




        Private Shared Function BuildSegmentedControlIndicatorFxSpec(app As IMASAppTheme) As MASVisualPrimitivesPainter.ChipFxSpec
            Dim wash As SKColor =
                ColorMath.Mix(MASAppThemeContracts.Foundation(app).Brand.BrandHighlight, MASAppThemeContracts.Foundation(app).Brand.BrandPrimary, 0.18F)

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

            Return New MASVisualPrimitivesPainter.ChipFxSpec With {
                .WashColor = wash,
                .WashAlpha = 30,
                .InnerGlossColor = gloss,
                .InnerGlossAlpha = 26,
                .InnerGlossFadeT = 0.24F,
                .InnerInsetMul = 0.06F,
                .InnerRadMul = 0.86F
            }
        End Function




        Private Shared Sub DrawSegmentedControlPillRim(canvas As SKCanvas,
                                               pillR As SKRect,
                                               radBase As Single,
                                               dpi As Single,
                                               hairW As Single,
                                               sw As IMASSegmentedControlTheme)

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

            Dim rimCol As SKColor = sw.Track.RimColor
            If rimCol.Alpha = 0 Then Return

            Dim outerAlpha As Byte = CByte(Math.Min(255, rimCol.Alpha * 1.08F))
            Dim outerStroke As Single = Math.Max(hairW * 1.06F, 1.0F)

            MASSurfaceInteractionRing.DrawIdleRim(
                canvas:=canvas,
                baseR:=pillR,
                baseRadiusPx:=radBase,
                rimColor:=rimCol,
                rimAlpha:=outerAlpha,
                strokePx:=outerStroke,
                dpi:=dpi,
                edgeInsetPx:=0.0F
            )

            Dim innerCol As SKColor = ColorMath.Mix(rimCol, SKColors.White, 0.18F)
            Dim innerAlpha As Byte = CByte(Math.Min(255, rimCol.Alpha * 0.36F))
            Dim innerStroke As Single = Math.Max(hairW * 0.72F, 1.0F)
            Dim inset As Single = Math.Max(1.0F * dpi, 1.0F)

            MASSurfaceInteractionRing.DrawIdleRim(
                canvas:=canvas,
                baseR:=pillR,
                baseRadiusPx:=radBase,
                rimColor:=innerCol,
                rimAlpha:=innerAlpha,
                strokePx:=innerStroke,
                dpi:=dpi,
                edgeInsetPx:=inset
            )
        End Sub

    End Class

End Namespace
