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

    ''' <summary>
    ''' Paints the segmented control with component-specific material identity.
    ''' Contracts and interaction resolver are used for state legality and semantic overlays only;
    ''' the selected indicator, track lighting, rim, and label hierarchy remain local visual DNA.
    ''' </summary>
    Partial Friend NotInheritable Class SegmentedControlPainter
        Implements IDisposable

        Friend NotInheritable Class Style
            Friend TextStyleIdle As MASTypography.MASTextStyle = MASTypography.MASTextStyle.Body
            Friend TextStyleSelected As MASTypography.MASTextStyle = MASTypography.MASTextStyle.Body
            Friend LabelScale As Single = SegmentedControlTokens.LabelScale
            Friend LabelBaselineNudgeDip As Single = SegmentedControlTokens.LabelBaselineNudge
        End Class

        Private ReadOnly _p As MASVisualPrimitivesPainter
        Private ReadOnly _shadow As MASShadowPainter

        Private _disposed As Boolean

        Private ReadOnly _indicatorFillPaint As New SKPaint With {
            .IsAntialias = True,
            .IsDither = True,
            .Style = SKPaintStyle.Fill
        }

        Private ReadOnly _indicatorFxPaint As New SKPaint With {
            .IsAntialias = True,
            .IsDither = True,
            .Style = SKPaintStyle.Fill
        }

        Private ReadOnly _indicatorStrokePaint As New SKPaint With {
            .IsAntialias = True,
            .IsDither = True,
            .Style = SKPaintStyle.Stroke,
            .StrokeJoin = SKStrokeJoin.Round,
            .StrokeCap = SKStrokeCap.Round
        }

        Private Const PillMidT As Single = SegmentedControlTokens.PillMidT
        Private Const IndicatorMidT As Single = SegmentedControlTokens.IndicatorMidT

        Friend Sub New(primitives As MASVisualPrimitivesPainter, shadow As MASShadowPainter)
            If primitives Is Nothing Then Throw New ArgumentNullException(NameOf(primitives))
            If shadow Is Nothing Then Throw New ArgumentNullException(NameOf(shadow))

            _p = primitives
            _shadow = shadow
        End Sub

        Public Sub Dispose() Implements IDisposable.Dispose
            If _disposed Then Return
            _disposed = True

            SafeDispose(_indicatorFillPaint)
            SafeDispose(_indicatorFxPaint)
            SafeDispose(_indicatorStrokePaint)
        End Sub




        Private Shared Function BuildPerCornerRoundRect(r As SKRect,
                                                        tl As Single,
                                                        tr As Single,
                                                        br As Single,
                                                        bl As Single) As SKRoundRect

            Dim rr As New SKRoundRect()
            rr.SetRectRadii(
                rect:=r,
                radii:=New SKPoint() {
                    New SKPoint(PixelSnap.SafeRadius(tl), PixelSnap.SafeRadius(tl)),
                    New SKPoint(PixelSnap.SafeRadius(tr), PixelSnap.SafeRadius(tr)),
                    New SKPoint(PixelSnap.SafeRadius(br), PixelSnap.SafeRadius(br)),
                    New SKPoint(PixelSnap.SafeRadius(bl), PixelSnap.SafeRadius(bl))
                }
            )
            Return rr
        End Function

        Private Shared Sub ResolveIndicatorCornerRadii(segmentIndex As Integer,
                                                       segmentCount As Integer,
                                                       radiusPx As Single,
                                                       ByRef tl As Single,
                                                       ByRef tr As Single,
                                                       ByRef br As Single,
                                                       ByRef bl As Single)

            Dim outerR As Single = PixelSnap.SafeRadius(radiusPx)
            Dim innerR As Single = PixelSnap.SafeRadius(Math.Max(outerR * 0.22F, 1.25F))

            tl = innerR
            tr = innerR
            br = innerR
            bl = innerR

            If segmentCount <= 1 Then
                tl = outerR
                tr = outerR
                br = outerR
                bl = outerR
                Return
            End If

            If segmentIndex <= 0 Then
                tl = outerR
                bl = outerR
            End If

            If segmentIndex >= segmentCount - 1 Then
                tr = outerR
                br = outerR
            End If
        End Sub

        Private Shared Function ResolveLabelColor(isSelected As Boolean,
                                                  isHover As Boolean,
                                                  sw As IMASSegmentedControlTheme,
                                                  app As IMASAppTheme) As SKColor

            If isSelected Then
                Return sw.Text.Selected
            End If

            If isHover Then
                Return ColorMath.Mix(sw.Text.Idle, MASAppThemeContracts.Foundation(app).Brand.BrandPrimary, 0.16F)
            End If

            Return sw.Text.Idle
        End Function

        Private Shared Function ForceOpaque(c As SKColor) As SKColor
            Return New SKColor(c.Red, c.Green, c.Blue, 255)
        End Function

        Private Shared Function ResolveBaseAlpha(c As SKColor) As Byte
            If c.Alpha = 0 Then Return 255
            Return c.Alpha
        End Function

        Private Shared Function ResolveMidAlpha(top As SKColor, bot As SKColor) As Byte
            Dim a As Integer = (CInt(ResolveBaseAlpha(top)) + CInt(ResolveBaseAlpha(bot))) \ 2
            a = Math.Max(0, Math.Min(255, a))
            Return CByte(a)
        End Function

    End Class

End Namespace