'========================================================
' File: SegmentedControlLayout.vb
' - FIXED: snapping uses SAME SSOT snap-step as MASSurfaceInteractionRing (Tokens.SurfaceFx.SnapStepPx)
'========================================================
Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Components

    Friend NotInheritable Class SegmentedControlLayout

        Friend NotInheritable Class Metrics
            Friend HostR As SKRect = SKRect.Empty
            Friend PillR As SKRect = SKRect.Empty
            Friend Segments() As SKRect = Array.Empty(Of SKRect)()
            Friend IndicatorR As SKRect = SKRect.Empty

            Friend Radius As Single
            Friend InnerRadius As Single
            Friend HeightPx As Single
            Friend HairW As Single
        End Class

        Friend Property InsetXRatio As Single = SegmentedControlTokens.InsetXRatio
        Friend Property InsetYRatio As Single = SegmentedControlTokens.InsetYRatio

        Friend Property HeightRatioToRow As Single = SegmentedControlTokens.HeightRatioToRow
        Friend Property MinHeightDp As Single = SegmentedControlTokens.MinHeight

        Friend Property InnerCornerDp As Single = SegmentedControlTokens.InnerCornerRadius

        Friend Function PreferredHeightPx(baseRowHeightPx As Single, dpi As Single) As Single
            dpi = PixelSnap.SafeDpi(dpi)
            Dim h As Single = baseRowHeightPx * HeightRatioToRow
            Dim minH As Single = MinHeightDp * dpi
            If h < minH Then h = minH
            Return h
        End Function

        Friend Function Compute(hostRect As SKRect, itemsCount As Integer, selectedIndex As Integer, dpi As Single) As Metrics
            Dim m As New Metrics()
            m.HostR = hostRect

            dpi = PixelSnap.SafeDpi(dpi)

            m.HairW = Math.Max(1.0F, 1.0F * dpi)


            If hostRect.IsEmpty OrElse hostRect.Width <= 1 OrElse hostRect.Height <= 1 Then Return m
            If itemsCount < 2 Then Return m

            Dim rm As MASSurfaceInteractionRing.Metrics = MASSurfaceInteractionRing.GetMetrics(dpi)
            Dim snap As Single = rm.SnapStepPx

            Dim h As Single = Math.Max(1.0F, hostRect.Height)
            m.HeightPx = h

            Dim baseLen As Single = Math.Max(1.0F, Math.Min(hostRect.Width, hostRect.Height))

            Dim insetX As Single = baseLen * InsetXRatio
            Dim insetY As Single = baseLen * InsetYRatio

            Dim maxInsetX As Single = (hostRect.Width - 2.0F) * 0.45F
            Dim maxInsetY As Single = (hostRect.Height - 2.0F) * 0.45F
            If insetX > maxInsetX Then insetX = maxInsetX
            If insetY > maxInsetY Then insetY = maxInsetY
            If insetX < 0 Then insetX = 0
            If insetY < 0 Then insetY = 0

            Dim pill As SKRect = SKRect.Create(
                hostRect.Left + insetX,
                hostRect.Top + insetY,
                Math.Max(1.0F, hostRect.Width - insetX * 2.0F),
                Math.Max(1.0F, hostRect.Height - insetY * 2.0F)
            )

            pill = PixelSnap.SnapRect(pill, snap)
            m.PillR = pill

            Dim pillH As Single = m.PillR.Height
            m.Radius = pillH * 0.5F

            Dim innerDp As Single = InnerCornerDp
            If innerDp < 0 Then innerDp = 0
            m.InnerRadius = Math.Max(0.0F, innerDp * dpi)

            Dim n As Integer = itemsCount
            ReDim m.Segments(n - 1)

            Dim w As Single = m.PillR.Width / CSng(n)
            If w <= 0.5F Then Return m

            For i As Integer = 0 To n - 1
                Dim l As Single = m.PillR.Left + (CSng(i) * w)
                Dim rr As Single = If(i = n - 1, m.PillR.Right, l + w)
                m.Segments(i) = PixelSnap.SnapRect(New SKRect(l, m.PillR.Top, rr, m.PillR.Bottom), snap)
            Next

            Dim sel As Integer = Math.Max(0, Math.Min(selectedIndex, n - 1))
            m.IndicatorR = m.Segments(sel)

            Return m
        End Function

    End Class

End Namespace