Imports System.Collections.ObjectModel
Imports System.IO
Imports EgtUILib

Public Class OpenFileWD

   ' Properties
   Private m_sCurrDir As String = String.Empty
   Private m_sCurrFile As String = String.Empty
   Private m_sExt As String = String.Empty
   Private m_sExt2 As String = String.Empty
   Private n_sDeniedFiles As New List(Of String)

   Private m_OpenItemList As New ObservableCollection(Of IconListBoxItem)

   Sub New(Owner As Window, sDir As String, sExt As String)
      Me.Owner = Owner
      m_sCurrDir = sDir
      m_sExt = sExt
      InitializeComponent()
   End Sub

   Sub New(Owner As Window, sDir As String, sExt As String, sExt2 As String)
      Me.Owner = Owner
      m_sCurrDir = sDir
      m_sExt = sExt
      m_sExt2 = sExt2
      InitializeComponent()
   End Sub

   Sub New(Owner As Window, sDir As String, sExt As String, sExt2 As String, DeniedFiles As List(Of String))
      Me.Owner = Owner
      m_sCurrDir = sDir
      m_sExt = sExt
      m_sExt2 = sExt2
      n_sDeniedFiles = DeniedFiles
      InitializeComponent()
   End Sub

   Private Sub OpenFile_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
      ' Posizione finestra
      Me.Top = Owner.Top + Owner.Height / 2 - Me.Height / 2
      Me.Left = Owner.Left + Owner.Width / 2 - Me.Width / 2
      ' Definizione del collegamento tra ItemList e ListBox1
      FileListBox.ItemsSource = m_OpenItemList
   End Sub

   Private Sub OpenFile_Loaded(sender As Object, e As EventArgs) Handles Me.Loaded
      ' carico direttorio
      LoadCurrDir()
      ' disabilito ok
      OkBtn.IsEnabled = False
   End Sub

   Private Function LoadCurrDir() As Boolean
      ' se direttorio corrente non valido, carico l'elenco dei dischi
      If String.IsNullOrWhiteSpace(m_sCurrDir) OrElse Not IO.Directory.Exists(m_sCurrDir) Then
         Return LoadDisks()
      End If
      Dim TempPath As New Text.StringBuilder(260)
      PathCompactPathEx(TempPath, m_sCurrDir, 28, 0)
      ' lo visualizzo
      FilePathTxBl.Content = TempPath.ToString
      ' pulisco la lista
      m_OpenItemList.Clear()
      ' per risalire al direttorio padre
      m_OpenItemList.Add(New IconListBoxItem("..", 0))
      ' elenco dei sottodirettori
      Dim DirInfo As New DirectoryInfo(m_sCurrDir)
      Dim vDirI As DirectoryInfo() = DirInfo.GetDirectories("*")
      Dim DirI As DirectoryInfo
      For Each DirI In vDirI
         ' per saltare i link, troppo complessi da gestire
         If (DirI.Attributes And FileAttributes.Hidden) <> FileAttributes.Hidden Or
            (DirI.Attributes And FileAttributes.System) <> FileAttributes.System Then
            m_OpenItemList.Add(New IconListBoxItem(DirI.Name, 2))
         End If
      Next
      ' elenco dei file
      Dim vFileI As FileInfo() = DirInfo.GetFiles()
      Dim FileI As FileInfo
      For Each FileI In vFileI
         Dim sExt As String = Path.GetExtension(FileI.Name).ToUpper()
         Dim sFileName As String = Path.GetFileNameWithoutExtension(FileI.Name).ToUpper.Trim
         Dim bDenied As Boolean = False
         For Each ItemFile As String In n_sDeniedFiles
            If ItemFile.ToUpper.Trim = sFileName Then
               bDenied = True
               Exit For
            End If
         Next
         If (String.Compare(sExt, m_sExt, True) = 0 Or String.Compare(sExt, m_sExt2, True) = 0) And Not bDenied Then
            m_OpenItemList.Add(New IconListBoxItem(FileI.Name, 3))
         End If
      Next
         ' riporto visualizzazione in cima alla lista (esiste sempre almeno un elemento)
         FileListBox.ScrollIntoView(m_OpenItemList(0))
      Return True
   End Function

   Private Function LoadDisks() As Boolean
      ' dir corrente vuoto
      m_sCurrDir = ""
      ' lo visualizzo
      FilePathTxBl.Content = m_sCurrDir
      ' pulisco la lista
      m_OpenItemList.Clear()
      ' elenco dei dischi
      Dim vDriI As DriveInfo() = DriveInfo.GetDrives()
      Dim DriI As DriveInfo
      For Each DriI In vDriI
         m_OpenItemList.Add(New IconListBoxItem(DriI.Name, 1))
      Next
      Return True
   End Function

   Private Sub FileListBox_PreviewMouseUp(sender As Object, e As MouseButtonEventArgs) Handles FileListBox.PreviewMouseUp
      ' Disabilito Ok
      OkBtn.IsEnabled = False

      ' Recupero item selezionato
      If FileListBox.SelectedItems.Count() = 0 Then
         Return
      End If

      Dim vItems As IconListBoxItem = FileListBox.SelectedItems(0)
      ' A seconda del tipo
      Select Case vItems.PictureID
      Case 0        ' Vai nel direttorio padre
         m_sCurrDir = IO.Path.GetDirectoryName(m_sCurrDir)
         m_sCurrFile = ""
         LoadCurrDir()
      Case 1        ' Vai nella radice del disco
         m_sCurrDir = vItems.Name
         m_sCurrFile = ""
         LoadCurrDir()
      Case 2        ' Vai nel sottodirettorio
         m_sCurrDir = IO.Path.Combine(m_sCurrDir, vItems.Name)
         m_sCurrFile = ""
         LoadCurrDir()
      Case 3        ' File
         m_sCurrFile = vItems.Name
         OkBtn.IsEnabled = True
      End Select

   End Sub

   Private Sub FileListBox_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles FileListBox.SelectionChanged
      ' Recupero item selezionato
      If FileListBox.SelectedItems.Count() = 0 Then
         Return
      End If

      Dim vItems As IconListBoxItem = FileListBox.SelectedItems(0)

      ' Gestisco solo aggiornamento visualizzazione file
      If vItems.PictureID = 3 Then
         m_sCurrFile = vItems.Name
         OkBtn.IsEnabled = True
      Else
         m_sCurrFile = ""
         OkBtn.IsEnabled = False
      End If
   End Sub

   Private Sub OkBtn_Click(sender As Object, e As RoutedEventArgs) Handles OkBtn.Click
      DialogResult = True
   End Sub

   Friend Function GetFilePath() As String
      If Not String.IsNullOrWhiteSpace(m_sCurrFile) Then
         Return m_sCurrDir & "\" & m_sCurrFile
      Else
         Return ""
      End If
   End Function

   Friend Function GetFileDir() As String
      If Not String.IsNullOrWhiteSpace(m_sCurrFile) Then
         Return m_sCurrDir
      Else
         Return ""
      End If
   End Function

End Class
