Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.Linq

Namespace Nexamas.UI.FileSystem

    ''' <summary>
    ''' Represents the result produced by a file picker action.
    ''' </summary>
    ''' <remarks>
    ''' MASFilePickerResult is pure data.
    '''
    ''' It does not know about Overlay.
    ''' It does not know about Float.
    ''' It does not close anything.
    ''' It does not own UI lifetime.
    '''
    ''' Accepted=True means the picker produced one or more valid selected paths.
    ''' </remarks>
    Public NotInheritable Class MASFilePickerResult

        Private ReadOnly _selectedPaths As IReadOnlyList(Of String)

        Public ReadOnly Property Accepted As Boolean
        Public ReadOnly Property SelectedPath As String

        Public ReadOnly Property SelectedPaths As IReadOnlyList(Of String)
            Get
                Return _selectedPaths
            End Get
        End Property

        Friend Sub New()
            Me.New(False, String.Empty, New String() {})
        End Sub

        Friend Sub New(accepted As Boolean,
                       selectedPath As String,
                       selectedPaths As IEnumerable(Of String))
            Me.Accepted = accepted
            Me.SelectedPath = NormalizePath(selectedPath)
            Me._selectedPaths = NormalizePaths(selectedPaths)
        End Sub

        Public ReadOnly Property HasSelection As Boolean
            Get
                Return _selectedPaths IsNot Nothing AndAlso
                       _selectedPaths.Any(Function(x) Not String.IsNullOrWhiteSpace(x))
            End Get
        End Property

        Public Shared Function Cancelled() As MASFilePickerResult
            Return New MASFilePickerResult(False, String.Empty, New String() {})
        End Function

        Public Shared Function FromPath(path As String) As MASFilePickerResult
            Dim p As String = NormalizePath(path)
            If String.IsNullOrWhiteSpace(p) Then Return Cancelled()

            Return New MASFilePickerResult(True, p, New String() {p})
        End Function

        Public Shared Function FromPaths(paths As IEnumerable(Of String)) As MASFilePickerResult
            Dim normalized As IReadOnlyList(Of String) = NormalizePaths(paths)
            If normalized.Count = 0 Then Return Cancelled()

            Return New MASFilePickerResult(True, normalized(0), normalized)
        End Function

        Private Shared Function NormalizePaths(paths As IEnumerable(Of String)) As IReadOnlyList(Of String)
            If paths Is Nothing Then Return New String() {}

            Dim selected As New List(Of String)()
            Dim seen As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase)

            For Each raw As String In paths
                Dim p As String = NormalizePath(raw)
                If String.IsNullOrWhiteSpace(p) Then Continue For

                If seen.Add(p) Then
                    selected.Add(p)
                End If
            Next

            Return selected.ToArray()
        End Function

        Private Shared Function NormalizePath(path As String) As String
            If String.IsNullOrWhiteSpace(path) Then Return String.Empty
            Return path.Trim()
        End Function

    End Class

End Namespace