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 DrawItemText(canvas As SKCanvas,
                                 ctx As MASThemeContext,
                                 lay As MASTabLayout.Metrics,
                                 itemR As SKRect,
                                 text As String,
                                 st As MASVisualInteractionState,
                                 isEnabled As Boolean,
                                 env As MASTabRenderEnv)

            If String.IsNullOrWhiteSpace(text) Then Return

            Dim textR As SKRect = itemR
            textR.Inflate(-lay.TextPadX, 0.0F)
            textR = PixelSnap.SnapRectHalf(textR)

            If textR.Width <= 2.0F OrElse textR.Height <= 2.0F Then Return

            Dim textCol As SKColor = _state.ResolveTextColor(st, isEnabled, env)
            Dim fittedText As String = FitTextToWidth(text, textR.Width, env.Dpi)

            If String.IsNullOrEmpty(fittedText) Then Return

            Dim saveCount As Integer = canvas.Save()
            Try
                canvas.ClipRect(textR)

                TypographyTextPrimitives.DrawSingleLineText(
                    canvas:=canvas,
                    ctx:=ctx,
                    text:=fittedText,
                    bounds:=textR,
                    dpi:=env.Dpi,
                    style:=MASTypography.MASTextStyle.Body,
                    color:=textCol,
                    align:=TypographyTextPrimitives.TextAlign.Center,
                    drawShadow:=False
                )
            Finally
                canvas.RestoreToCount(saveCount)
            End Try
        End Sub



        Private Function MeasureTabTextWidth(text As String,
                                             dpi As Single) As Single

            Dim safeText As String = If(text, String.Empty)
            If safeText.Length = 0 Then Return 0.0F

            Dim textSize As Single = Math.Max(11.0F, 13.0F * PixelSnap.SafeDpi(dpi))
            Using paint As New SKPaint With {.Typeface = SKTypeface.Default, .TextSize = textSize, .IsAntialias = True}
                Return CSng(Math.Ceiling(MASTextLayoutMeasurementGateway.MeasureShapedWidth(safeText, paint)))
            End Using
        End Function




        Private Function FitTextToWidth(text As String,
                                        maxWidth As Single,
                                        dpi As Single) As String

            Dim safeText As String = If(text, String.Empty)
            If safeText.Length = 0 Then Return String.Empty
            If maxWidth <= 4.0F Then Return String.Empty

            Dim textSize As Single = Math.Max(11.0F, 13.0F * PixelSnap.SafeDpi(dpi))
            Using paint As New SKPaint With {.Typeface = SKTypeface.Default, .TextSize = textSize, .IsAntialias = True}
                Dim fitted As String = MASTextLayoutMeasurementGateway.FitEllipsisShaped(safeText, maxWidth, paint)
                If String.IsNullOrEmpty(fitted) Then Return String.Empty
                If MASTextLayoutMeasurementGateway.MeasureShapedWidth(fitted, paint) > maxWidth + 0.5F Then Return String.Empty
                Return fitted
            End Using
        End Function

    End Class

End Namespace
