Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Controls
Imports SkiaSharp

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Internal content insertion host for reusable PanelShell floats.
    ''' Nexamas UI owns shell chrome, official sections, footer, clipping, and scrolling;
    ''' high-level public gateways decide which controls are inserted through this host.
    ''' </summary>
    Friend NotInheritable Class MASPanelShellContentHost

        Private ReadOnly _owner As MASPanelShellFloatHost
        Private ReadOnly _sectionBoundsLogical As SKRect
        Private ReadOnly _hasSectionBounds As Boolean

        Friend Sub New(owner As MASPanelShellFloatHost)
            Me.New(owner, SKRect.Empty)
        End Sub

        Friend Sub New(owner As MASPanelShellFloatHost,
                       relativeSectionBoundsLogical As SKRect)

            If owner Is Nothing Then Throw New ArgumentNullException(NameOf(owner))

            _owner = owner
            _sectionBoundsLogical = relativeSectionBoundsLogical
            _hasSectionBounds = IsUsableBounds(relativeSectionBoundsLogical)

        End Sub

        ''' <summary>
        ''' Logical bounds of the official content section relative to the PanelShell content root.
        ''' Most callers can ignore this and use the contentBounds argument supplied to ContentBuilder.
        ''' </summary>
        Friend ReadOnly Property SectionBoundsLogical As SKRect
            Get
                Return _sectionBoundsLogical
            End Get
        End Property

        Friend Sub Add(child As MASControlBase,
                       relativeContentBoundsLogical As SKRect)

            AddScrollable(child, relativeContentBoundsLogical)

        End Sub

        Friend Sub AddFixed(child As MASControlBase,
                            relativeContentBoundsLogical As SKRect)

            _owner.AddFixedContent(child, TranslateFromSection(relativeContentBoundsLogical))

        End Sub

        Friend Sub AddScrollable(child As MASControlBase,
                                 relativeScrollableBoundsLogical As SKRect)

            _owner.AddScrollableContent(child, relativeScrollableBoundsLogical)

        End Sub

        Friend Function SetScrollableArea(relativeContentBoundsLogical As SKRect) As SKRect

            Dim effective As SKRect = _owner.SetScrollableArea(TranslateFromSection(relativeContentBoundsLogical))
            Return TranslateToSection(effective)

        End Function

        Friend Sub Remove(child As MASControlBase)
            _owner.RemoveContent(child)
        End Sub

        Friend Sub Clear()
            _owner.ClearContent()
        End Sub

        Private Function TranslateFromSection(relativeBoundsLogical As SKRect) As SKRect

            If Not _hasSectionBounds Then Return relativeBoundsLogical

            Return New SKRect(
                _sectionBoundsLogical.Left + relativeBoundsLogical.Left,
                _sectionBoundsLogical.Top + relativeBoundsLogical.Top,
                _sectionBoundsLogical.Left + relativeBoundsLogical.Right,
                _sectionBoundsLogical.Top + relativeBoundsLogical.Bottom)

        End Function

        Private Function TranslateToSection(relativeBoundsLogical As SKRect) As SKRect

            If Not _hasSectionBounds Then Return relativeBoundsLogical

            Return New SKRect(
                relativeBoundsLogical.Left - _sectionBoundsLogical.Left,
                relativeBoundsLogical.Top - _sectionBoundsLogical.Top,
                relativeBoundsLogical.Right - _sectionBoundsLogical.Left,
                relativeBoundsLogical.Bottom - _sectionBoundsLogical.Top)

        End Function

        Private Shared Function IsUsableBounds(bounds As SKRect) As Boolean

            Return Not bounds.IsEmpty AndAlso
                   bounds.Width > 0.5F AndAlso
                   bounds.Height > 0.5F

        End Function

    End Class

End Namespace
