Imports System.Collections.ObjectModel
Imports EgtUILib

Public Class ChooseTestToolWD

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

   Private m_SetUpToolList As New ObservableCollection(Of TestTool)

   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(90945)              ' Selezionare l'utensile da tastare
   End Sub

   Private Sub OpenFile_Loaded(sender As Object, e As EventArgs) Handles Me.Loaded
      ' carico elenco degli utensili impostati attualmente in macchina (anche il laser!)
      LoadSetUpTool()
      ' ricerco l'utensile della lavorazione indicata
      Dim Item As TestTool = m_SetUpToolList.FirstOrDefault(Function(x) x.ToolName = m_MainWindow.m_CadCutPageUC.m_NestPage.m_CurrToolFromSelectedSawCurv)
      Dim Index As Integer = m_SetUpToolList.IndexOf(Item)
      If Index < 0 Then
         Index = 0
      End If
      ' se presente seleziono il primo elemento
      If m_SetUpToolList.Count > 0 Then
         SetUpToolListBox.SelectedItem = m_SetUpToolList(Index)
         OkBtn.IsEnabled = True
      Else
         OkBtn.IsEnabled = False
      End If
   End Sub

   Private Function LoadSetUpTool() As Boolean
      Dim sNameTool As String = String.Empty
      Dim sHeadTool As String = String.Empty
      Dim nExitTool As Integer = 0

      'sNameTool = m_MainWindow.m_CurrentMachine.sCurrSaw
      'If Not String.IsNullOrEmpty(sNameTool) Then
      '   ' Imposto la lama
      '   m_SetUpToolList.Add(New TestTool(sNameTool, sNameTool, True))
      'End If
      'sNameTool = m_MainWindow.m_CurrentMachine.sCurrMill
      'If Not String.IsNullOrEmpty(sNameTool) Then
      '   ' Imposto la fresa
      '   m_SetUpToolList.Add(New TestTool(sNameTool, sNameTool, False))
      'End If
      'sNameTool = m_MainWindow.m_CurrentMachine.sCurrDrill
      'If Not String.IsNullOrEmpty(sNameTool) Then
      '   ' Imposto il foretto
      '   m_SetUpToolList.Add(New TestTool(sNameTool, sNameTool, False))
      'End If

      sNameTool = "Laser point"
      If Not String.IsNullOrEmpty(sNameTool) Then
         ' Imposto laser di puntamento
         m_SetUpToolList.Add(New TestTool(sNameTool, ""))
      End If

      sNameTool = m_MainWindow.m_CurrentMachine.sCurrWaterJet
      If Not String.IsNullOrEmpty(sNameTool) Then
         ' Imposto WJ
         m_SetUpToolList.Add(New TestTool(sNameTool, sNameTool))
      End If

      Select Case m_MainWindow.m_CurrentMachine.MountedToolConfig
         Case CurrentMachine.MountedToolConfigs.SAW
            sNameTool = m_MainWindow.m_CurrentMachine.sCurrSaw
            If Not String.IsNullOrEmpty(sNameTool) Then
               ' Imposto la lama
               m_SetUpToolList.Add(New TestTool(sNameTool, sNameTool))
            End If
         Case CurrentMachine.MountedToolConfigs.SAWANDAUXTOOL
            sNameTool = m_MainWindow.m_CurrentMachine.sCurrSaw
            If Not String.IsNullOrEmpty(sNameTool) Then
               ' Imposto la lama
               m_SetUpToolList.Add(New TestTool(sNameTool, sNameTool))
            End If
            sNameTool = m_MainWindow.m_CurrentMachine.sCurrMill
            If Not String.IsNullOrEmpty(sNameTool) Then
               ' Imposto la fresa
               m_SetUpToolList.Add(New TestTool(sNameTool, sNameTool))
            End If
            sNameTool = m_MainWindow.m_CurrentMachine.sCurrDrill
            If Not String.IsNullOrEmpty(sNameTool) Then
               ' Imposto il foretto
               m_SetUpToolList.Add(New TestTool(sNameTool, sNameTool))
            End If

         Case CurrentMachine.MountedToolConfigs.MANUALTOOLCHANGER
            sNameTool = m_MainWindow.m_CurrentMachine.sCurrSaw
            If Not String.IsNullOrEmpty(sNameTool) Then
               ' Imposto la lama
               m_SetUpToolList.Add(New TestTool(sNameTool, sNameTool))
            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))
               sNameTool = ToolChangerPos.sTool
               If Not String.IsNullOrEmpty(sNameTool) Then
                  ' Imposto la lama
                  m_SetUpToolList.Add(New TestTool(sNameTool, sNameTool))
               End If
            Next
            Return True

         Case CurrentMachine.MountedToolConfigs.TOOLCHANGER
            ' Recupero tutti gli utensili attrezzati (nel ToolChanger e nel ManualToolChanger)
            sNameTool = m_MainWindow.m_CurrentMachine.sCurrSaw
            If Not String.IsNullOrEmpty(sNameTool) Then
               ' Imposto la lama
               m_SetUpToolList.Add(New TestTool(sNameTool, sNameTool))
            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))
                  sNameTool = ToolChangerPos.sTool
                  If Not String.IsNullOrEmpty(sNameTool) Then
                     ' Imposto la lama
                     m_SetUpToolList.Add(New TestTool(sNameTool, sNameTool))
                  End If
               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))
                  sNameTool = ToolChangerPos.sTool
                  If Not String.IsNullOrEmpty(sNameTool) Then
                     ' Imposto la lama
                     m_SetUpToolList.Add(New TestTool(sNameTool, sNameTool))
                  End If
               End If
            Next
            Return True

         Case CurrentMachine.MountedToolConfigs.TOOLCHANGERWITHSAW
            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))
                  sNameTool = ToolChangerPos.sTool
                  If Not String.IsNullOrEmpty(sNameTool) Then
                     ' Imposto la lama
                     m_SetUpToolList.Add(New TestTool(sNameTool, sNameTool))
                  End If
               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))
                  sNameTool = ToolChangerPos.sTool
                  If Not String.IsNullOrEmpty(sNameTool) Then
                     ' Imposto la lama
                     m_SetUpToolList.Add(New TestTool(sNameTool, sNameTool))
                  End If
               End If
            Next
            Return True

         Case Else
            Return False
      End Select


      Return True
   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

   ' restituisce l'oggetto selezionato in elenco e lo imposta come attivo
   Friend Function GetSelectedTool() As TestTool
      Dim SelTestTool As TestTool = DirectCast(SetUpToolListBox.SelectedItem, TestTool)
      If SelTestTool.ToolAlias = "Laser point" Then
         SelTestTool.ToolHead = "H3"
         SelTestTool.ToolExit = 1
         SelTestTool.ToolIsSaw = False
         SelTestTool.ToolIsLaser = True
      Else
         EgtTdbSetCurrTool(SelTestTool.ToolName)
         Dim sHeadTool As String = String.Empty
         Dim nExitTool As Integer = 0
         Dim sTypeTool As String = String.Empty
         Dim nTypeTool As Integer
         EgtTdbGetCurrToolParam(MCH_TP.TYPE, nTypeTool)
         EgtTdbGetCurrToolParam(MCH_TP.HEAD, sHeadTool)
         EgtTdbGetCurrToolParam(MCH_TP.EXIT_, nExitTool)
         EgtTdbGetCurrToolParam(MCH_TP.TYPE, sTypeTool)
         SelTestTool.ToolHead = sHeadTool
         SelTestTool.ToolExit = nExitTool
         SelTestTool.ToolType = nTypeTool
         SelTestTool.ToolIsSaw = (nTypeTool = MCH_TY.SAW_STD)
         SelTestTool.ToolIsLaser = False
      End If

      Return SelTestTool
   End Function

End Class

Public Class TestTool

   Private m_ToolAlias As String = String.Empty
   Private m_ToolName As String = String.Empty
   Private m_ToolHead As String = "H1"
   Private m_ToolExit As Integer = 1
   Private m_ToolIsSaw As Boolean = False
   Private m_ToolIsLaser As Boolean = False
   Private m_ToolType As Integer = -1

   Public Property ToolAlias As String
      Get
         Return m_ToolAlias
      End Get
      Set(value As String)
         m_ToolAlias = value
      End Set
   End Property

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

   Public Property ToolHead As String
      Get
         Return m_ToolHead
      End Get
      Set(value As String)
         m_ToolHead = value
      End Set
   End Property

   Public Property ToolExit As Integer
      Get
         Return m_ToolExit
      End Get
      Set(value As Integer)
         m_ToolExit = value
      End Set
   End Property

   Public Property ToolType As Integer
      Get
         Return m_ToolType
      End Get
      Set(value As Integer)
         m_ToolType = value
      End Set
   End Property

   Public Property ToolIsSaw As Boolean
      Get
         Return m_ToolIsSaw
      End Get
      Set(value As Boolean)
         m_ToolIsSaw = value
      End Set
   End Property

   Public Property ToolIsLaser As Boolean
      Get
         Return m_ToolIsLaser
      End Get
      Set(value As Boolean)
         m_ToolIsLaser = value
      End Set
   End Property

   Sub New(sToolAlias As String, sToolName As String)
      m_ToolAlias = sToolAlias
      m_ToolName = sToolName
   End Sub

End Class
