Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Values
Imports SkiaSharp

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Render-only responsive layout facts for MASAgendaView. The agenda keeps its
    ''' public size contract in MASSizeResolver; this plan owns inner header/body/footer decisions.
    ''' </summary>
    Friend NotInheritable Class MASAgendaViewLayoutPlan
        Friend Shared ReadOnly Thresholds As New MASResponsiveLayoutThresholds(
            fullWidthDip:=AgendaViewTokens.PreferredWidthDip,
            fullHeightDip:=AgendaViewTokens.PreferredHeightDip,
            denseWidthDip:=430.0F,
            denseHeightDip:=320.0F,
            compactWidthDip:=300.0F,
            compactHeightDip:=230.0F,
            minimalWidthDip:=210.0F,
            minimalHeightDip:=150.0F)

        Friend ReadOnly Property ResponsiveProfile As MASResponsiveLayoutProfile
        Friend ReadOnly Property Mode As MASResponsiveLayoutMode
        Friend ReadOnly Property Dpi As Single
        Friend ReadOnly Property PaddingPx As Single
        Friend ReadOnly Property HeaderHeightPx As Single
        Friend ReadOnly Property FooterHeightPx As Single
        Friend ReadOnly Property RowHeightPx As Single
        Friend ReadOnly Property RowGapPx As Single
        Friend ReadOnly Property EventRadiusPx As Single
        Friend ReadOnly Property TimeRailWidthPx As Single
        Friend ReadOnly Property TimePillWidthPx As Single
        Friend ReadOnly Property TimePillHeightPx As Single
        Friend ReadOnly Property NavButtonSizePx As Single
        Friend ReadOnly Property MaxVisibleRows As Integer
        Friend ReadOnly Property ShowHeader As Boolean
        Friend ReadOnly Property ShowHeaderSubtitle As Boolean
        Friend ReadOnly Property ShowFooter As Boolean
        Friend ReadOnly Property ShowTimeRail As Boolean
        Friend ReadOnly Property ShowEventDetail As Boolean

        Private Sub New(profile As MASResponsiveLayoutProfile,
                        paddingPx As Single,
                        headerHeightPx As Single,
                        footerHeightPx As Single,
                        rowHeightPx As Single,
                        rowGapPx As Single,
                        eventRadiusPx As Single,
                        timeRailWidthPx As Single,
                        timePillWidthPx As Single,
                        timePillHeightPx As Single,
                        navButtonSizePx As Single,
                        maxVisibleRows As Integer,
                        showHeader As Boolean,
                        showHeaderSubtitle As Boolean,
                        showFooter As Boolean,
                        showTimeRail As Boolean,
                        showEventDetail As Boolean)
            Dim safeProfile As MASResponsiveLayoutProfile = If(profile, MASResponsiveLayoutResolver.FromLogicalSize(MASSizeLayoutIntegrationContext.Empty, SKSize.Empty, MASSize.Default, Thresholds))
            Me.ResponsiveProfile = safeProfile
            Me.Mode = safeProfile.Mode
            Me.Dpi = Math.Max(0.64F, PositiveOrDefault(safeProfile.DpiScale, 1.0F))
            Me.PaddingPx = PositiveOrZero(paddingPx)
            Me.HeaderHeightPx = PositiveOrZero(headerHeightPx)
            Me.FooterHeightPx = PositiveOrZero(footerHeightPx)
            Me.RowHeightPx = Math.Max(24.0F, PositiveOrDefault(rowHeightPx, 24.0F))
            Me.RowGapPx = PositiveOrZero(rowGapPx)
            Me.EventRadiusPx = PositiveOrZero(eventRadiusPx)
            Me.TimeRailWidthPx = PositiveOrZero(timeRailWidthPx)
            Me.TimePillWidthPx = PositiveOrZero(timePillWidthPx)
            Me.TimePillHeightPx = PositiveOrZero(timePillHeightPx)
            Me.NavButtonSizePx = PositiveOrZero(navButtonSizePx)
            Me.MaxVisibleRows = Math.Max(1, maxVisibleRows)
            Me.ShowHeader = showHeader
            Me.ShowHeaderSubtitle = showHeaderSubtitle
            Me.ShowFooter = showFooter
            Me.ShowTimeRail = showTimeRail
            Me.ShowEventDetail = showEventDetail
        End Sub

        Friend Shared Function Create(profile As MASResponsiveLayoutProfile) As MASAgendaViewLayoutPlan
            Dim safeProfile As MASResponsiveLayoutProfile = If(profile, MASResponsiveLayoutResolver.FromLogicalSize(MASSizeLayoutIntegrationContext.Empty, SKSize.Empty, MASSize.Default, Thresholds))
            Dim dpi As Single = Math.Max(0.64F, safeProfile.DpiScale)
            Dim chromeDpi As Single = Math.Max(0.64F, dpi * safeProfile.ChromeScale)
            Dim gapDpi As Single = Math.Max(0.64F, dpi * safeProfile.GapScale)
            Dim contentDpi As Single = Math.Max(0.64F, dpi * safeProfile.ContentScale)

            Dim showHeader As Boolean = safeProfile.Mode <> MASResponsiveLayoutMode.Collapsed
            Dim showSubtitle As Boolean = safeProfile.Mode = MASResponsiveLayoutMode.Full OrElse safeProfile.Mode = MASResponsiveLayoutMode.Dense
            Dim showFooter As Boolean = safeProfile.Mode = MASResponsiveLayoutMode.Full OrElse safeProfile.Mode = MASResponsiveLayoutMode.Dense
            Dim showRail As Boolean = safeProfile.Mode <> MASResponsiveLayoutMode.Minimal AndAlso safeProfile.Mode <> MASResponsiveLayoutMode.Collapsed AndAlso safeProfile.AvailableSizeDip.Width >= 300.0F
            Dim showDetail As Boolean = safeProfile.Mode = MASResponsiveLayoutMode.Full OrElse safeProfile.Mode = MASResponsiveLayoutMode.Dense
            Dim maxRows As Integer
            Select Case safeProfile.Mode
                Case MASResponsiveLayoutMode.Full
                    maxRows = AgendaViewTokens.MaxVisibleEvents
                Case MASResponsiveLayoutMode.Dense
                    maxRows = Math.Min(5, AgendaViewTokens.MaxVisibleEvents)
                Case MASResponsiveLayoutMode.Compact
                    maxRows = 4
                Case Else
                    maxRows = 3
            End Select

            Return New MASAgendaViewLayoutPlan(
                profile:=safeProfile,
                paddingPx:=AgendaViewTokens.PaddingDip * gapDpi,
                headerHeightPx:=If(showHeader, AgendaViewTokens.HeaderHeightDip * chromeDpi, 0.0F),
                footerHeightPx:=If(showFooter, AgendaViewTokens.FooterHeightDip * chromeDpi, 0.0F),
                rowHeightPx:=AgendaViewTokens.RowHeightDip * contentDpi,
                rowGapPx:=AgendaViewTokens.RowGapDip * gapDpi,
                eventRadiusPx:=AgendaViewTokens.EventRadiusDip * chromeDpi,
                timeRailWidthPx:=If(showRail, AgendaViewTokens.TimeRailWidthDip * chromeDpi, 0.0F),
                timePillWidthPx:=AgendaViewTokens.TimePillWidthDip * chromeDpi,
                timePillHeightPx:=AgendaViewTokens.TimePillHeightDip * chromeDpi,
                navButtonSizePx:=AgendaViewTokens.NavButtonSizeDip * chromeDpi,
                maxVisibleRows:=maxRows,
                showHeader:=showHeader,
                showHeaderSubtitle:=showSubtitle,
                showFooter:=showFooter,
                showTimeRail:=showRail,
                showEventDetail:=showDetail)
        End Function

        Private Shared Function PositiveOrDefault(value As Single, fallback As Single) As Single
            If Single.IsNaN(value) OrElse Single.IsInfinity(value) OrElse value <= 0.0F Then Return fallback
            Return value
        End Function

        Private Shared Function PositiveOrZero(value As Single) As Single
            If Single.IsNaN(value) OrElse Single.IsInfinity(value) OrElse value < 0.0F Then Return 0.0F
            Return value
        End Function
    End Class

End Namespace
