Imports System.Collections.ObjectModel
Imports System.IO
Imports EgtUILib
Imports System.Runtime.InteropServices

Public Class PhotoPageUC

   'Riferimento alla MainWindow
   Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)

   ' Properties
   Private m_sCurrDir As String = String.Empty
   Private m_sCurrFile As String = String.Empty
   Private m_bFileOk As Boolean = False

   Private Sub PhotoPage_Initialized(sender As Object, e As EventArgs)
      'Definizione del collegamento tra ItemList e ListBox1
      FileListBox.ItemsSource = m_MainWindow.m_PhotoItemList
   End Sub

   Private Sub PhotoPage_Loaded(sender As Object, e As RoutedEventArgs)
      ' leggo direttorio corrente
      GetPrivateProfileString(S_GENERAL, K_IMAGEDIR, "", m_sCurrDir, m_MainWindow.GetIniFile())
      ' lo carico
      LoadCurrDir()
   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, 22, 0)
      ' lo visualizzo
      FilePathTxBl.Text = TempPath.ToString
      ' pulisco la lista
      m_MainWindow.m_PhotoItemList.Clear()
      ' per risalire al direttorio padre
      m_MainWindow.m_PhotoItemList.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_MainWindow.m_PhotoItemList.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()
         If sExt = ".JPG" Or sExt = ".PNG" Or sExt = ".BMP" Then
            m_MainWindow.m_PhotoItemList.Add(New IconListBoxItem(FileI.Name, 3))
         End If
      Next
      ' pulisco la vista
      PhotoImage.Source = Nothing
      m_bFileOk = False
      Return True
   End Function

   Private Function LoadDisks() As Boolean
      ' dir corrente vuoto
      m_sCurrDir = ""
      ' lo visualizzo
      FilePathTxBl.Text = m_sCurrDir
      ' pulisco la lista
      m_MainWindow.m_PhotoItemList.Clear()
      ' elenco dei dischi
      Dim vDriI As DriveInfo() = DriveInfo.GetDrives()
      Dim DriI As DriveInfo
      For Each DriI In vDriI
         m_MainWindow.m_PhotoItemList.Add(New IconListBoxItem(DriI.Name, 1))
      Next
      ' pulisco la vista
      PhotoImage.Source = Nothing
      m_bFileOk = False
      Return True
   End Function

   Private Sub FileListBox_PreviewMouseUp(sender As Object, e As MouseButtonEventArgs) Handles FileListBox.PreviewMouseUp
      ' 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
            LoadCurrFile()
      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
         LoadCurrFile()
      End If
   End Sub

   Private Function LoadCurrFile() As Boolean
      ' Costruisco path completa del componente
      Dim sPath = IO.Path.Combine(m_sCurrDir, m_sCurrFile)
      ' abilito bottone Ok
      PhotoImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri(sPath, UriKind.Absolute))
      OkBtn.IsEnabled = True
      m_bFileOk = True
      Return True
   End Function

   Private Sub OkBtn_Click(sender As Object, e As RoutedEventArgs) Handles OkBtn.Click
      ' Istruzioni per chiudere ImportPageUC e aprire CadCutPageUC
      m_MainWindow.MainWindowGrid.Children.Remove(m_MainWindow.m_PhotoPage)
      m_MainWindow.MainWindowGrid.Children.Add(m_MainWindow.m_CurrentProjectPageUC)
      m_MainWindow.m_ActivePage = m_MainWindow.m_PrevActivePage
      ' Lancio caricamento nuova foto
      If m_bFileOk Then
         Dim sPhoto As String = m_sCurrDir & "\" & m_sCurrFile
         Dim sContour As String = String.Empty
         If m_MainWindow.m_Camera.GetCalcContour() Then
            sContour = Path.ChangeExtension(sPhoto, "dxf")
         End If
         m_MainWindow.m_CadCutPageUC.PostPhoto(sPhoto, sContour)
      End If
   End Sub

   Private Sub ExitBtn_Click(sender As Object, e As RoutedEventArgs) Handles ExitBtn.Click
      ' Istruzioni per chiudere ImportPageUC e aprire CadCutPageUC
      m_MainWindow.MainWindowGrid.Children.Remove(m_MainWindow.m_PhotoPage)
      m_MainWindow.MainWindowGrid.Children.Add(m_MainWindow.m_CurrentProjectPageUC)
      m_MainWindow.m_ActivePage = m_MainWindow.m_PrevActivePage
   End Sub

   Private Sub PhotoPage_Unloaded(sender As Object, e As RoutedEventArgs)
   End Sub

End Class
