Option Strict On
Option Explicit On

Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports Nexamas.UI.Composition
Imports SkiaSharp

Namespace Nexamas.UI.Controls

    ''' <summary>
    ''' Public standalone title-and-divider control. It exposes only title text and divider visibility;
    ''' the decorative text embossing and separator rendering stay inside Nexamas.UI.Rendering.
    ''' </summary>
    Public NotInheritable Class MASTitle
        Inherits MASControlBase
        Implements IMASLayoutParticipant
        Implements IMASIntrinsicSizeContract

        Private ReadOnly _renderer As New TitleSeparator()
        Private _text As String = String.Empty
        Private _drawDivider As Boolean = True
        Private _disposed As Boolean

        Public Sub New()
            Me.Name = "MASTitle"
        End Sub

        Public Sub New(text As String)
            Me.New()
            _text = MASTitleTypographyPolicy.NormalizeTitleText(text)
        End Sub

        Public Shared Function Create(text As String) As MASTitle
            Return New MASTitle(text)
        End Function

        Public Property Text As String
            Get
                Return _text
            End Get
            Set(value As String)
                Dim nextText As String = MASTitleTypographyPolicy.NormalizeTitleText(value)
                If String.Equals(_text, nextText, StringComparison.Ordinal) Then Return

                _text = nextText
                InvalidateVisual()
            End Set
        End Property

        Public Property DrawDivider As Boolean
            Get
                Return _drawDivider
            End Get
            Set(value As Boolean)
                If _drawDivider = value Then Return
                _drawDivider = value
                InvalidateVisual()
            End Set
        End Property

        Friend Function SetBounds(bounds As SKRect) As MASTitle
            Me.SetLocalLayoutBoundsInternal(bounds)
            Return Me
        End Function

        Friend Function SetBounds(x As Single,
                                  y As Single,
                                  width As Single,
                                  height As Single) As MASTitle

            Me.SetLocalLayoutBoundsInternal(New SKRect(x, y, x + width, y + height))
            Return Me
        End Function

        Friend Function WithBounds(bounds As SKRect) As MASTitle
            Return SetBounds(bounds)
        End Function

        Friend Function WithBounds(x As Single,
                                   y As Single,
                                   width As Single,
                                   height As Single) As MASTitle

            Return SetBounds(x, y, width, height)
        End Function

        Public Function WithText(text As String) As MASTitle
            Me.Text = text
            Return Me
        End Function

        Public Function WithDivider(value As Boolean) As MASTitle
            Me.DrawDivider = value
            Return Me
        End Function

        Friend Function GetPreferredLayoutSize(context As MASLayoutMeasureContext) As SKSize Implements IMASLayoutParticipant.GetPreferredLayoutSize
            Return MeasureIntrinsicSize(CreateSizeContext(context), SizeIntent).DesiredSize
        End Function

        Friend Function GetMinLayoutSize(context As MASLayoutMeasureContext) As SKSize Implements IMASLayoutParticipant.GetMinLayoutSize
            Return MeasureIntrinsicSize(CreateSizeContext(context), SizeIntent).MinSize
        End Function

        Friend Function MeasureIntrinsicSize(context As MASSizeContext, intent As MASSize) As MASSizeResult Implements IMASIntrinsicSizeContract.MeasureIntrinsicSize
            Dim safeContext As MASSizeContext = MASIntrinsicControlSizeMetrics.EnsureContext(context)
            Dim textMeasure As MASTextMeasureResult =
                MASLayoutTextMeasureGateway.Measure(
                    safeContext.IntegrationContext,
                    _text,
                    MASTypography.MASTextStyle.Title,
                    Single.PositiveInfinity,
                    allowWrap:=False)

            Dim desired As SKSize = MASIntrinsicControlSizeMetrics.ResolveTitleDesiredSize(textMeasure.Size, _drawDivider)
            Dim minimum As SKSize = MASIntrinsicControlSizeMetrics.ResolveTitleMinimumSize(textMeasure.Size, _drawDivider)

            Return MASSizeResolver.FromMeasured(
                desiredSize:=desired,
                minSize:=minimum,
                maxSize:=New SKSize(Single.PositiveInfinity, Single.PositiveInfinity),
                intent:=intent,
                context:=safeContext,
                canGrowWidth:=True,
                canGrowHeight:=False,
                contentInset:=MASIntrinsicControlSizeMetrics.CreateTitleContentInset(_drawDivider),
                visualOverflowInset:=MASLayoutInset.Empty,
                hitOverflowInset:=MASLayoutInset.Empty,
                isFallback:=False)
        End Function

        Private Shared Function CreateSizeContext(context As MASLayoutMeasureContext) As MASSizeContext
            If context Is Nothing Then Return MASIntrinsicControlSizeMetrics.EnsureContext(Nothing)
            Return context.ToSizeContext()
        End Function

        Protected Overrides Sub Render(canvas As SKCanvas,
                                       ctx As MASThemeContext,
                                       pixelBounds As SKRect)

            If _disposed OrElse canvas Is Nothing OrElse ctx Is Nothing Then Return
            If pixelBounds.IsEmpty OrElse pixelBounds.Width <= 1.0F OrElse pixelBounds.Height <= 1.0F Then Return

            Dim textColor As SKColor = SKColors.Transparent
            Dim dividerColor As SKColor = SKColors.Transparent
            MASTitleTypographyPolicy.ResolveTitleColors(ctx, textColor, dividerColor)

            _renderer.DrawTitleWithSeparator(
                canvas:=canvas,
                ctx:=ctx,
                bounds:=pixelBounds,
                dpi:=ctx.Dpi,
                titleText:=_text,
                textColor:=textColor,
                dividerColor:=dividerColor,
                drawDivider:=_drawDivider)
        End Sub

        Protected Overrides Function HitTest(ctx As MASThemeContext, ptPx As SKPoint) As Boolean
            Return False
        End Function

        Protected Overrides Sub Dispose(disposing As Boolean)
            If Not _disposed Then
                _disposed = True

                If disposing Then
                    Try
                        _renderer.Dispose()
                    Catch masCaughtException1 As Exception
                        Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDisposeCleanup(masCaughtException1)
                    End Try
                End If
            End If

            MyBase.Dispose(disposing)
        End Sub


#Region "Unified MASSize Fluent API"

        ''' <summary>
        ''' Strongly typed entry into the shared MASSize intent API.
        ''' This method keeps fluent chains on the concrete element while delegating all sizing decisions to MASControlBase/MASSize.
        ''' </summary>
        Public Shadows Function WithSize(sizeIntent As Nexamas.UI.Layout.MASSize) As MASTitle
            MyBase.SetSize(sizeIntent)
            Return Me
        End Function






#End Region
    End Class

End Namespace
