Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Rendering

    Friend Structure MASReadyFormShellResult
        Friend PanelRect As SKRect
        Friend ContentRect As SKRect
    End Structure

    Friend NotInheritable Class MASReadyFormRenderer

        Private Sub New()
        End Sub

        Friend Shared Function DrawShell(canvas As SKCanvas,
                                         width As Integer,
                                         height As Integer,
                                         ctx As MASThemeContext,
                                         Optional shellTitle As String = "MAS",
                                         Optional dpi As Single = 1.0F) As MASReadyFormShellResult

            If canvas Is Nothing Then Return EmptyResult()
            If ctx Is Nothing OrElse ctx.Theme Is Nothing Then Return EmptyResult()
            If width <= 0 OrElse height <= 0 Then Return EmptyResult()

            dpi = PixelSnap.SafeDpi(dpi)

            canvas.Clear(SKColors.Transparent)

            Dim panelRect As SKRect =
                GetPanelRect(width, height, dpi)

            Dim shellResult As MASPanelShellResult =
                DrawPanelShell(
                    canvas:=canvas,
                    ctx:=ctx,
                    panelRect:=panelRect,
                    shellTitle:=shellTitle,
                    dpi:=dpi)

            Dim contentRect As SKRect =
                If(Not shellResult.ContentRectPx.IsEmpty,
                   shellResult.ContentRectPx,
                   panelRect)

            Return New MASReadyFormShellResult With {
                .PanelRect = panelRect,
                .ContentRect = contentRect
            }

        End Function

        Friend Shared Function MeasureShell(width As Integer,
                                            height As Integer,
                                            Optional dpi As Single = 1.0F) As MASReadyFormShellResult

            If width <= 0 OrElse height <= 0 Then Return EmptyResult()

            dpi = PixelSnap.SafeDpi(dpi)

            Dim panelRect As SKRect =
                GetPanelRect(width, height, dpi)

            Dim contentRect As New SKRect(
                panelRect.Left + (34.0F * dpi),
                panelRect.Top + (72.0F * dpi),
                panelRect.Right - (34.0F * dpi),
                panelRect.Bottom - (24.0F * dpi))

            Return New MASReadyFormShellResult With {
                .PanelRect = panelRect,
                .ContentRect = contentRect
            }

        End Function

        Private Shared Function DrawPanelShell(canvas As SKCanvas,
                                               ctx As MASThemeContext,
                                               panelRect As SKRect,
                                               shellTitle As String,
                                               dpi As Single) As MASPanelShellResult

            Dim safeTitle As String =
                If(String.IsNullOrWhiteSpace(shellTitle),
                   "MAS",
                   shellTitle.Trim())

            Dim header As MASPanelShellHeaderSpec =
                MASPanelShellHeaderSpec.Create(
                    title:=safeTitle,
                    subtitle:=String.Empty,
                    showIcon:=False,
                    showCloseButton:=False,
                    size:=MASPanelShellHeaderSize.Normal,
                    density:=MASPanelShellDensity.Normal
                ).Copy(
                    drawSeparator:=True,
                    drawGloss:=True,
                    drawCrown:=True,
                    drawSeam:=True,
                    drawInnerRim:=True
                )

            Dim spec As MASPanelShellSpec =
                MASPanelShellSpec.Create(panelRect).
                    WithDensity(MASPanelShellDensity.Normal).
                    WithHeader(header).
                    WithSurfaceStrength(MASPanelShellStrength.Level5).
                    WithBorderStrength(MASPanelShellStrength.Level5).
                    WithSurfaceVisualStyleKey(MASSurfaceVisualStyleIds.PlatformDialogFrame).
                    Build()

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

        End Function

        Private Shared Function GetPanelRect(width As Integer,
                                             height As Integer,
                                             dpi As Single) As SKRect

            Dim w As Single = Math.Max(1.0F, CSng(width))
            Dim h As Single = Math.Max(1.0F, CSng(height))

            Dim targetW As Single = Math.Min(w - (48.0F * dpi), 520.0F * dpi)
            Dim targetH As Single = Math.Min(h - (48.0F * dpi), 280.0F * dpi)

            targetW = Math.Max(320.0F * dpi, targetW)
            targetH = Math.Max(180.0F * dpi, targetH)

            targetW = Math.Min(targetW, w - (24.0F * dpi))
            targetH = Math.Min(targetH, h - (24.0F * dpi))

            Dim x As Single = (w - targetW) * 0.5F
            Dim y As Single = (h - targetH) * 0.5F

            Return New SKRect(x, y, x + targetW, y + targetH)

        End Function

        Private Shared Function EmptyResult() As MASReadyFormShellResult
            Return New MASReadyFormShellResult With {
                .PanelRect = SKRect.Empty,
                .ContentRect = SKRect.Empty
            }
        End Function

    End Class

End Namespace