Option Strict On
Option Explicit On

Namespace Nexamas.UI.FileSystem

    Friend NotInheritable Class FilePickerSelectionPolicy

        Friend Property SelectableKinds As MASFileSystemSelectionKinds = MASFileSystemSelectionKinds.None
        Friend Property ResultKinds As MASFileSystemSelectionKinds = MASFileSystemSelectionKinds.None

        Friend Property AllowMultiSelect As Boolean = False
        Friend Property AcceptCurrentPathWhenNoSelection As Boolean = False

        Friend Property InvokeNavigatesIntoFolders As Boolean = True
        Friend Property InvokeNavigatesIntoDrives As Boolean = True
        Friend Property InvokeNavigatesIntoSpecial As Boolean = True

        Friend Function CanSelect(entry As FileSystemEntry) As Boolean
            If entry Is Nothing Then Return False
            Return (SelectableKinds And GetKind(entry)) <> MASFileSystemSelectionKinds.None
        End Function

        Friend Function CanReturn(entry As FileSystemEntry) As Boolean
            If entry Is Nothing Then Return False
            Return (ResultKinds And GetKind(entry)) <> MASFileSystemSelectionKinds.None
        End Function

        Friend Function CanNavigateInto(entry As FileSystemEntry) As Boolean
            If entry Is Nothing Then Return False

            Select Case GetKind(entry)
                Case MASFileSystemSelectionKinds.Folder
                    Return InvokeNavigatesIntoFolders

                Case MASFileSystemSelectionKinds.Drive
                    Return InvokeNavigatesIntoDrives

                Case MASFileSystemSelectionKinds.Special
                    Return InvokeNavigatesIntoSpecial

                Case Else
                    Return False
            End Select
        End Function

        Friend Shared Function CreateForOptions(options As MASFilePickerOptions) As FilePickerSelectionPolicy
            Dim safe As MASFilePickerOptions = MASFilePickerOptions.SafeClone(options)

            Dim policy As New FilePickerSelectionPolicy With {
                .AllowMultiSelect = safe.AllowMultiSelect,
                .AcceptCurrentPathWhenNoSelection = safe.AcceptCurrentPathWhenNoSelection,
                .InvokeNavigatesIntoFolders = True,
                .InvokeNavigatesIntoDrives = True,
                .InvokeNavigatesIntoSpecial = True
            }

            Select Case safe.Mode
                Case MASFilePickerMode.OpenFile
                    policy.SelectableKinds = MASFileSystemSelectionKinds.FileItem
                    policy.ResultKinds = MASFileSystemSelectionKinds.FileItem

                Case MASFilePickerMode.SaveFile
                    policy.SelectableKinds = MASFileSystemSelectionKinds.FileItem
                    policy.ResultKinds = MASFileSystemSelectionKinds.None
                    policy.AllowMultiSelect = False
                    policy.AcceptCurrentPathWhenNoSelection = False

                Case MASFilePickerMode.PickTargets
                    Dim targetKinds As MASFileSystemSelectionKinds = safe.TargetKinds

                    If targetKinds = MASFileSystemSelectionKinds.None Then
                        targetKinds = MASFileSystemSelectionKinds.Folder Or MASFileSystemSelectionKinds.Drive
                    End If

                    policy.SelectableKinds = targetKinds
                    policy.ResultKinds = targetKinds

                    If targetKinds = MASFileSystemSelectionKinds.Drive Then
                        policy.InvokeNavigatesIntoFolders = False
                        policy.InvokeNavigatesIntoDrives = False
                        policy.InvokeNavigatesIntoSpecial = False
                    Else
                        policy.InvokeNavigatesIntoFolders = True
                        policy.InvokeNavigatesIntoDrives = True
                        policy.InvokeNavigatesIntoSpecial = True
                    End If

                Case Else
                    policy.SelectableKinds = MASFileSystemSelectionKinds.None
                    policy.ResultKinds = MASFileSystemSelectionKinds.None
            End Select

            Return policy
        End Function

        Friend Shared Function GetKind(entry As FileSystemEntry) As MASFileSystemSelectionKinds
            If entry Is Nothing Then Return MASFileSystemSelectionKinds.None

            Select Case entry.EntryType
                Case FileSystemEntryType.FileItem
                    Return MASFileSystemSelectionKinds.FileItem

                Case FileSystemEntryType.Folder,
                     FileSystemEntryType.Desktop,
                     FileSystemEntryType.Documents,
                     FileSystemEntryType.Downloads,
                     FileSystemEntryType.UserProfile

                    Return MASFileSystemSelectionKinds.Folder

                Case FileSystemEntryType.Drive
                    Return MASFileSystemSelectionKinds.Drive

                Case FileSystemEntryType.Special,
                     FileSystemEntryType.Computer,
                     FileSystemEntryType.RecycleBin,
                     FileSystemEntryType.ControlPanel,
                     FileSystemEntryType.Network

                    Return MASFileSystemSelectionKinds.Special

                Case Else
                    Return MASFileSystemSelectionKinds.None
            End Select
        End Function

    End Class

End Namespace