Option Strict On
Option Explicit On

Imports Nexamas.UI.Icons
Imports SkiaSharp

Namespace Nexamas.UI.Rendering

    ''' <summary>
    ''' Internal immutable header contract for PanelShell chrome.
    ''' Public applications select intent through Application-level gateways; this object
    ''' stays inside Nexamas UI so renderer geometry and hit-state details do not leak.
    ''' </summary>
    Friend NotInheritable Class MASPanelShellHeaderSpec

        Private Shared ReadOnly _none As MASPanelShellHeaderSpec =
            New MASPanelShellHeaderSpec(
                size:=MASPanelShellHeaderSize.None,
                density:=MASPanelShellDensity.Normal,
                title:=String.Empty,
                subtitle:=String.Empty,
                showIcon:=False,
                iconKind:=MASIconKind.Info,
                iconStyle:=MASIconStyle.Outline,
                iconColorOverride:=Nothing,
                showCloseButton:=False,
                closeHover:=False,
                closePressed:=False,
                drawSeparator:=True,
                drawGloss:=True,
                drawCrown:=True,
                drawSeam:=True,
                drawInnerRim:=True)

        Friend Shared ReadOnly Property None As MASPanelShellHeaderSpec
            Get
                Return _none
            End Get
        End Property

        Friend ReadOnly Property Size As MASPanelShellHeaderSize
        Friend ReadOnly Property Density As MASPanelShellDensity
        Friend ReadOnly Property Title As String
        Friend ReadOnly Property Subtitle As String
        Friend ReadOnly Property ShowIcon As Boolean
        Friend ReadOnly Property IconKind As MASIconKind
        Friend ReadOnly Property IconStyle As MASIconStyle
        Friend ReadOnly Property IconColorOverride As Nullable(Of SKColor)
        Friend ReadOnly Property ShowCloseButton As Boolean
        Friend ReadOnly Property CloseHover As Boolean
        Friend ReadOnly Property ClosePressed As Boolean
        Friend ReadOnly Property DrawSeparator As Boolean
        Friend ReadOnly Property DrawGloss As Boolean
        Friend ReadOnly Property DrawCrown As Boolean
        Friend ReadOnly Property DrawSeam As Boolean
        Friend ReadOnly Property DrawInnerRim As Boolean

        Friend ReadOnly Property Layout As MASPanelShellHeaderLayout
            Get
                Return ResolveLayout()
            End Get
        End Property

        Friend ReadOnly Property HasTitle As Boolean
            Get
                Return Not String.IsNullOrWhiteSpace(Title)
            End Get
        End Property

        Friend ReadOnly Property HasSubtitle As Boolean
            Get
                Return Not String.IsNullOrWhiteSpace(Subtitle)
            End Get
        End Property

        Friend ReadOnly Property IsEnabled As Boolean
            Get
                Return Size <> MASPanelShellHeaderSize.None
            End Get
        End Property

        Friend Sub New(Optional size As MASPanelShellHeaderSize = MASPanelShellHeaderSize.Normal,
                       Optional density As MASPanelShellDensity = MASPanelShellDensity.Normal,
                       Optional title As String = "",
                       Optional subtitle As String = "",
                       Optional showIcon As Boolean = False,
                       Optional iconKind As MASIconKind = MASIconKind.Info,
                       Optional iconStyle As MASIconStyle = MASIconStyle.Outline,
                       Optional iconColorOverride As Nullable(Of SKColor) = Nothing,
                       Optional showCloseButton As Boolean = False,
                       Optional closeHover As Boolean = False,
                       Optional closePressed As Boolean = False,
                       Optional drawSeparator As Boolean = True,
                       Optional drawGloss As Boolean = True,
                       Optional drawCrown As Boolean = True,
                       Optional drawSeam As Boolean = True,
                       Optional drawInnerRim As Boolean = True)

            Dim safeSize As MASPanelShellHeaderSize =
                MASPanelShellValueNormalizer.NormalizeHeaderSize(size)

            Dim safeDensity As MASPanelShellDensity =
                MASPanelShellValueNormalizer.NormalizeDensity(density)

            Me.Size = safeSize
            Me.Density = safeDensity
            Me.Title = If(title, String.Empty).Trim()
            Me.Subtitle = If(subtitle, String.Empty).Trim()
            Me.ShowIcon = showIcon AndAlso safeSize <> MASPanelShellHeaderSize.None
            Me.IconKind = iconKind
            Me.IconStyle = iconStyle
            Me.IconColorOverride = iconColorOverride
            Me.ShowCloseButton = showCloseButton AndAlso safeSize <> MASPanelShellHeaderSize.None
            Me.CloseHover = closeHover
            Me.ClosePressed = closePressed
            Me.DrawSeparator = drawSeparator
            Me.DrawGloss = drawGloss
            Me.DrawCrown = drawCrown
            Me.DrawSeam = drawSeam
            Me.DrawInnerRim = drawInnerRim
        End Sub

        Friend Shared Function Create(title As String,
                                      Optional subtitle As String = "",
                                      Optional iconKind As MASIconKind = MASIconKind.Info,
                                      Optional showIcon As Boolean = False,
                                      Optional showCloseButton As Boolean = False,
                                      Optional size As MASPanelShellHeaderSize = MASPanelShellHeaderSize.Normal,
                                      Optional density As MASPanelShellDensity = MASPanelShellDensity.Normal) As MASPanelShellHeaderSpec

            Return New MASPanelShellHeaderSpec(
                size:=size,
                density:=density,
                title:=title,
                subtitle:=subtitle,
                showIcon:=showIcon,
                iconKind:=iconKind,
                showCloseButton:=showCloseButton)
        End Function

        Friend Function WithDensity(value As MASPanelShellDensity) As MASPanelShellHeaderSpec
            Return Copy(density:=MASPanelShellValueNormalizer.NormalizeDensity(value))
        End Function

        Friend Function WithSize(value As MASPanelShellHeaderSize) As MASPanelShellHeaderSpec
            Return Copy(size:=MASPanelShellValueNormalizer.NormalizeHeaderSize(value))
        End Function

        Friend Function WithCloseState(isHover As Boolean,
                                       isPressed As Boolean) As MASPanelShellHeaderSpec
            Return Copy(closeHover:=isHover, closePressed:=isPressed)
        End Function

        Friend Function Copy(Optional size As MASPanelShellHeaderSize? = Nothing,
                             Optional density As MASPanelShellDensity? = Nothing,
                             Optional title As String = Nothing,
                             Optional subtitle As String = Nothing,
                             Optional showIcon As Boolean? = Nothing,
                             Optional iconKind As MASIconKind? = Nothing,
                             Optional iconStyle As MASIconStyle? = Nothing,
                             Optional iconColorOverride As Nullable(Of SKColor) = Nothing,
                             Optional iconColorOverrideSet As Boolean = False,
                             Optional showCloseButton As Boolean? = Nothing,
                             Optional closeHover As Boolean? = Nothing,
                             Optional closePressed As Boolean? = Nothing,
                             Optional drawSeparator As Boolean? = Nothing,
                             Optional drawGloss As Boolean? = Nothing,
                             Optional drawCrown As Boolean? = Nothing,
                             Optional drawSeam As Boolean? = Nothing,
                             Optional drawInnerRim As Boolean? = Nothing) As MASPanelShellHeaderSpec

            Return New MASPanelShellHeaderSpec(
                size:=If(size.HasValue, size.Value, Me.Size),
                density:=If(density.HasValue, density.Value, Me.Density),
                title:=If(title IsNot Nothing, title, Me.Title),
                subtitle:=If(subtitle IsNot Nothing, subtitle, Me.Subtitle),
                showIcon:=If(showIcon.HasValue, showIcon.Value, Me.ShowIcon),
                iconKind:=If(iconKind.HasValue, iconKind.Value, Me.IconKind),
                iconStyle:=If(iconStyle.HasValue, iconStyle.Value, Me.IconStyle),
                iconColorOverride:=If(iconColorOverrideSet, iconColorOverride, Me.IconColorOverride),
                showCloseButton:=If(showCloseButton.HasValue, showCloseButton.Value, Me.ShowCloseButton),
                closeHover:=If(closeHover.HasValue, closeHover.Value, Me.CloseHover),
                closePressed:=If(closePressed.HasValue, closePressed.Value, Me.ClosePressed),
                drawSeparator:=If(drawSeparator.HasValue, drawSeparator.Value, Me.DrawSeparator),
                drawGloss:=If(drawGloss.HasValue, drawGloss.Value, Me.DrawGloss),
                drawCrown:=If(drawCrown.HasValue, drawCrown.Value, Me.DrawCrown),
                drawSeam:=If(drawSeam.HasValue, drawSeam.Value, Me.DrawSeam),
                drawInnerRim:=If(drawInnerRim.HasValue, drawInnerRim.Value, Me.DrawInnerRim))
        End Function

        Private Function ResolveLayout() As MASPanelShellHeaderLayout
            If Not IsEnabled Then Return MASPanelShellHeaderLayout.None

            If ShowIcon AndAlso HasSubtitle Then Return MASPanelShellHeaderLayout.IconTitleSubtitle
            If ShowIcon Then Return MASPanelShellHeaderLayout.IconTitle
            If HasSubtitle Then Return MASPanelShellHeaderLayout.TitleSubtitle
            If HasTitle Then Return MASPanelShellHeaderLayout.TitleOnly

            Return MASPanelShellHeaderLayout.TitleOnly
        End Function

    End Class

End Namespace
