Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Threading
Imports System.Threading.Tasks

Imports Nexamas.UI.Architecture
Imports Nexamas.UI.Composition
Imports Nexamas.UI.Controls
Imports Nexamas.UI.FileSystem
Imports Nexamas.UI.General
Imports Nexamas.UI.Icons
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Platform
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values

Imports SkiaSharp

Namespace Nexamas.UI.Components


    Partial Friend NotInheritable Class MASFileBrowser
#Region "Browser state API"

        Friend Property Provider As IFileSystemProvider
            Get
                Return _provider
            End Get
            Set(value As IFileSystemProvider)
                If value Is Nothing Then Return
                If Object.ReferenceEquals(_provider, value) Then Return

                CancelPendingItemLoad()

                _provider = value
                _childrenLoader = New FileSystemEntryChildrenLoader(_provider)
                _computerRootBuilder = New FileSystemComputerRootBuilder(_provider)

                ClearSelectionInternal(False)
                RefreshItems()
                SafeInvalidate()
            End Set
        End Property


        Friend Property SelectionPolicy As MASFileBrowserSelectionPolicy
            Get
                Return _selectionPolicy
            End Get
            Set(value As MASFileBrowserSelectionPolicy)
                _selectionPolicy = If(value, MASFileBrowserSelectionPolicy.FilesOnly(False))

                If Not _selectionPolicy.AllowMultiSelect AndAlso _selectedPaths.Count > 1 Then
                    Dim keepPath As String = GetPrimarySelectedPath()
                    _selectedPaths.Clear()

                    If Not String.IsNullOrWhiteSpace(keepPath) Then
                        _selectedPaths.Add(keepPath)
                    End If

                    MarkFileBrowserSelectionSnapshotDirty()
                End If

                ReconcileSelectionWithItems(True)
                SyncListSelectionFromSelectionState()
                SafeInvalidate()
            End Set
        End Property


        Friend Property CurrentPath As String
            Get
                Return _currentPath
            End Get
            Set(value As String)
                Dim normalized As String = If(value, String.Empty)

                If _provider IsNot Nothing Then
                    normalized = _provider.NormalizePath(normalized)
                End If

                If String.Equals(_currentPath, normalized, StringComparison.OrdinalIgnoreCase) Then Return

                _currentPath = normalized
                ClearSelectionInternal(False)

                RaiseEvent CurrentPathChanged(Me, EventArgs.Empty)

                RefreshItems()
                SafeInvalidate()
            End Set
        End Property


        Friend ReadOnly Property Items As IReadOnlyList(Of FileSystemEntry)
            Get
                Return _items
            End Get
        End Property


        Friend Property ShowHidden As Boolean
            Get
                Return _showHidden
            End Get
            Set(value As Boolean)
                If _showHidden = value Then Return

                _showHidden = value
                RefreshItems()
                SafeInvalidate()
            End Set
        End Property


        Friend Property Filter As String
            Get
                Return _filter
            End Get
            Set(value As String)
                Dim v As String = NormalizeFilter(value)

                If String.Equals(_filter, v, StringComparison.OrdinalIgnoreCase) Then Return

                _filter = v
                RefreshItems()
                SafeInvalidate()
            End Set
        End Property


        Friend ReadOnly Property SelectedIndex As Integer
            Get
                Return _selectedIndex
            End Get
        End Property


        Friend ReadOnly Property SelectedItem As FileSystemEntry
            Get
                If _selectedIndex < 0 OrElse _selectedIndex >= _items.Count Then Return Nothing
                Return _items(_selectedIndex)
            End Get
        End Property


        Friend ReadOnly Property SelectedItems As IReadOnlyList(Of FileSystemEntry)
            Get
                Dim result As New List(Of FileSystemEntry)()

                For Each it As FileSystemEntry In _items
                    If it IsNot Nothing AndAlso _selectedPaths.Contains(If(it.FullPath, String.Empty)) Then
                        result.Add(it)
                    End If
                Next

                Return result
            End Get
        End Property


        Friend ReadOnly Property SelectedPaths As IReadOnlyList(Of String)
            Get
                Return EnsureSelectedPathSnapshot()
            End Get
        End Property


        Friend Property AllowMultiSelect As Boolean
            Get
                Return _selectionPolicy IsNot Nothing AndAlso _selectionPolicy.AllowMultiSelect
            End Get
            Set(value As Boolean)
                If _selectionPolicy Is Nothing Then
                    _selectionPolicy = MASFileBrowserSelectionPolicy.FilesOnly(value)
                End If

                If _selectionPolicy.AllowMultiSelect = value Then Return

                _selectionPolicy.AllowMultiSelect = value

                If Not value AndAlso _selectedPaths.Count > 1 Then
                    Dim keepPath As String = GetPrimarySelectedPath()
                    _selectedPaths.Clear()

                    If Not String.IsNullOrWhiteSpace(keepPath) Then
                        _selectedPaths.Add(keepPath)
                    End If

                    MarkFileBrowserSelectionSnapshotDirty()
                End If

                ReconcileSelectionWithItems(True)
                SyncListSelectionFromSelectionState()
                SafeInvalidate()
            End Set
        End Property



#End Region

    End Class

End Namespace
