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

Public Class ChooseToolWD

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

   Private m_SetUpToolList As New ObservableCollection(Of ToolPos)

   Sub New(Owner As Window)
      Me.Owner = Owner
      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
      SetUpToolListBox.ItemsSource = m_SetUpToolList
      FilePathTxBl.Text = EgtMsg(MSG_ALARMSPAGEUC + 45)              ' Selezionare l'utensile da tastare
   End Sub

   Private Sub OpenFile_Loaded(sender As Object, e As EventArgs) Handles Me.Loaded
      ' carico direttorio
      LoadSetUpTool()
      ' se presente seleziono il primo elemento
      If m_SetUpToolList.Count > 0 Then
         SetUpToolListBox.SelectedItem = m_SetUpToolList(0)
         OkBtn.IsEnabled = True
      Else
         OkBtn.IsEnabled = False
      End If
   End Sub

   Private Function LoadSetUpTool() As Boolean
      Select Case m_MainWindow.m_CurrentMachine.MountedToolConfig
         Case CurrentMachine.MountedToolConfigs.SAWANDAUXTOOL
            m_SetUpToolList.Add(New ToolPos(m_MainWindow.m_CurrentMachine.sCurrSaw, "T100", True))
            If Not String.IsNullOrWhiteSpace(m_MainWindow.m_CurrentMachine.sCurrMill) Then
               m_SetUpToolList.Add(New ToolPos(m_MainWindow.m_CurrentMachine.sCurrMill, "T100", False))
            End If
            If Not String.IsNullOrWhiteSpace(m_MainWindow.m_CurrentMachine.sCurrDrill) Then
               m_SetUpToolList.Add(New ToolPos(m_MainWindow.m_CurrentMachine.sCurrDrill, "T100", False))
            End If
            Return True
         Case CurrentMachine.MountedToolConfigs.MANUALTOOLCHANGER
            If m_MainWindow.m_CurrentMachine.bSaw And Not String.IsNullOrWhiteSpace(m_MainWindow.m_CurrentMachine.sCurrSaw) Then
               EgtTdbSetCurrTool(m_MainWindow.m_CurrentMachine.sCurrSaw)
               Dim sToolPos As String = String.Empty
               EgtTdbGetCurrToolParam(MCH_TP.TCPOS, sToolPos)
               m_SetUpToolList.Add(New ToolPos(m_MainWindow.m_CurrentMachine.sCurrSaw, sToolPos, True))
            End If
            ' Recupero tutti gli utensili attrezzati (nel ToolChanger e nel ManualToolChanger)
            For Each ToolChangerPos As ToolChangerPos In m_MainWindow.m_CurrentMachine.ManualToolChanger
               m_SetUpToolList.Add(New ToolPos(ToolChangerPos.sTool, ToolChangerPos.sName, False))
            Next
            Return True
         Case CurrentMachine.MountedToolConfigs.TOOLCHANGER
            ' Recupero tutti gli utensili attrezzati (nel ToolChanger e nel ManualToolChanger)
            If m_MainWindow.m_CurrentMachine.bSaw And Not String.IsNullOrWhiteSpace(m_MainWindow.m_CurrentMachine.sCurrSaw) Then
               EgtTdbSetCurrTool(m_MainWindow.m_CurrentMachine.sCurrSaw)
               Dim sToolPos As String = String.Empty
               EgtTdbGetCurrToolParam(MCH_TP.TCPOS, sToolPos)
               m_SetUpToolList.Add(New ToolPos(m_MainWindow.m_CurrentMachine.sCurrSaw, sToolPos, True))
            End If
            For Each ToolChangerPos As ToolChangerPos In m_MainWindow.m_CurrentMachine.ToolChanger
               If Not String.IsNullOrWhiteSpace(ToolChangerPos.sTool) Then
                  m_SetUpToolList.Add(New ToolPos(ToolChangerPos.sTool, ToolChangerPos.sName, False))
               End If
            Next
            For Each ToolChangerPos As ToolChangerPos In m_MainWindow.m_CurrentMachine.ManualToolChanger
               If Not String.IsNullOrWhiteSpace(ToolChangerPos.sTool) Then
                  m_SetUpToolList.Add(New ToolPos(ToolChangerPos.sTool, ToolChangerPos.sName, False))
               End If
            Next
            Return True
         Case Else
            Return False
      End Select
   End Function

   Private Sub SetUpToolListBox_PreviewMouseUp(sender As Object, e As MouseButtonEventArgs) Handles SetUpToolListBox.PreviewMouseUp
      ' Disabilito Ok
      OkBtn.IsEnabled = False
      ' Recupero item selezionato
      If SetUpToolListBox.SelectedItems.Count() = 0 Then
         Return
      End If
      ' A seconda del tipo
      OkBtn.IsEnabled = True
   End Sub

   Private Sub SetUpToolListBox_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles SetUpToolListBox.SelectionChanged
      ' Disabilito Ok
      OkBtn.IsEnabled = False
      ' Recupero item selezionato
      If SetUpToolListBox.SelectedItems.Count() = 0 Then
         Return
      Else
         OkBtn.IsEnabled = True
      End If
   End Sub

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

   Friend Function GetSelectedTool() As ToolPos
      Return DirectCast(SetUpToolListBox.SelectedItem, ToolPos)
   End Function

End Class

Public Class ToolPos

   Friend m_ToolName As String
   Public Property ToolName As String
      Get
         Return m_ToolName
      End Get
      Set(value As String)
         m_ToolName = value
      End Set
   End Property

   Friend m_ToolPos As String
   Friend m_IsSaw As Boolean

   Sub New(sToolName As String, sToolPos As String, bIsSaw As Boolean)
      m_ToolName = sToolName
      m_ToolPos = sToolPos
      m_IsSaw = bIsSaw
   End Sub

End Class