Option Strict On
Option Explicit On

Imports Nexamas.UI.Layout

Namespace Nexamas.UI.Application

    ''' <summary>
    ''' Options for applying a MAS window presentation profile.
    ''' Consumers express preference and persistence intent; Nexamas UI still owns final safe bounds and refresh.
    ''' </summary>
    Friend NotInheritable Class MASWindowPresentationOptions

        Friend Sub New()
            StartupPlacement = MASWindowStartupPlacement.Auto
            RestoreUserChoice = True
            RestoreProfileChoice = True
            SaveUserChoice = True
            ApplySizeProfile = False
            SizeProfileKind = MASSizeProfileKind.[Default]
        End Sub

        Friend Property PersistenceKey As String
        Friend Property StartupPlacement As MASWindowStartupPlacement
        Friend Property RestoreUserChoice As Boolean
        Friend Property RestoreProfileChoice As Boolean
        Friend Property SaveUserChoice As Boolean
        Friend Property ApplySizeProfile As Boolean
        Friend Property SizeProfileKind As MASSizeProfileKind

        ''' <summary>
        ''' Creates options for a user-customizable window. The supplied key identifies this logical window,
        ''' not a pixel layout, and is stored under Nexamas UI runtime settings for the current application.
        ''' </summary>
        Friend Shared Function UserPreferences(persistenceKey As String) As MASWindowPresentationOptions
            Return New MASWindowPresentationOptions() With {
                .PersistenceKey = persistenceKey,
                .StartupPlacement = MASWindowStartupPlacement.RestoreUserChoice,
                .RestoreUserChoice = True,
                .RestoreProfileChoice = True,
                .SaveUserChoice = True
            }
        End Function

        ''' <summary>
        ''' Creates options for a deterministic startup placement without durable user state.
        ''' </summary>
        Friend Shared Function OneShot(Optional placement As MASWindowStartupPlacement = MASWindowStartupPlacement.CenterOnScreen) As MASWindowPresentationOptions
            Return New MASWindowPresentationOptions() With {
                .StartupPlacement = placement,
                .RestoreUserChoice = False,
                .RestoreProfileChoice = False,
                .SaveUserChoice = False
            }
        End Function

        Friend Shared Function Normalize(options As MASWindowPresentationOptions,
                                         profile As MASWindowPresentationProfile) As MASWindowPresentationOptions
            Dim result As MASWindowPresentationOptions = If(options, New MASWindowPresentationOptions())
            Dim safeProfile As MASWindowPresentationProfile = MASWindowPresentationProfile.Normalize(profile)

            If result.ApplySizeProfile = False Then
                result.SizeProfileKind = safeProfile.SizeProfileKind
            End If

            If String.IsNullOrWhiteSpace(result.PersistenceKey) Then
                result.RestoreUserChoice = False
                result.RestoreProfileChoice = False
                result.SaveUserChoice = False
                If result.StartupPlacement = MASWindowStartupPlacement.RestoreUserChoice Then
                    result.StartupPlacement = MASWindowStartupPlacement.Auto
                End If
            End If

            Return result
        End Function

    End Class

End Namespace
