Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Specialized
Imports System.Diagnostics
Imports System.IO
Imports System.Windows.Forms
Imports Nexamas.UI.Controls
Imports Nexamas.UI.Composition
Imports Nexamas.UI.FileSystem
Imports Nexamas.UI.General
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports Nexamas.UI.Layout
Imports SkiaSharp

Namespace Nexamas.UI.Components


    Partial Public NotInheritable Class MASFilePickerControl
#Region "Stable picker API"

        Public Property FooterText As String
            Get
                Return CurrentOptions.FooterText
            End Get
            Set(value As String)
                Dim v As String = If(value, String.Empty)

                If String.Equals(CurrentOptions.FooterText, v, StringComparison.Ordinal) Then Return

                UpdateOptions(
            Sub(o)
                o.FooterText = v
            End Sub,
            fullRebuild:=False,
            keepPath:=True
        )
            End Set
        End Property

        Public ReadOnly Property PickerOptions As MASFilePickerOptions
            Get
                Return GetOptionsSnapshot()
            End Get
        End Property


        Friend ReadOnly Property Browser As MASFileBrowser
            Get
                Return _browser
            End Get
        End Property


        Public Property Mode As MASFilePickerMode
            Get
                Return CurrentOptions.Mode
            End Get
            Set(value As MASFilePickerMode)
                If CurrentOptions.Mode = value Then Return

                UpdateOptions(
                    Sub(o)
                        o.Mode = value
                        o.Normalize()
                    End Sub,
                    fullRebuild:=True,
                    keepPath:=True
                )
            End Set
        End Property


        Public Property Title As String
            Get
                Return CurrentOptions.Title
            End Get
            Set(value As String)
                Dim v As String = If(value, String.Empty)
                If String.Equals(CurrentOptions.Title, v, StringComparison.Ordinal) Then Return

                UpdateOptions(
                    Sub(o)
                        o.Title = v
                    End Sub,
                    fullRebuild:=False,
                    keepPath:=True
                )
            End Set
        End Property


        Public Property InitialPath As String
            Get
                Return CurrentOptions.InitialPath
            End Get
            Set(value As String)
                NavigateTo(If(value, String.Empty))
            End Set
        End Property


        Public Property FileName As String
            Get
                Return CurrentOptions.FileName
            End Get
            Set(value As String)
                Dim v As String = If(value, String.Empty)

                If _controller IsNot Nothing Then
                    _controller.SaveFileName = v
                End If

                If _fileNameBox IsNot Nothing AndAlso
                   Not String.Equals(_fileNameBox.Text, v, StringComparison.Ordinal) Then

                    _fileNameBox.Text = v
                End If

                RaiseStateChanged()
                SafeInvalidate()
            End Set
        End Property


        Public Property AllowMultiSelect As Boolean
            Get
                Return CurrentOptions.AllowMultiSelect
            End Get
            Set(value As Boolean)
                If CurrentOptions.AllowMultiSelect = value Then Return

                UpdateOptions(
                    Sub(o)
                        o.AllowMultiSelect = value
                        o.Normalize()
                    End Sub,
                    fullRebuild:=True,
                    keepPath:=True
                )
            End Set
        End Property


        Public Property ShowHidden As Boolean
            Get
                Return CurrentOptions.ShowHidden
            End Get
            Set(value As Boolean)
                If CurrentOptions.ShowHidden = value Then Return

                If _browser IsNot Nothing Then
                    _browser.ShowHidden = value
                End If

                UpdateOptions(
                    Sub(o)
                        o.ShowHidden = value
                    End Sub,
                    fullRebuild:=False,
                    keepPath:=True
                )
            End Set
        End Property


        Public Property Filter As String
            Get
                Return CurrentOptions.Filter
            End Get
            Set(value As String)
                Dim v As String = MASFilePickerOptions.NormalizeFilter(value)
                If String.Equals(CurrentOptions.Filter, v, StringComparison.OrdinalIgnoreCase) Then Return

                If _browser IsNot Nothing Then
                    _browser.Filter = v
                End If

                UpdateOptions(
                    Sub(o)
                        o.Filter = v
                    End Sub,
                    fullRebuild:=False,
                    keepPath:=True
                )
            End Set
        End Property


        Public Property ConfirmOverwrite As Boolean
            Get
                Return CurrentOptions.ConfirmOverwrite
            End Get
            Set(value As Boolean)
                If CurrentOptions.ConfirmOverwrite = value Then Return

                UpdateOptions(
                    Sub(o)
                        o.ConfirmOverwrite = value
                    End Sub,
                    fullRebuild:=False,
                    keepPath:=True
                )
            End Set
        End Property


        Public Property PrimaryButtonTextOverride As String
            Get
                Return CurrentOptions.PrimaryButtonTextOverride
            End Get
            Set(value As String)
                Dim v As String = If(value, String.Empty)
                If String.Equals(CurrentOptions.PrimaryButtonTextOverride, v, StringComparison.Ordinal) Then Return

                UpdateOptions(
                    Sub(o)
                        o.PrimaryButtonTextOverride = v
                    End Sub,
                    fullRebuild:=False,
                    keepPath:=True
                )
            End Set
        End Property


        Public ReadOnly Property CurrentPath As String
            Get
                If _controller Is Nothing Then Return String.Empty
                Return _controller.CurrentPath
            End Get
        End Property


        Friend ReadOnly Property SelectedItem As FileSystemEntry
            Get
                If _controller Is Nothing Then Return Nothing
                Return _controller.SelectedItem
            End Get
        End Property

        ''' <summary>
        ''' Gets the primary selected path without exposing the internal FileSystemEntry model.
        ''' </summary>


        ''' <summary>
        ''' Gets the primary selected path without exposing the internal FileSystemEntry model.
        ''' </summary>
        Public ReadOnly Property SelectedPath As String
            Get
                If _controller Is Nothing OrElse _controller.SelectedItem Is Nothing Then Return String.Empty
                Return If(_controller.SelectedItem.FullPath, String.Empty)
            End Get
        End Property

        ''' <summary>
        ''' Gets a snapshot of the current selected paths without exposing browser internals.
        ''' </summary>


        ''' <summary>
        ''' Gets a snapshot of the current selected paths without exposing browser internals.
        ''' </summary>
        Public ReadOnly Property SelectedPaths As IReadOnlyList(Of String)
            Get
                Dim result As New List(Of String)()

                If _controller Is Nothing Then Return result

                For Each entry As FileSystemEntry In _controller.SelectedItems
                    If entry Is Nothing Then Continue For

                    Dim path As String = If(entry.FullPath, String.Empty).Trim()
                    If path.Length > 0 Then result.Add(path)
                Next

                Return result
            End Get
        End Property


        Public ReadOnly Property SelectedIndex As Integer
            Get
                If _browser Is Nothing Then Return -1
                Return _browser.SelectedIndex
            End Get
        End Property


        Public ReadOnly Property CanAccept As Boolean
            Get
                If _controller Is Nothing Then Return False
                Return _controller.CanAccept
            End Get
        End Property


        Public ReadOnly Property IsSaveMode As Boolean
            Get
                Return CurrentOptions.Mode = MASFilePickerMode.SaveFile
            End Get
        End Property


        Public ReadOnly Property IsOpenFileMode As Boolean
            Get
                Return CurrentOptions.Mode = MASFilePickerMode.OpenFile
            End Get
        End Property


        Public ReadOnly Property IsPickTargetsMode As Boolean
            Get
                Return CurrentOptions.Mode = MASFilePickerMode.PickTargets
            End Get
        End Property



#End Region

    End Class

End Namespace
