Option Strict On
Option Explicit On

Imports Nexamas.UI.General
Imports Nexamas.UI.TextRendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Rendering

    Friend NotInheritable Class TitleSeparator
        Implements IDisposable

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

        Private _disposed As Boolean

        '========================================================
        ' Draw Title + Separator
        '========================================================
        Friend Sub DrawTitleWithSeparator(canvas As SKCanvas,
                                          ctx As MASThemeContext,
                                          bounds As SKRect,
                                          dpi As Single,
                                          titleText As String,
                                          textColor As SKColor,
                                          dividerColor As SKColor,
                                          Optional drawDivider As Boolean = True)

            If _disposed Then Return
            If canvas Is Nothing OrElse ctx Is Nothing Then Return
            If bounds.IsEmpty OrElse bounds.Width <= TitleTokens.RendererMinBoundsSizePx OrElse bounds.Height <= TitleTokens.RendererMinBoundsSizePx Then Return

            dpi = PixelSnap.SafeDpi(dpi)

            Dim s As String = MASTitleTypographyPolicy.NormalizeTitleText(titleText)
            If s.Length = 0 Then s = " "

            ' Typography
            Using tp As SKPaint = MASTitleTypographyPolicy.ResolveTitlePaint(ctx, textColor)
                If tp Is Nothing Then Return

                ' Layout
                Dim xPad As Single = Math.Max(TitleTokens.RendererHorizontalPadMinDip * dpi, TitleTokens.RendererHorizontalPadPreferredDip * dpi)
                Dim lineStartX As Single = bounds.Left + xPad

                Dim textShift As Single = TitleTokens.RendererTextShiftDip * dpi
                Dim leftTextInset As Single = xPad + textShift
                Dim rightTextInset As Single = xPad + textShift
                Dim maxTextWidth As Single = Math.Max(1.0F, bounds.Width - leftTextInset - rightTextInset)
                s = MASTitleTypographyPolicy.FitTitleText(s, maxTextWidth, tp)
                If s.Length = 0 Then s = " "

                Dim textWidth As Single = MASShapedText.MeasureWidth(s, tp)
                Dim isRtl As Boolean = MASTitleTypographyPolicy.ResolveTextIsRtl(s)
                Dim tx As Single = MASTitleTypographyPolicy.ResolveTitleTextX(bounds, textWidth, leftTextInset, rightTextInset, isRtl)
                Dim ty As Single = MASTitleTypographyPolicy.ResolveBaselineY(ctx, bounds, tp)

            '========================================================
            ' Micro Emboss (neutral, no theme dependency)
            '========================================================
            Dim embAlpha As Byte = DeriveEmbossAlpha(dividerColor, textColor)

            If embAlpha > 0 Then
                Dim dy As Single = Math.Max(TitleTokens.RendererEmbossOffsetDip * dpi, TitleTokens.RendererEmbossOffsetDip)

                Dim hi As SKColor =
                    ColorMath.WithAlpha(
                        ColorMath.Mix(textColor, SKColors.White, TitleTokens.RendererEmbossHighlightMixT),
                        embAlpha
                    )

                Dim sh As SKColor =
                    ColorMath.WithAlpha(
                        ColorMath.Mix(SKColors.Black, textColor, TitleTokens.RendererEmbossShadowMixT),
                        embAlpha
                    )

                Dim oldCol As SKColor = tp.Color

                tp.Color = hi
                MASShapedText.Draw(canvas, s, tx, ty - dy, tp)

                tp.Color = sh
                MASShapedText.Draw(canvas, s, tx, ty + dy, tp)

                tp.Color = oldCol
            End If

                ' Main text
                MASShapedText.Draw(canvas, s, tx, ty, tp)

                If Not drawDivider Then Return

                '========================================================
                ' Divider
                '========================================================
                Dim strokeW As Single = Math.Max(TitleTokens.DividerStrokePreferredDip * dpi, TitleTokens.DividerStrokeMinPx)

            Dim yBase As Single = ty + Math.Max(TitleTokens.DividerBaselineOffsetDip * dpi, TitleTokens.DividerBaselineOffsetDip)
            yBase = Math.Min(yBase, bounds.Bottom - Math.Max(TitleTokens.DividerBottomInsetDip * dpi, TitleTokens.DividerBottomInsetDip))

            Dim y As Single = CSng(Math.Round(yBase * 2.0F) / 2.0F) + (0.5F * strokeW)

            Dim div As SKColor = dividerColor
            Dim a As Integer = div.Alpha
            If a = 0 Then a = TitleTokens.DividerFallbackAlpha

            a = Math.Max(0, Math.Min(255, a))

            _stroke.StrokeWidth = strokeW

            Dim x1 As Single = lineStartX
            Dim x2 As Single = bounds.Right - xPad

            If x2 <= x1 + (TitleTokens.DividerMinLineWidthDip * dpi) Then Return

            Dim fadePx As Single = Math.Max(TitleTokens.DividerFadeDip * dpi, TitleTokens.DividerFadeDip)
            Dim lineW As Single = (x2 - x1)

            If lineW <= (fadePx * TitleTokens.DividerShortLineThresholdRatio) Then
                fadePx = lineW * TitleTokens.DividerShortLineFadeRatio
            End If

            Dim tFade As Single = CSng(Math.Max(TitleTokens.DividerFadeMinRatio, Math.Min(TitleTokens.DividerFadeMaxRatio, fadePx / Math.Max(TitleTokens.RendererMinBoundsSizePx, lineW))))

            Dim c0 As SKColor = ColorMath.WithAlpha(div, 0)
            Dim c1 As SKColor = ColorMath.WithAlpha(div, CByte(a))

                Using shd As SKShader = SKShader.CreateLinearGradient(
                    New SKPoint(x1, y),
                    New SKPoint(x2, y),
                    New SKColor() {c0, c1, c1, c0},
                    New Single() {0.0F, tFade, 1.0F - tFade, 1.0F},
                    SKShaderTileMode.Clamp
                )
                    _stroke.Shader = shd
                    canvas.DrawLine(x1, y, x2, y, _stroke)
                    _stroke.Shader = Nothing
                End Using
            End Using

        End Sub

        '========================================================
        ' Separator ONLY (بدون عنوان)
        '========================================================
        Friend Sub DrawSeparator(canvas As SKCanvas,
                                 bounds As SKRect,
                                 dpi As Single,
                                 dividerColor As SKColor)

            If _disposed Then Return
            If canvas Is Nothing Then Return
            If bounds.IsEmpty OrElse bounds.Width <= TitleTokens.RendererMinBoundsSizePx Then Return

            dpi = PixelSnap.SafeDpi(dpi)

            Dim strokeW As Single = Math.Max(TitleTokens.DividerStrokePreferredDip * dpi, TitleTokens.DividerStrokeMinPx)
            Dim y As Single = bounds.MidY

            _stroke.StrokeWidth = strokeW
            _stroke.Color = dividerColor

            canvas.DrawLine(bounds.Left, y, bounds.Right, y, _stroke)
        End Sub

        '========================================================
        ' Helpers
        '========================================================
        Private Shared Function DeriveEmbossAlpha(divider As SKColor,
                                                  text As SKColor) As Byte
            Dim a As Integer = divider.Alpha
            If a <= 0 Then a = text.Alpha
            If a <= 0 Then Return 0

            a = CInt(Math.Round(a * TitleTokens.EmbossAlphaScale))
            a = CInt(Math.Max(TitleTokens.EmbossAlphaMin, Math.Min(TitleTokens.EmbossAlphaMax, a)))

            Return CByte(a)
        End Function

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

    End Class

End Namespace