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 "Picker commands and creation gateways"

        Public Sub ApplyOptions(options As MASFilePickerOptions)
            If options Is Nothing Then Throw New ArgumentNullException(NameOf(options))
            RebuildWithOptions(MASFilePickerOptions.SafeClone(options), fullRebuild:=True, keepPath:=False)
        End Sub


        Public Function AcceptSelection() As MASFilePickerResult
            ThrowIfDisposed()
            SyncControllerSaveFileNameFromTextBox()

            If _controller Is Nothing Then Return New MASFilePickerResult()
            Return _controller.AcceptSelection()
        End Function


        Public Sub CancelSelection()
            ThrowIfDisposed()
            If _controller Is Nothing Then Return
            _controller.CancelSelection()
        End Sub


        Public Sub LoadRoots()
            ThrowIfDisposed()
            If _controller Is Nothing Then Return

            _controller.LoadRoots()

            SyncNavigationBarState()
            RaiseStateChanged()
            SafeInvalidate()
        End Sub


        Public Sub RefreshItems()
            ThrowIfDisposed()
            If _browser Is Nothing Then Return

            _browser.RefreshItems()
            SyncNavigationBarState()
            RaiseStateChanged()
            SafeInvalidate()
        End Sub


        Public Sub NavigateUp()
            ThrowIfDisposed()
            If _controller Is Nothing Then Return

            _controller.NavigateUp()

            SyncNavigationBarState()
            RaiseStateChanged()
            SafeInvalidate()
        End Sub


        Public Sub NavigateTo(path As String)
            ThrowIfDisposed()
            If _controller Is Nothing Then Return

            _controller.NavigateTo(If(path, String.Empty))

            SyncNavigationBarState()
            RaiseStateChanged()
            SafeInvalidate()
        End Sub


        Public Sub SelectFirst()
            ThrowIfDisposed()
            If _controller Is Nothing Then Return

            _controller.SelectFirst()
            RaiseStateChanged()
            SafeInvalidate()
        End Sub


        Public Sub UseSelectedFileNameForSave()
            ThrowIfDisposed()
            If _controller Is Nothing Then Return

            _controller.UseSelectedFileNameForSave()
            SyncFileNameBoxFromController()

            RaiseStateChanged()
            SafeInvalidate()
        End Sub


        Friend Shared Function CreateOpenFile(initialPath As String, filter As String) As MASFilePickerControl
            Return New MASFilePickerControl(
                MASFilePickerOptions.OpenFile(
                    initialPath:=If(initialPath, String.Empty),
                    filter:=MASFilePickerOptions.NormalizeFilter(filter)
                )
            )
        End Function


        Friend Shared Function CreateSaveFile(initialPath As String,
                                              filter As String,
                                              defaultFileName As String) As MASFilePickerControl

            Return New MASFilePickerControl(
                MASFilePickerOptions.SaveFile(
                    initialPath:=If(initialPath, String.Empty),
                    filter:=MASFilePickerOptions.NormalizeFilter(filter),
                    fileName:=If(defaultFileName, String.Empty)
                )
            )
        End Function


        Friend Shared Function CreateTargetsPicker(initialPath As String) As MASFilePickerControl
            Return New MASFilePickerControl(
                MASFilePickerOptions.TargetsPicker(
                    targetKinds:=MASFileSystemSelectionKinds.Folder Or MASFileSystemSelectionKinds.Drive,
                    initialPath:=If(initialPath, String.Empty)
                )
            )
        End Function

        Public Sub GoBack()
            ThrowIfDisposed()
            If _controller Is Nothing Then Return

            If _controller.GoBack() Then
                SyncNavigationBarState()
                RaiseStateChanged()
                SafeInvalidate()
            End If
        End Sub


        Public Sub GoForward()
            ThrowIfDisposed()
            If _controller Is Nothing Then Return

            If _controller.GoForward() Then
                SyncNavigationBarState()
                RaiseStateChanged()
                SafeInvalidate()
            End If
        End Sub


#End Region

    End Class

End Namespace
