Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports Nexamas.UI.Host
Imports Nexamas.UI.Layout

Namespace Nexamas.UI.Application

    ''' <summary>
    ''' Window-facing size profile facade. The selected profile is process-wide by design, matching the global
    ''' application theme model, but this facade gives each window an explicit refresh hook and a convenient API.
    ''' </summary>
    Public NotInheritable Class MASApplicationWindowSizing

        Private ReadOnly _rootHost As MASSkiaRootHost

        Private ReadOnly Property RootHost As MASSkiaRootHost
            Get
                _rootHost.ThrowIfDisposedCore()
                Return _rootHost
            End Get
        End Property

        Friend Sub New(rootHost As MASSkiaRootHost)
            If rootHost Is Nothing Then Throw New ArgumentNullException(NameOf(rootHost))
            _rootHost = rootHost
        End Sub

        Public Custom Event CurrentChanged As EventHandler(Of MASSizeProfileChangedEventArgs)
            AddHandler(value As EventHandler(Of MASSizeProfileChangedEventArgs))
                If value Is Nothing Then Return
                _rootHost.ThrowIfDisposedCore()
                MASRuntimeSizeProfileService.AddPublicCurrentChangedHandler(value)
            End AddHandler
            RemoveHandler(value As EventHandler(Of MASSizeProfileChangedEventArgs))
                If value Is Nothing Then Return
                _rootHost.ThrowIfDisposedCore()
                MASRuntimeSizeProfileService.RemovePublicCurrentChangedHandler(value)
            End RemoveHandler
            RaiseEvent(sender As Object, e As MASSizeProfileChangedEventArgs)
                ' Forwarded by MASRuntimeSizeProfileService.
            End RaiseEvent
        End Event

        Public ReadOnly Property IsGlobalApplicationSizeProfile As Boolean
            Get
                Return True
            End Get
        End Property

        Public ReadOnly Property Current As MASSizeProfile
            Get
                _rootHost.ThrowIfDisposedCore()
                Return MASRuntimeSizeProfileService.Current
            End Get
        End Property

        Public ReadOnly Property CurrentKind As MASSizeProfileKind
            Get
                _rootHost.ThrowIfDisposedCore()
                Return MASRuntimeSizeProfileService.CurrentKind
            End Get
        End Property

        Public Function GetAll() As IReadOnlyList(Of MASSizeProfile)
            _rootHost.ThrowIfDisposedCore()
            Return MASRuntimeSizeProfileService.GetAllProfiles()
        End Function

        Public Function TryUse(kind As MASSizeProfileKind) As Boolean
            _rootHost.ThrowIfDisposedCore()
            Return MASRuntimeSizeProfileService.TryUse(kind)
        End Function

        Public Function TryUse(profile As MASSizeProfile) As Boolean
            _rootHost.ThrowIfDisposedCore()
            Return MASRuntimeSizeProfileService.TryUse(profile)
        End Function

        Public Sub Use(kind As MASSizeProfileKind)
            TryUse(kind)
        End Sub

        Public Sub Use(profile As MASSizeProfile)
            TryUse(profile)
        End Sub

        Public Function TryApplyAndSave(kind As MASSizeProfileKind) As Boolean
            _rootHost.ThrowIfDisposedCore()
            Return MASRuntimeSizeProfileService.TryApplyAndSave(kind)
        End Function

        Public Function TryApplyAndSave(profile As MASSizeProfile) As Boolean
            _rootHost.ThrowIfDisposedCore()
            Return MASRuntimeSizeProfileService.TryApplyAndSave(profile)
        End Function

        Public Sub ApplyAndSave(kind As MASSizeProfileKind)
            TryApplyAndSave(kind)
        End Sub

        Public Sub ApplyAndSave(profile As MASSizeProfile)
            TryApplyAndSave(profile)
        End Sub

        Public Function SaveCurrent() As Boolean
            _rootHost.ThrowIfDisposedCore()
            Return MASRuntimeSizeProfileService.SaveCurrent()
        End Function

        Public Sub Reset()
            TryUse(MASSizeProfile.[Default])
        End Sub

        Public Sub Refresh()
            Dim root As MASSkiaRootHost = RootHost

            root.PostToUiThreadCore(
                Sub()
                    If root.IsDisposed Then Return
                    root.RefreshSizeLayoutForRuntimeProfile()
                End Sub,
                "MASApplicationWindowSizing.Refresh")
        End Sub

    End Class

End Namespace
