Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Icons
Imports Nexamas.UI.Theming
Imports SkiaSharp

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Single reusable MASPanelShell dialog host recipe.
    ''' It builds, measures, and draws the common shell used by dialog-like components.
    ''' The hosted content remains owned by the feature; the shell/top-bar contract stays here.
    ''' </summary>
    Friend NotInheritable Class MASPanelShellDialogHost

        Private Sub New()
        End Sub

        Friend Shared Function CreateSpec(boundsPx As SKRect,
                                          options As MASPanelShellDialogOptions,
                                          Optional closeHover As Boolean = False,
                                          Optional closePressed As Boolean = False) As MASPanelShellSpec

            Dim opts As MASPanelShellDialogOptions = MASPanelShellDialogOptions.SafeClone(options)

            Dim header As MASPanelShellHeaderSpec =
                MASPanelShellHeaderSpec.Create(
                    title:=If(opts.Title, String.Empty),
                    subtitle:=If(opts.Subtitle, String.Empty),
                    iconKind:=opts.IconKind,
                    showIcon:=opts.ShowIcon,
                    showCloseButton:=opts.ShowCloseButton,
                    size:=opts.HeaderSize,
                    density:=opts.Density).
                Copy(
                    closeHover:=closeHover,
                    closePressed:=closePressed,
                    iconStyle:=opts.HeaderIconStyle,
                    drawSeparator:=opts.DrawSeparator,
                    drawGloss:=opts.DrawGloss,
                    drawCrown:=opts.DrawCrown,
                    drawSeam:=opts.DrawSeam,
                    drawInnerRim:=opts.DrawInnerRim)

            Return MASPanelShellSpec.Create(boundsPx).
                WithDensity(opts.Density).
                WithHeader(header).
                WithPadding(
                    leftDip:=opts.PaddingLeftDip,
                    topDip:=opts.PaddingTopDip,
                    rightDip:=opts.PaddingRightDip,
                    bottomDip:=opts.PaddingBottomDip).
                WithStrengths(
                    border:=opts.BorderStrength,
                    surface:=opts.SurfaceStrength).
                WithSurfaceVisualStyleKey(opts.SurfaceVisualStyleKey).
                WithSurfaceMaterialKey(opts.SurfaceMaterialKey).
                Build()

        End Function

        Friend Shared Function Measure(dpi As Single,
                                       boundsPx As SKRect,
                                       options As MASPanelShellDialogOptions,
                                       Optional closeHover As Boolean = False,
                                       Optional closePressed As Boolean = False) As MASPanelShellResult

            Dim spec As MASPanelShellSpec =
                CreateSpec(boundsPx, options, closeHover, closePressed)

            Return MASPanelShellRenderer.Measure(
                dpi:=NormalizeDpi(dpi),
                spec:=spec)

        End Function

        Friend Shared Function Draw(canvas As SKCanvas,
                                    ctx As MASThemeContext,
                                    dpi As Single,
                                    boundsPx As SKRect,
                                    options As MASPanelShellDialogOptions,
                                    Optional closeHover As Boolean = False,
                                    Optional closePressed As Boolean = False) As MASPanelShellResult

            If canvas Is Nothing Then Return MASPanelShellResult.Empty
            If ctx Is Nothing Then Return MASPanelShellResult.Empty

            Dim spec As MASPanelShellSpec =
                CreateSpec(boundsPx, options, closeHover, closePressed)

            Return MASPanelShellRenderer.Draw(
                canvas:=canvas,
                ctx:=ctx,
                dpi:=NormalizeDpi(dpi),
                spec:=spec)

        End Function

        Friend Shared Function IsDragTarget(result As MASPanelShellResult,
                                            options As MASPanelShellDialogOptions,
                                            pointPx As SKPoint) As Boolean

            If result Is Nothing Then Return False
            If options IsNot Nothing AndAlso Not options.AllowHeaderDrag Then Return False

            Return result.HitTest(pointPx) = MASPanelShellHitTarget.Header
        End Function

        Private Shared Function NormalizeDpi(value As Single) As Single
            If value <= 0.0F OrElse Single.IsNaN(value) OrElse Single.IsInfinity(value) Then Return 1.0F
            Return Math.Max(0.75F, value)
        End Function

    End Class

End Namespace
