Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports Nexamas.UI.Controls

Namespace Nexamas.UI.Application

    ''' <summary>
    ''' MASApplicationWindow.Pages internal immutable page record. It is not public API: consumers register pages
    ''' through MASApplicationPageHost so ApplicationSystem remains the single owner of same-window navigation state.
    ''' </summary>
    Friend NotInheritable Class MASApplicationPageRegistration
        Private ReadOnly _pageId As String
        Private ReadOnly _title As String
        Private ReadOnly _groupName As String
        Private ReadOnly _description As String
        Private ReadOnly _buildPage As Action(Of MASApplicationLayoutPageBuilder)
        Private _cachedPage As MASApplicationLayoutPageBuilder

        Friend Sub New(pageId As String,
                       title As String,
                       groupName As String,
                       description As String,
                       buildPage As Action(Of MASApplicationLayoutPageBuilder))
            _pageId = NormalizePageId(pageId)
            _title = NormalizeText(title)
            _groupName = NormalizeText(groupName)
            _description = NormalizeText(description)
            _buildPage = buildPage
        End Sub

        Friend ReadOnly Property PageId As String
            Get
                Return _pageId
            End Get
        End Property

        Friend ReadOnly Property Title As String
            Get
                Return _title
            End Get
        End Property

        Friend ReadOnly Property GroupName As String
            Get
                Return _groupName
            End Get
        End Property

        Friend ReadOnly Property Description As String
            Get
                Return _description
            End Get
        End Property

        Friend ReadOnly Property IsValid As Boolean
            Get
                Return _pageId.Length > 0 AndAlso _title.Length > 0 AndAlso _buildPage IsNot Nothing
            End Get
        End Property

        Friend Function GetOrBuildPage() As MASApplicationLayoutPageBuilder
            If _cachedPage IsNot Nothing AndAlso ContainsDisposedControls(_cachedPage) Then
                _cachedPage = Nothing
            End If

            If _cachedPage Is Nothing Then
                Dim page As New MASApplicationLayoutPageBuilder()
                _buildPage.Invoke(page)
                _cachedPage = page
            End If

            Return _cachedPage
        End Function

        Friend Shared Function ContainsDisposedControls(page As MASApplicationLayoutPageBuilder) As Boolean
            If page Is Nothing Then Return False

            Dim controls As New List(Of MASControlBase)()
            page.CollectControls(controls)

            For Each control As MASControlBase In controls
                If control IsNot Nothing AndAlso control.IsDisposedInternal Then Return True
            Next

            Return False
        End Function

        Friend Sub InvalidateCachedPage()
            _cachedPage = Nothing
        End Sub

        Friend Shared Function NormalizePageId(value As String) As String
            Dim text As String = If(value, String.Empty).Trim()
            Return text
        End Function

        Friend Shared Function NormalizeText(value As String) As String
            Return If(value, String.Empty).Trim()
        End Function
    End Class

End Namespace
