Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Architecture
Imports Nexamas.UI.Controls
Imports Nexamas.UI.General
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.TextRendering
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components


    Partial Friend NotInheritable Class MASTabRenderer
        Private Sub DrawViewportEdgeFade(canvas As SKCanvas,
                                         lay As MASTabLayout.Metrics,
                                         env As MASTabRenderEnv)

            If lay.ViewportR.Width <= 1.0F OrElse lay.ViewportR.Height <= 1.0F Then Return

            Dim fadeW As Single = MASTabPremiumVisualPolicy.ResolveViewportFadeWidth(lay.ViewportR.Width, env.Dpi)
            If fadeW <= 2.0F Then Return

            Dim topCol As SKColor = ColorMath.Mix(env.Tabs.Strip.FillTop, env.Surface.SurfaceTop, 0.16F)
            Dim solidCol As SKColor = WithAlpha(topCol, 90)
            Dim clearCol As SKColor = WithAlpha(topCol, 0)

            If lay.ShowLeftNav Then
                Dim r As New SKRect(
                    lay.ViewportR.Left + 1.0F,
                    lay.ViewportR.Top + 1.0F,
                    lay.ViewportR.Left + fadeW,
                    lay.ViewportR.Bottom - 1.0F
                )

                Using fadeShader As SKShader =
                    SKShader.CreateLinearGradient(
                        New SKPoint(r.Left, r.MidY),
                        New SKPoint(r.Right, r.MidY),
                        New SKColor() {solidCol, clearCol},
                        New Single() {0.0F, 1.0F},
                        SKShaderTileMode.Clamp)

                    ResetEdgeFadePaint()
                    _edgeFadePaint.Shader = fadeShader
                    canvas.DrawRect(r, _edgeFadePaint)
                    _edgeFadePaint.Shader = Nothing
                End Using
            End If

            If lay.ShowRightNav Then
                Dim r As New SKRect(
                    lay.ViewportR.Right - fadeW,
                    lay.ViewportR.Top + 1.0F,
                    lay.ViewportR.Right - 1.0F,
                    lay.ViewportR.Bottom - 1.0F
                )

                Using fadeShader As SKShader =
                    SKShader.CreateLinearGradient(
                        New SKPoint(r.Left, r.MidY),
                        New SKPoint(r.Right, r.MidY),
                        New SKColor() {clearCol, solidCol},
                        New Single() {0.0F, 1.0F},
                        SKShaderTileMode.Clamp)

                    ResetEdgeFadePaint()
                    _edgeFadePaint.Shader = fadeShader
                    canvas.DrawRect(r, _edgeFadePaint)
                    _edgeFadePaint.Shader = Nothing
                End Using
            End If
        End Sub




        Private Sub DrawViewportPlate(canvas As SKCanvas,
                              lay As MASTabLayout.Metrics,
                              contract As MASResolvedComponentContract,
                              env As MASTabRenderEnv)

            If lay.ViewportR.Width <= 1.0F OrElse lay.ViewportR.Height <= 1.0F Then Return

            Dim r As SKRect = lay.ViewportR
            Dim plateInset As Single = MASTabPremiumVisualPolicy.ResolveViewportPlateInset(env.Dpi)
            r.Inflate(-plateInset, -plateInset)
            r = PixelSnap.SnapRectHalf(r)

            Dim radiusPx As Single =
    PixelSnap.SafeRadius(
        MASRadiusResolver.ResolveSurfacePx(
            contract:=contract,
            rPx:=r,
            dpi:=env.Dpi) * MASTabPremiumVisualPolicy.ResolveViewportRadiusMultiplier()
    )

            Dim topCol As SKColor = ColorMath.Mix(env.Tabs.Strip.FillTop, env.Surface.SurfaceTop, 0.12F)
            Dim botCol As SKColor = ColorMath.Mix(env.Tabs.Strip.FillBottom, env.Surface.SurfaceMid, 0.1F)

            Dim chipSpec As New MASVisualPrimitivesPainter.ChipBaseSpec With {
                .Top = topCol,
                .Bot = botCol,
                .MidT = 0.555F,
                .TopAlpha = 255,
                .MidAlpha = 255,
                .BotAlpha = 255
            }

            Dim fxSpec As New MASVisualPrimitivesPainter.ChipFxSpec With {
                .WashColor = ColorMath.Mix(env.Tabs.Strip.WashColor, env.Foundation.Brand.BrandHighlight, 0.04F),
                .WashAlpha = 20,
                .InnerGlossColor = SKColors.White,
                .InnerGlossAlpha = 16,
                .InnerGlossFadeT = 0.26F,
                .InnerInsetMul = 0.03F,
                .InnerRadMul = 0.98F
            }

            _p.DrawChipBaseNoShadow(
                canvas:=canvas,
                ctx:=env.ThemeContext,
                r:=r,
                radius:=radiusPx,
                dpi:=env.Dpi,
                spec:=chipSpec
            )

            _p.DrawChipFxSSOT(
                canvas:=canvas,
                r:=r,
                radius:=radiusPx,
                dpi:=env.Dpi,
                spec:=chipSpec,
                fxSpec:=fxSpec
            )

            MASSurfaceInteractionRing.DrawIdleRim(
                canvas:=canvas,
                baseR:=r,
                baseRadiusPx:=radiusPx,
                rimColor:=ColorMath.Mix(env.Tabs.Strip.RimColor, env.Tabs.Item.BaseRim, 0.18F),
                rimAlpha:=34,
                strokePx:=Math.Max(1.0F, lay.HairW),
                dpi:=env.Dpi,
                edgeInsetPx:=0.0F
            )
        End Sub




        Private Sub DrawNavBaySeams(canvas As SKCanvas,
                                    lay As MASTabLayout.Metrics,
                                    env As MASTabRenderEnv)

            If Not lay.HasOverflow Then Return

            ' Premium tabs avoid hard divider seams at the rail ends. These very
            ' soft boundary hints keep the rail readable without chopping the strip
            ' into visibly separate boxes.
            Dim seamAlpha As Byte = 6
            Dim seamCol As SKColor = ColorMath.Mix(env.Tabs.Strip.RimColor, env.Tabs.Item.BaseRim, 0.18F)

            ResetSeamPaint()
            _seamPaint.Color = New SKColor(seamCol.Red, seamCol.Green, seamCol.Blue, seamAlpha)
            _seamPaint.StrokeWidth = Math.Max(1.0F, lay.HairW)

            Dim topY As Single = lay.ViewportR.Top + Math.Max(7.0F, 7.0F * env.Dpi)
            Dim botY As Single = lay.ViewportR.Bottom - Math.Max(7.0F, 7.0F * env.Dpi)

            If Not lay.LeftNavR.IsEmpty Then
                Dim x As Single = lay.ViewportR.Left - Math.Max(1.0F, 0.5F * env.Dpi)
                canvas.DrawLine(x, topY, x, botY, _seamPaint)
            End If

            If Not lay.RightNavR.IsEmpty Then
                Dim x As Single = lay.ViewportR.Right + Math.Max(1.0F, 0.5F * env.Dpi)
                canvas.DrawLine(x, topY, x, botY, _seamPaint)
            End If
        End Sub

    End Class

End Namespace
