Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports EgtUILib
Imports EgtWPFLib

Public Class PolishingsPageUC

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

   ' Variabile che conserva la lucidatura precedentemente selezionata, usata nel caso si selezioni annulla quando si cambia lucidatura selezionata
   Private m_OldItem As Object

   ' File kits di lucidatura
   Private m_sKitFile As String = String.Empty

   Private m_KitList As New ObservableCollection(Of Kit)

   Private m_KitMachList As New ObservableCollection(Of KitMach)

   ' Lista che contiene gli utensili disponibili (per Combobox in ciascun KitMachItem)
   Private m_ToolList As New ObservableCollection(Of String)

   ' Lista che contiene gli UUID degli utensili disponibili nello stesso ordine della lista dei nomi (ComboBox) per la scrittura degli stessi sull'INI
   Private m_ToolUuidList As New ObservableCollection(Of String)

   Private m_PreviousSelectedKitIndex As Integer = 0

   Private m_PreviousSelectedKitMachIndex As Integer = 0

   Public ReadOnly Property ToolList As ObservableCollection(Of String)
      Get
         Return m_ToolList
      End Get
   End Property

   Public ReadOnly Property ToolUuidList As ObservableCollection(Of String)
      Get
         Return m_ToolUuidList
      End Get
   End Property

   Private Sub PolishingsPage_Initialized(sender As Object, e As EventArgs)

      ' Impostazione Path KitIni file
      m_sKitFile = m_MainWindow.GetKitsFile()

      ' Imposto i messaggi letti dal file dei messaggi
      NewKitBtn.Content = EgtMsg(MSG_MACHINEPAGEUC + 16)
      SaveKitBtn.Content = EgtMsg(MSG_MACHINEPAGEUC + 17)
      RemoveKitBtn.Content = EgtMsg(MSG_MACHINEPAGEUC + 18)

      NewKitMachBtn.Content = EgtMsg(MSG_MACHINEPAGEUC + 16)
      RemoveKitMachBtn.Content = EgtMsg(MSG_MACHINEPAGEUC + 18)
      MoveUpKitMachBtn.Content = EgtMsg(91098)
      MoveDownKitMachBtn.Content = EgtMsg(91099)

      KitsGpBx.Header = EgtMsg(91091)             ' Lucidature
      KitMachsGpBx.Header = EgtMsg(91096)         ' Lista utensili
      MotionTxBl.Text = EgtMsg(91092)             ' Movimento
      RepeatTxBl.Text = EgtMsg(91093)             ' Ripetizioni
      StepTxBl.Text = EgtMsg(90787)               ' Passo
      OffSetTxBl.Text = EgtMsg(91089)             ' Offset
      RadiusTxBl.Text = "Raggio"
      DistanceTxBl.Text = "Distanza"
      LiLoGpBx.Header = EgtMsg(91094)             ' Attacco/Uscita
      LiLenTxBl.Text = EgtMsg(91097)              ' Lunghezza
      LiHeightTxBl.Text = EgtMsg(91095)           ' Altezza
      LiLoadTxBl.Text = EgtMsg(91100)             ' Precarico

   End Sub

   Private Sub PolishingsPage_Loaded(sender As Object, e As RoutedEventArgs)
      EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())

      LoadPage()

   End Sub

   Private Sub LoadPage()

      ' Leggo utensili dal Db
      m_ToolList.Clear()
      Dim sToolName As String = String.Empty
      Dim nType As Integer = MCH_TY.NONE
      Dim bOk As Boolean = EgtTdbGetFirstTool( MCH_TF.MILL, sToolName, nType)
      While bOk
         If nType = MCH_TY.MILL_POLISHING Then
            m_ToolList.Add( sToolName)
            ' Recupero l'UUID dell' utensile
            EgtTdbSetCurrTool( sToolName)
            Dim sToolUuid As String = String.Empty
            EgtTdbGetCurrToolParam( MCH_TP.UUID, sToolUuid)
            ToolUuidList.Add( sToolUuid)
         End If
         ' Passo al successivo
         bOk = EgtTdbGetNextTool( MCH_TF.MILL, sToolName, nType)
      End While

      ' Leggo la lista dei kit
      m_KitList.Clear()
      m_KitMachList.Clear()
      Dim Kit As Kit = Nothing
      Dim nKitIndex As Integer = 1
      While ReadKit( nKitIndex, False, Kit)
         m_KitList.Add( Kit)
         nKitIndex += 1
      End While

      KitsLstBx.ItemsSource = m_KitList

      KitMachsLstBx.ItemsSource = m_KitMachList

      KitMach.m_ToolList = ToolList.ToList()

      ' Recupero nome utensile tramite UUID
      Dim ToolString As String = String.Empty
      For Each KitItem As Kit In m_KitList
         For Each KitMachItem As KitMach In KitItem.KitMachList
            Dim selToolIndex As Integer = 0
            EgtTdbGetToolFromUUID(KitMachItem.sToolUUID, ToolString)
            Dim bToolExist As Boolean = False
            For Each CurrTool As String In ToolList
               If CurrTool.ToString() = ToolString Then
                  bToolExist = True
                  Exit For
               End If
               selToolIndex += 1
            Next
            If bToolExist Then
               KitMachItem.SelTool = selToolIndex
            Else
               KitMachItem.SelTool = -1
            End If
         Next
      Next

      If KitsLstBx.Items.Count > 0 Then
         ' seleziono il primo elemento della lista dei Kit
         KitsLstBx.SelectedItem = m_KitList( m_PreviousSelectedKitIndex)
         m_OldItem = KitsLstBx.SelectedItem

         ' carico la lista dei KitMach relativi al Kit selezionato
         For Each kmItem As KitMach In KitsLstBx.SelectedItem.KitMachList
            Dim KitMachItem = New KitMach( kmItem)
            m_KitMachList.Add(KitMachItem)
         Next
         ' Verifico che l'indice del KitMach da selezionare rientri nell'intervallo degli indici esistenti altrimenti lo setto a 0
         Dim kitMachIndex As Integer = m_PreviousSelectedKitMachIndex
         If kitMachIndex > KitMachsLstBx.Items.Count Then
            kitMachIndex = 0
         End If
         KitMachsLstBx.SelectedItem = m_KitMachList(kitMachIndex)
         ' visualizzo i parametri relativi al KitMach selezionato
         ShowKitMach( m_KitMachList( kitMachIndex))
      End If

      If KitMachsLstBx.Items.Count = 0 Then RemoveKitMachBtn.IsEnabled = False
      If KitsLstBx.Items.Count = 0 Then RemoveKitBtn.IsEnabled = False

      IsModifiedReset()

   End Sub

   Private Sub NewKitBtn_Click(sender As Object, e As RoutedEventArgs) Handles NewKitBtn.Click
      ' Se ci sono state modifiche chiedo se si desidera salvarle
      If Not SaveKit() Then
         KitsLstBx.SelectedItem = m_OldItem
         Exit Sub
      End If
      ' Mostro textbox per il nome
      KitNameTxBx.Text = String.Empty
      KitNameTxBx.Visibility = Windows.Visibility.Visible
      KitsLstBx.SetValue( Grid.RowSpanProperty, 1)
   End Sub

   Private Sub KitNameTxBx_EgtClosed(sender As Object, e As EventArgs) Handles KitNameTxBx.EgtClosed
      ' Verifico che il nome non sia vuoto
      If Not String.IsNullOrWhiteSpace( KitNameTxBx.Text) Then
         ' Verifico che il nome non sia già utilizzato
         Dim bNameExist As Boolean = False
         For Each Kit As Kit In m_KitList
            If Kit.sName = KitNameTxBx.Text Then
               bNameExist = True
               Exit For
            End If
         Next
         If Not bNameExist Then
            ' Aggiungo un Kit con Id uguale al numero di Kit già presenti + 1 e nome appena immesso
            AddKit( KitNameTxBx.Text, m_PreviousSelectedKitIndex)
         End If
      End If

      RemoveKitBtn.IsEnabled = True
      RemoveKitMachBtn.IsEnabled = True

      m_PreviousSelectedKitIndex = m_KitList.Count - 1

      LoadPage()

      ' Nascondo textbox per il nome
      KitsLstBx.SetValue( Grid.RowSpanProperty, 2)
      KitNameTxBx.Visibility = Windows.Visibility.Hidden
   End Sub

   Private Sub SaveBtn_Click(sender As Object, e As RoutedEventArgs) Handles SaveKitBtn.Click
      If IsNothing( KitsLstBx.SelectedItem) Then Return
      ' Salvo il Kit correntemente selezionato
      Save( KitsLstBx.SelectedItem.nId)
   End Sub

   Private Sub Save( nKitIndex As Integer)

      Dim kitCurr = m_KitList( nKitIndex - 1)

      ' Salvo e scrivo tutti i KitMach del Kit che ha Id uguale all'argomento passato alla funzione Save
      Dim nKitMachIndex As Integer = 1
      For Each kmlbCurr As Object In KitMachsLstBx.Items
         ' Recupero i dati
         Dim kmCurr As KitMach = kitCurr.KitMachList( nKitMachIndex - 1)
         kmCurr.SelTool = kmlbCurr.SelTool
         If kmCurr.SelTool >= 0 Then
            kmCurr.sToolName = ToolList( kmCurr.SelTool)
            kmCurr.sToolUUID = ToolUuidList( kmCurr.SelTool)
         Else
            kmCurr.sToolName = "---"
            kmCurr.sToolUUID = "---"
         End If
         kmCurr.bActive = kmlbCurr.bActive
         kmCurr.nContour = kmlbCurr.nContour
         kmCurr.nZigZagX = kmlbCurr.nZigZagX
         kmCurr.dStepX = kmlbCurr.dStepX
         kmCurr.nZigZagY = kmlbCurr.nZigZagY
         kmCurr.dStepY = kmlbCurr.dStepY
         kmCurr.dLiLen = kmlbCurr.dLiLen
         kmCurr.dLiHeight = kmlbCurr.dLiHeight
         ' Salvo i dati
         WriteKitMach( kitCurr.nId, nKitMachIndex, kmCurr)
         nKitMachIndex += 1
      Next

      IsModifiedReset()
   End Sub

   Private Sub RemoveKitBtn_Click(sender As Object, e As RoutedEventArgs) Handles RemoveKitBtn.Click
      If IsNothing( KitsLstBx.SelectedItem) Then Return
      ' Rimuovo il Kit correntemente selezionato
      RemoveKit( KitsLstBx.SelectedItem.nId)
   End Sub

   Private Sub RemoveKitMachBtn_Click(sender As Object, e As RoutedEventArgs) Handles RemoveKitMachBtn.Click
      Dim SelectedKit As Kit = DirectCast(KitsLstBx.SelectedItem, Kit)
      Dim SelectedKitMach As KitMach = DirectCast(KitMachsLstBx.SelectedItem, KitMach)
      ' Se sono presenti più di 1 KitMach elimina quello selezionato altrimenti elimina l'intero Kit
      If KitMachsLstBx.Items.Count > 1 Then
         If Not IsNothing( SelectedKitMach) Then
            RemoveKitMach( SelectedKit, SelectedKitMach.nId)
         End If
      Else
         RemoveKit( SelectedKit.nId)
      End If
   End Sub

   Private Sub MoveUpKitMachBtn_Click( sender As Object, e As RoutedEventArgs) Handles MoveUpKitMachBtn.Click

      ' Kit corrente
      Dim kitCurr As Kit = DirectCast( KitsLstBx.SelectedItem, Kit)
      If IsNothing( kitCurr) Then Return
      
      ' Lavorazione corrente del kit corrente
      Dim kmCurr As KitMach = DirectCast( KitMachsLstBx.SelectedItem, KitMach)
      If IsNothing( kmCurr) Then Return
      Dim nkmCurrIdx As Integer = kmCurr.nId

      ' Se lavorazione è prima della lista, non devo fare alcunchè
      If nkmCurrIdx = 0 Then Return

      ' Riscrivo i parametri del KitMach precedente con gli indici del KitMach selezionato
      Dim kmPrev As KitMach = KitMachsLstBx.Items( kmCurr.nId - 2)
      WriteKitMach( kitCurr.nId, nkmCurrIdx, kmPrev)

      ' Riscrivo i parametri del KitMach selezionato con gli indici del KitMach precedente
      Dim nkmPrevIdx As Integer = kmCurr.nId - 1
      WriteKitMach( kitCurr.nId, nkmPrevIdx, kmCurr)

      ' Aggiorno selezioni
      m_PreviousSelectedKitIndex = KitsLstBx.SelectedItem.nId - 1
      m_PreviousSelectedKitMachIndex = KitMachsLstBx.SelectedItem.nId - 2

      ' Ricarico tutto
      LoadPage()

   End Sub

   Private Sub MoveDownKitMachBtn_Click(sender As Object, e As RoutedEventArgs) Handles MoveDownKitMachBtn.Click

      ' Kit corrente
      Dim kitCurr As Kit = DirectCast( KitsLstBx.SelectedItem, Kit)
      If IsNothing( kitCurr) Then Return
      
      ' Lavorazione corrente del kit corrente
      Dim kmCurr As KitMach = DirectCast( KitMachsLstBx.SelectedItem, KitMach)
      If IsNothing( kmCurr) Then Return
      Dim nkmCurrIdx As Integer = kmCurr.nId

      ' Se lavorazione è ultima della lista, non devo fare alcunchè
      If nkmCurrIdx = KitMachsLstBx.Items.Count Then Return

      ' Riscrivo i parametri del KitMach seguente con gli indici del KitMach selezionato
      Dim kmNext As KitMach = KitMachsLstBx.Items( kmCurr.nId)
      WriteKitMach( kitCurr.nId, nkmCurrIdx, kmNext)

      ' Riscrivo i parametri del KitMach selezionato con gli indici del KitMach seguente
      Dim nkmNextIdx As Integer = kmCurr.nId + 1
      WriteKitMach( kitCurr.nId, nkmNextIdx, kmCurr)

      ' Aggiorno selezioni
      m_PreviousSelectedKitIndex = KitsLstBx.SelectedItem.nId - 1
      m_PreviousSelectedKitMachIndex = KitMachsLstBx.SelectedItem.nId

      ' Ricarico tutto
      LoadPage()

   End Sub

   Private Sub NewKitMachBtn_Click(sender As Object, e As RoutedEventArgs) Handles NewKitMachBtn.Click
      ' Se ci sono state modifiche chiedo se si desidera salvarle
      If Not SaveKit() Then
         KitsLstBx.SelectedItem = m_OldItem
         Exit Sub
      End If

      If Not IsNothing( KitsLstBx.SelectedItem) Then
         ' Scrivo nuova lavorazione
         Dim nKitIdx As Integer = m_KitList(KitsLstBx.SelectedIndex).nId
         Dim nMachKitIdx As Integer = m_KitMachList.Count + 1
         WriteKitMach( nKitIdx, nMachKitIdx, New KitMach( nMachKitIdx))
         ' Ricarico
         m_PreviousSelectedKitIndex = KitsLstBx.SelectedItem.nId - 1
         LoadPage()
      End If
   End Sub

   Private Sub AddKit( sNewKitName As String, nCurrKitIndex As Integer)
      Dim kitNew As Kit = Nothing
      If nCurrKitIndex >= 0 And nCurrKitIndex < m_KitList.Count() Then
         ' Creo il Kit come copia di quello corrente
         kitNew = New Kit( m_KitList.Count + 1, sNewKitName, m_KitList( nCurrKitIndex))
      Else
         ' Creo il Kit e aggiungo un KitMach vuoto
         kitNew = New Kit( m_KitList.Count + 1, sNewKitName)
         kitNew.KitMachList.Add( New KitMach( 1))
      End If
      ' Inserisco in lista
      m_KitList.Add( kitNew)
      ' Eseguo il salvataggio
      WriteKitName( kitNew.nId, kitNew.sName)
      For Each kmCurr As KitMach In kitNew.KitMachList
         WriteKitMach( kitNew.nId, kmCurr.nId, kmCurr)
      Next

   End Sub

   Private Sub RemoveKit( nKitIndex As Integer)
      ' Rimuovo il Kit dall'elenco
      For Index As Integer = 0 To m_KitList.Count - 1
         If m_KitList( Index).nId = nKitIndex Then
            m_KitList.RemoveAt( Index)
            Exit For
         End If
      Next
      ' Tolgo il kit dal file
      EraseKit( nKitIndex)

      ' Ricarico tutto
      m_PreviousSelectedKitIndex = 0
      LoadPage()

   End Sub

   Friend Sub RemoveKitMach( KitValue As Kit, nKitMachIndex As Integer)
      ' Rimuovo il Kit dall'elenco
      Dim Index As Integer
      For Index = 0 To m_KitMachList.Count - 1
         If m_KitMachList( Index).nId = nKitMachIndex Then
            m_KitMachList.RemoveAt( Index)
            Exit For
         End If
      Next
      KitValue.KitMachList.RemoveAt( Index)

      ' Tolgo la lavorazione dal file
      EraseKitMach( KitValue.nId, nKitMachIndex)

      ' Ricarico tutto
      m_PreviousSelectedKitIndex = KitsLstBx.SelectedItem.nId - 1
      m_PreviousSelectedKitMachIndex = 0
      LoadPage()

   End Sub

   Private Sub KitsLstBx_PreviewMouseUp(sender As Object, e As MouseButtonEventArgs) Handles KitsLstBx.PreviewMouseUp

      If Not SaveKit() Then
         KitsLstBx.SelectedItem = m_OldItem
         Exit Sub
      End If

      Dim SelectedKit As Kit = KitsLstBx.SelectedItem
      If Not IsNothing(SelectedKit) Then
         ' resetto la lista
         m_KitMachList.Clear()
         ' carico la lista dei KitMach relativi al Kit selezionato
         For Each kmItem As KitMach In KitsLstBx.SelectedItem.KitMachList
            Dim KitMachItem = New KitMach( kmItem)
            m_KitMachList.Add( KitMachItem)
         Next
         ' seleziono il primo elemento della lista dei Kit
         KitMachsLstBx.SelectedItem = m_KitMachList( 0)
         ' visualizzo i parametri relativi al KitMach selezionato
         ShowKitMach( m_KitMachList( 0))

         m_OldItem = KitsLstBx.SelectedItem

         IsModifiedReset()
      End If

   End Sub

   Friend Function SaveKit() As Boolean
      ' Se non modificato non va fatto alcunché
      If Not IsModified() Then Return True
      ' Chiedo cosa fare ed eseguo
      Dim SaveCurrKitWnd As New EgtMsgBox( m_MainWindow, "", EgtMsg(91124), EgtMsgBox.Buttons.YES_NO_CANCEL, EgtMsgBox.Icons.NULL)
      Select Case SaveCurrKitWnd.m_nPressedBtn
         Case 1    ' Si
            Save( m_OldItem.nId)
            Return True
         Case 2    ' No
            m_PreviousSelectedKitIndex = KitsLstBx.SelectedItem.nId - 1
            LoadPage()
            Return True
         Case Else    ' Annulla
            Return False
      End Select
   End Function

   Private ReadOnly Property IsModified() As Boolean
      Get
         If Not IsNothing(m_OldItem) Then
            Dim nKitIndex As Integer = m_OldItem.nId
            If nKitIndex <= m_KitList.Count Then
               For Each KitMachItem As KitMach In m_KitList(nKitIndex - 1).KitMachList
                  If KitMachItem.m_IsModifiedId OrElse KitMachItem.m_IsModifiedToolUUID OrElse KitMachItem.m_IsModifiedToolName OrElse KitMachItem.m_IsModifiedSelTool OrElse
                     KitMachItem.m_IsModifiedActive OrElse KitMachItem.m_IsModifiedContour OrElse KitMachItem.m_IsModifiedOffCnt OrElse KitMachItem.m_IsModifiedZigZagX OrElse
                     KitMachItem.m_IsModifiedOffZigZagX OrElse KitMachItem.m_IsModifiedZigZagY OrElse KitMachItem.m_IsModifiedStepX OrElse KitMachItem.m_IsModifiedStepY OrElse
                     KitMachItem.m_IsModifiedOffZigZagY OrElse KitMachItem.m_IsModifiedSpiral OrElse KitMachItem.m_IsModifiedOffSpiral OrElse KitMachItem.m_IsModifiedStepSpiral OrElse
                     KitMachItem.m_IsModifiedRadiusSpiral OrElse KitMachItem.m_IsModifiedDistanceSpiral OrElse KitMachItem.m_IsModifiedLiLen OrElse KitMachItem.m_IsModifiedLiHeight OrElse
                     KitMachItem.m_IsModifiedLiLoad Then
                     Return True
                  End If
               Next
            End If
         End If
         Return False
      End Get
   End Property

   Private Sub IsModifiedReset()
      If IsNothing( m_OldItem) Then Return
      Dim nKitIndex As Integer = m_OldItem.nId
      If nKitIndex <= m_KitList.Count Then
         For Each KitMachItem As KitMach In m_KitList(nKitIndex - 1).KitMachList
            KitMachItem.m_IsModifiedId = False
            KitMachItem.m_IsModifiedToolUUID = False
            KitMachItem.m_IsModifiedToolName = False
            KitMachItem.m_IsModifiedSelTool = False
            KitMachItem.m_IsModifiedActive = False
            KitMachItem.m_IsModifiedContour = False
            KitMachItem.m_IsModifiedOffCnt = False
            KitMachItem.m_IsModifiedZigZagX = False
            KitMachItem.m_IsModifiedOffZigZagX = False
            KitMachItem.m_IsModifiedRadiusX = False
            KitMachItem.m_IsModifiedDistanceX = False
            KitMachItem.m_IsModifiedZigZagY = False
            KitMachItem.m_IsModifiedOffZigZagY = False
            KitMachItem.m_IsModifiedRadiusY = False
            KitMachItem.m_IsModifiedDistanceY = False
            KitMachItem.m_IsModifiedSpiral = False
            KitMachItem.m_IsModifiedOffSpiral = False
            KitMachItem.m_IsModifiedRadiusSpiral = False
            KitMachItem.m_IsModifiedDistanceSpiral = False
            KitMachItem.m_IsModifiedStepX = False
            KitMachItem.m_IsModifiedStepY = False
            KitMachItem.m_IsModifiedStepSpiral = False
            KitMachItem.m_IsModifiedLiLen = False
            KitMachItem.m_IsModifiedLiHeight = False
            KitMachItem.m_IsModifiedLiLoad = False
         Next
      End If
   End Sub

   Private Sub ShowKitMach( kmShow As KitMach)
      ' visualizzo i parametri del KitMach
      ContourTxBx.Text = kmShow.nContour.ToString()
      OffContourTxBx.Text = LenToString(kmShow.dOffsetCnt, 3)
      ZigZagXTxBx.Text = kmShow.nZigZagX.ToString()
      OffZigZagXTxBx.Text = LenToString(kmShow.dOffZigZagX, 3)
      StepXTxBx.Text = LenToString(kmShow.dStepX, 3)
      RadiusXTxBx.Text = LenToString(kmShow.dRadiusX, 3)
      DistanceXTxBx.Text = LenToString(kmShow.dDistanceX, 3)
      ZigZagYTxBx.Text = kmShow.nZigZagY.ToString()
      OffZigZagYTxBx.Text = LenToString(kmShow.dOffZigZagY, 3)
      StepYTxBx.Text = LenToString(kmShow.dStepY, 3)
      RadiusYTxBx.Text = LenToString(kmShow.dRadiusY, 3)
      DistanceYTxBx.Text = LenToString(kmShow.dDistanceY, 3)
      SpiralTxBx.Text = kmShow.nSpiral.ToString()
      StepSpiralTxBx.Text = LenToString(kmShow.dStepSpiral, 3)
      OffSpiralTxBx.Text = LenToString(kmShow.dOffSpiral, 3)
      RadiusSpiralTxBx.Text = LenToString(kmShow.dRadiusSpiral, 3)
      DistanceSpiralTxBx.Text = LenToString(kmShow.dDistanceSpiral, 3)
      LiLenTxBx.Text = LenToString(kmShow.dLiLen, 3)
      LiHeightTxBx.Text = LenToString(kmShow.dLiHeight, 3)
      LiLoadTxBx.Text = LenToString(kmShow.dLiLoad, 3)
   End Sub

   Private Sub KitMachsLstBx_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles KitMachsLstBx.SelectionChanged
      Dim SelectedKitMach As KitMach = KitMachsLstBx.SelectedItem
      If Not IsNothing(SelectedKitMach) Then
         ' visualizzo i parametri relativi al KitMach selezionato
         ShowKitMach(SelectedKitMach)
      End If
   End Sub

   Private Sub ContourTxBx_EgtClosed(sender As Object, e As EventArgs) Handles ContourTxBx.EgtClosed
      Dim nTemp As Integer = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         nTemp = ContourTxBx.Text
         KitMachsLstBx.SelectedItem.nContour = nTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).nContour = nTemp
      End If
   End Sub

   Private Sub OffContourTxBx_EgtClosed(sender As Object, e As EventArgs) Handles OffContourTxBx.EgtClosed
      Dim dTemp As Double = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         StringToLen(OffContourTxBx.Text, dTemp)
         KitMachsLstBx.SelectedItem.dOffsetCnt = dTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).dOffsetCnt = dTemp
      End If
   End Sub

#Region "ZigZag X"

   Private Sub ZigZagXTxBx_EgtClosed(sender As Object, e As EventArgs) Handles ZigZagXTxBx.EgtClosed
      Dim nTemp As Integer = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         nTemp = ZigZagXTxBx.Text
         KitMachsLstBx.SelectedItem.nZigZagX = nTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).nZigZagX = nTemp
      End If
   End Sub

   Private Sub OffZigZagXTxBx_EgtClosed(sender As Object, e As EventArgs) Handles OffZigZagXTxBx.EgtClosed
      Dim dTemp As Double = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         StringToLen(OffZigZagXTxBx.Text, dTemp)
         KitMachsLstBx.SelectedItem.dOffZigZagX = dTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).dOffZigZagX = dTemp
      End If
   End Sub

   Private Sub StepXTxBx_EgtClosed(sender As Object, e As EventArgs) Handles StepXTxBx.EgtClosed
      Dim dTemp As Double = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         StringToLen(StepXTxBx.Text, dTemp)
         KitMachsLstBx.SelectedItem.dStepX = dTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).dStepX = dTemp
      End If
   End Sub

   Private Sub RadiusXTxBx_EgtClosed(sender As Object, e As EventArgs) Handles RadiusXTxBx.EgtClosed
      Dim dTemp As Double = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         StringToLen(RadiusXTxBx.Text, dTemp)
         KitMachsLstBx.SelectedItem.dRadiusX = dTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).dRadiusX = dTemp
      End If
   End Sub

   Private Sub DistanceXTxBx_EgtClosed(sender As Object, e As EventArgs) Handles DistanceXTxBx.EgtClosed
      Dim dTemp As Double = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         StringToLen(DistanceXTxBx.Text, dTemp)
         KitMachsLstBx.SelectedItem.dDistanceX = dTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).dDistanceX = dTemp
      End If
   End Sub

#End Region  ' ZigZag X

#Region "ZizZag Y"

   Private Sub ZigZagYTxBx_EgtClosed(sender As Object, e As EventArgs) Handles ZigZagYTxBx.EgtClosed
      Dim nTemp As Integer = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         nTemp = ZigZagYTxBx.Text
         KitMachsLstBx.SelectedItem.nZigZagY = nTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).nZigZagY = nTemp
      End If
   End Sub

   Private Sub OffZigZagYTxBx_EgtClosed(sender As Object, e As EventArgs) Handles OffZigZagYTxBx.EgtClosed
      Dim dTemp As Double = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         StringToLen(OffZigZagYTxBx.Text, dTemp)
         KitMachsLstBx.SelectedItem.dOffZigZagY = dTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).dOffZigZagY = dTemp
      End If
   End Sub

   Private Sub StepYTxBx_EgtClosed(sender As Object, e As EventArgs) Handles StepYTxBx.EgtClosed
      Dim dTemp As Double = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         StringToLen(StepYTxBx.Text, dTemp)
         KitMachsLstBx.SelectedItem.dStepY = dTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).dStepY = dTemp
      End If
   End Sub

   Private Sub RadiusYTxBx_EgtClosed(sender As Object, e As EventArgs) Handles RadiusYTxBx.EgtClosed
      Dim dTemp As Double = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         StringToLen(RadiusYTxBx.Text, dTemp)
         KitMachsLstBx.SelectedItem.dRadiusY = dTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).dRadiusY = dTemp
      End If
   End Sub

   Private Sub DistanceYTxBx_EgtClosed(sender As Object, e As EventArgs) Handles DistanceYTxBx.EgtClosed
      Dim dTemp As Double = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         StringToLen(DistanceYTxBx.Text, dTemp)
         KitMachsLstBx.SelectedItem.dDistanceY = dTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).dDistanceY = dTemp
      End If
   End Sub

#End Region    ' ZigZagY

#Region "Spirale"

   Private Sub SpiralTxBx_EgtClosed(sender As Object, e As EventArgs) Handles SpiralTxBx.EgtClosed
      Dim nTemp As Integer = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         nTemp = SpiralTxBx.Text
         KitMachsLstBx.SelectedItem.nSpiral = nTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).nSpiral = nTemp
      End If
   End Sub

   Private Sub OffSpiralTxBx_EgtClosed(sender As Object, e As EventArgs) Handles OffSpiralTxBx.EgtClosed
      Dim dTemp As Double = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         StringToLen(OffSpiralTxBx.Text, dTemp)
         KitMachsLstBx.SelectedItem.dOffSpiral = dTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).dOffSpiral = dTemp
      End If
   End Sub

   Private Sub StepSpiralTxBx_EgtClosed(sender As Object, e As EventArgs) Handles StepSpiralTxBx.EgtClosed
      Dim dTemp As Double = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         StringToLen(StepSpiralTxBx.Text, dTemp)
         KitMachsLstBx.SelectedItem.dStepSpiral = dTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).dStepSpiral = dTemp
      End If
   End Sub

   Private Sub RadiusSpiralTxBx_EgtClosed(sender As Object, e As EventArgs) Handles RadiusSpiralTxBx.EgtClosed
      Dim dTemp As Double = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         StringToLen(RadiusSpiralTxBx.Text, dTemp)
         KitMachsLstBx.SelectedItem.dRadiusSpiral = dTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).dRadiusSpiral = dTemp
      End If
   End Sub

   Private Sub DistanceSpiralTxBx_EgtClosed(sender As Object, e As EventArgs) Handles DistanceSpiralTxBx.EgtClosed
      Dim dTemp As Double = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         StringToLen(DistanceSpiralTxBx.Text, dTemp)
         KitMachsLstBx.SelectedItem.dDistanceSpiral = dTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).dDistanceSpiral = dTemp
      End If
   End Sub

#End Region  ' Spirale

   Private Sub LiLenTxBx_EgtClosed(sender As Object, e As EventArgs) Handles LiLenTxBx.EgtClosed
      Dim dTemp As Double = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         StringToLen(LiLenTxBx.Text, dTemp)
         KitMachsLstBx.SelectedItem.dLiLen = dTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).dLiLen = dTemp
      End If
   End Sub

   Private Sub LiHeightTxBx_EgtClosed(sender As Object, e As EventArgs) Handles LiHeightTxBx.EgtClosed
      Dim dTemp As Double = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         StringToLen(LiHeightTxBx.Text, dTemp)
         KitMachsLstBx.SelectedItem.dLiHeight = dTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).dLiHeight = dTemp
      End If
   End Sub

   Private Sub LiLoadTxBx_EgtClosed(sender As Object, e As EventArgs) Handles LiLoadTxBx.EgtClosed
      Dim dTemp As Double = 0
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         StringToLen(LiLoadTxBx.Text, dTemp)
         KitMachsLstBx.SelectedItem.dLiLoad = dTemp
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).dLiLoad = dTemp
      End If
   End Sub

   Private Sub CheckBox_Click(sender As Object, e As RoutedEventArgs)
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).bActive = KitMachsLstBx.SelectedItem.bActive
      End If
   End Sub

   Private Sub ToolCmBx_SelectionChanged(sender As Object, e As SelectionChangedEventArgs)
      If Not IsNothing(KitMachsLstBx.SelectedItem) Then
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).SelTool = KitMachsLstBx.SelectedItem.SelTool
         If KitMachsLstBx.SelectedItem.SelTool >= 0 Then
            KitMachsLstBx.SelectedItem.sToolName = m_ToolList(KitMachsLstBx.SelectedItem.SelTool)
            KitMachsLstBx.SelectedItem.sToolUUID = m_ToolUuidList(KitMachsLstBx.SelectedItem.SelTool)
         Else
            KitMachsLstBx.SelectedItem.sToolName = "---"
            KitMachsLstBx.SelectedItem.sToolUUID = "---"
         End If
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).sToolName = KitMachsLstBx.SelectedItem.sToolName
         m_OldItem.KitMachList(KitMachsLstBx.SelectedItem.nId - 1).sToolUUID = KitMachsLstBx.SelectedItem.sToolUUID
      End If
   End Sub

   Private Function ReadKitName(nKitIndex As Integer) As String
      ' Leggo il nome del kit
      Dim sKey As String = K_KIT & nKitIndex.ToString("D2")
      Dim sName As String = ""
      GetPrivateProfileString(S_HEADER, sKey, "", sName, m_sKitFile)
      Return sName
   End Function

   Private Function ReadKit(nKitIndex As Integer, removeFlag As Boolean, ByRef Kit As Kit) As Boolean
      ' Leggo il nome del kit
      Dim sName As String = ReadKitName(nKitIndex)
      If String.IsNullOrWhiteSpace(sName) Then Return False
      ' Creo il kit
      Kit = New Kit(If(removeFlag, nKitIndex - 1, nKitIndex), sName)
      ' Leggo le sue lavorazioni
      For nkitMachIndex As Integer = 1 To 100
         Dim kmItem As KitMach = Nothing
         If ReadKitMach(sName, nkitMachIndex, kmItem) Then
            Kit.AddKitMach(kmItem)
         Else
            Exit For
         End If
      Next
      ' Se non sono state trovate lavorazioni, ne aggiungo una standard
      If Kit.KitMachList.Count() = 0 Then
         Kit.AddKitMach(New KitMach(1))
      End If
      Return True
   End Function

   Private Function ReadKitMach(sKitName As String, nKitMachIndex As Integer, ByRef kmItem As KitMach) As Boolean
      ' Verifico esistenza lavorazione del kit
      Dim sSect As String = S_KIT & "." & sKitName
      Dim sKeyInd As String = nKitMachIndex.ToString("D2") & "."
      Dim sName As String = ""
      If GetPrivateProfileString(sSect, sKeyInd & K_TOOLNAME, "", sName, m_sKitFile) = 0 Then Return False
      ' Lettura completa
      kmItem = New KitMach
      kmItem.sToolName = sName
      kmItem.nId = nKitMachIndex
      GetPrivateProfileString(sSect, sKeyInd & K_TOOL, "", kmItem.sToolUUID, m_sKitFile)
      kmItem.bActive = (GetPrivateProfileInt(sSect, sKeyInd & K_ACTIVE, 0, m_sKitFile) = 1)
      kmItem.nContour = GetPrivateProfileInt(sSect, sKeyInd & K_CONTOUR, 0, m_sKitFile)
      kmItem.dOffsetCnt = GetPrivateProfileDouble(sSect, sKeyInd & K_OFFSETCNT, 0, m_sKitFile)
      kmItem.nZigZagX = GetPrivateProfileInt(sSect, sKeyInd & K_ZIGZAGX, 0, m_sKitFile)
      kmItem.dOffZigZagX = GetPrivateProfileDouble(sSect, sKeyInd & K_OFFZIGZAGX, 0, m_sKitFile)
      kmItem.dStepX = GetPrivateProfileDouble(sSect, sKeyInd & K_STEPX, 0, m_sKitFile)
      kmItem.dRadiusX = GetPrivateProfileDouble(sSect, sKeyInd & K_RADIUSX, 0, m_sKitFile)
      kmItem.dDistanceX = GetPrivateProfileDouble(sSect, sKeyInd & K_DISTANCEX, 0, m_sKitFile)
      kmItem.nZigZagY = GetPrivateProfileInt(sSect, sKeyInd & K_ZIGZAGY, 0, m_sKitFile)
      kmItem.dOffZigZagY = GetPrivateProfileDouble(sSect, sKeyInd & K_OFFZIGZAGY, 0, m_sKitFile)
      kmItem.dStepY = GetPrivateProfileDouble(sSect, sKeyInd & K_STEPY, 0, m_sKitFile)
      kmItem.dRadiusY = GetPrivateProfileDouble(sSect, sKeyInd & K_RADIUSY, 0, m_sKitFile)
      kmItem.dDistanceY = GetPrivateProfileDouble(sSect, sKeyInd & K_DISTANCEY, 0, m_sKitFile)

      kmItem.nSpiral = GetPrivateProfileInt(sSect, sKeyInd & K_SPIRAL, 0, m_sKitFile)
      kmItem.dOffSpiral = GetPrivateProfileDouble(sSect, sKeyInd & K_OFFSPIRAL, 0, m_sKitFile)
      kmItem.dStepSpiral = GetPrivateProfileDouble(sSect, sKeyInd & K_STEPSPIRAL, 0, m_sKitFile)
      kmItem.dRadiusSpiral = GetPrivateProfileDouble(sSect, sKeyInd & K_RADIUSSPIRAL, 0, m_sKitFile)
      kmItem.dDistanceSpiral = GetPrivateProfileDouble(sSect, sKeyInd & K_DISTANCESPIRAL, 0, m_sKitFile)

      kmItem.dLiLen = GetPrivateProfileDouble(sSect, sKeyInd & K_LILEN, 0, m_sKitFile)
      kmItem.dLiHeight = GetPrivateProfileDouble(sSect, sKeyInd & K_LIHEIGHT, 0, m_sKitFile)
      kmItem.dLiLoad = GetPrivateProfileDouble(sSect, sKeyInd & K_LILOAD, 0, m_sKitFile)
      Return True
   End Function

   Friend Function WriteKitName(nKitIndex As Integer, sKitName As String) As Boolean
      Dim sKey As String = K_KIT & nKitIndex.ToString("D2")
      Return WritePrivateProfileString(S_HEADER, sKey, sKitName, m_sKitFile)
   End Function

   Private Function WriteKitMach(nKitIndex As Integer, nKitMachIndex As Integer, kmItem As KitMach) As Boolean
      ' Leggo il nome del kit
      Dim sKitName As String = ReadKitName(nKitIndex)
      If String.IsNullOrWhiteSpace(sKitName) Then Return False
      ' Sezione
      Dim sSect As String = S_KIT & "." & sKitName
      ' Indice
      Dim sKeyInd As String = nKitMachIndex.ToString("D2") & "."
      ' Scrivo i dati della lavorazione
      If Not IsNothing(kmItem) Then
         WritePrivateProfileString(sSect, sKeyInd & K_TOOL, kmItem.sToolUUID, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_TOOLNAME, kmItem.sToolName, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_ACTIVE, If(kmItem.bActive, "1", "0"), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_CONTOUR, kmItem.nContour.ToString(), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_OFFSETCNT, DoubleToString(kmItem.dOffsetCnt, 3), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_ZIGZAGX, kmItem.nZigZagX.ToString(), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_OFFZIGZAGX, DoubleToString(kmItem.dOffZigZagX.ToString, 3), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_STEPX, DoubleToString(kmItem.dStepX, 3), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_RADIUSX, DoubleToString(kmItem.dRadiusX, 3), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_DISTANCEX, DoubleToString(kmItem.dDistanceX, 3), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_ZIGZAGY, kmItem.nZigZagY.ToString(), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_OFFZIGZAGY, DoubleToString(kmItem.dOffZigZagY, 3), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_STEPY, DoubleToString(kmItem.dStepY, 3), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_RADIUSY, DoubleToString(kmItem.dRadiusY, 3), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_DISTANCEY, DoubleToString(kmItem.dDistanceY, 3), m_sKitFile)

         WritePrivateProfileString(sSect, sKeyInd & K_SPIRAL, kmItem.nSpiral.ToString(), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_OFFSPIRAL, DoubleToString(kmItem.dOffSpiral.ToString, 3), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_STEPSPIRAL, DoubleToString(kmItem.dStepSpiral, 3), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_RADIUSSPIRAL, DoubleToString(kmItem.dRadiusSpiral, 3), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_DISTANCESPIRAL, DoubleToString(kmItem.dDistanceSpiral, 3), m_sKitFile)

         WritePrivateProfileString(sSect, sKeyInd & K_LILEN, DoubleToString(kmItem.dLiLen, 3), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_LIHEIGHT, DoubleToString(kmItem.dLiHeight, 3), m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_LILOAD, DoubleToString(kmItem.dLiLoad, 3), m_sKitFile)
      Else
         WritePrivateProfileString(sSect, sKeyInd & K_TOOL, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_TOOLNAME, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_ACTIVE, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_CONTOUR, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_OFFSETCNT, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_ZIGZAGX, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_OFFZIGZAGX, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_STEPX, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_RADIUSX, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_DISTANCEX, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_ZIGZAGY, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_OFFZIGZAGY, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_STEPY, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_RADIUSY, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_DISTANCEY, Nothing, m_sKitFile)

         WritePrivateProfileString(sSect, sKeyInd & K_SPIRAL, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_OFFSPIRAL, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_STEPSPIRAL, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_RADIUSSPIRAL, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_DISTANCESPIRAL, Nothing, m_sKitFile)

         WritePrivateProfileString(sSect, sKeyInd & K_LILEN, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_LIHEIGHT, Nothing, m_sKitFile)
         WritePrivateProfileString(sSect, sKeyInd & K_LILOAD, Nothing, m_sKitFile)
      End If
      Return True
   End Function

   Private Function EraseKit(nKitIndex As Integer) As Boolean
      ' Leggo il nome del kit
      Dim sKitName As String = ReadKitName(nKitIndex)
      If String.IsNullOrWhiteSpace(sKitName) Then Return False
      ' Lo elimino dalla lista dei kit scalando tutti di una posizione e eliminando l'ultimo
      Dim nNextKitIndex As Integer = nKitIndex + 1
      Dim sNextKitName As String = ReadKitName(nNextKitIndex)
      While Not String.IsNullOrWhiteSpace(sNextKitName)
         WriteKitName(nNextKitIndex - 1, sNextKitName)
         nNextKitIndex += 1
         sNextKitName = ReadKitName(nNextKitIndex)
      End While
      WriteKitName(nNextKitIndex - 1, Nothing)
      ' Sezione
      Dim sSect As String = S_KIT & "." & sKitName
      ' Cancello tutte le lavorazioni del kit
      WritePrivateProfileString(sSect, Nothing, Nothing, m_sKitFile)
      Return True
   End Function

   Private Function EraseKitMach(nKitIndex As Integer, nKitMachIndex As Integer) As Boolean
      ' Leggo il nome del kit
      Dim sKitName As String = ReadKitName(nKitIndex)
      If String.IsNullOrWhiteSpace(sKitName) Then Return False
      ' Lo elimino dali KitMach scalando tutti di una posizione e eliminando l'ultimo
      Dim kmCurr As KitMach = Nothing
      Dim nNextKitMachIndex As Integer = nKitMachIndex + 1
      While ReadKitMach(sKitName, nNextKitMachIndex, kmCurr)
         kmCurr.nId = nNextKitMachIndex - 1
         WriteKitMach(nKitIndex, nNextKitMachIndex - 1, kmCurr)
         nNextKitMachIndex += 1
      End While
      WriteKitMach(nKitIndex, nNextKitMachIndex - 1, Nothing)
      Return True
   End Function

   Private Sub ActiveChBx_GotFocus(sender As Object, e As RoutedEventArgs)
      Dim myCheckBox As CheckBox = sender
      Dim Parent As DependencyObject = VisualTreeHelper.GetParent(myCheckBox)
      While (Not (TypeOf (Parent) Is ListBoxItem))
         Parent = VisualTreeHelper.GetParent(Parent)
      End While
      Dim myListBoxItem As ListBoxItem = Parent
      myListBoxItem.IsSelected = True
   End Sub

   Private Sub ToolCmBx_GotFocus(sender As Object, e As RoutedEventArgs)
      Dim myComboBox As ComboBox = sender
      Dim Parent As DependencyObject = VisualTreeHelper.GetParent(myComboBox)
      While (Not (TypeOf (Parent) Is ListBoxItem))
         Parent = VisualTreeHelper.GetParent(Parent)
      End While
      Dim myListBoxItem As ListBoxItem = Parent
      myListBoxItem.IsSelected = True
   End Sub
End Class

Class Kit

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

   Private m_nId As Integer
   Private m_sName As String
   Private m_KitMachList As List(Of KitMach)

   Public Property nId As Integer
      Get
         Return m_nId
      End Get
      Set(value As Integer)
         m_nId = value
      End Set
   End Property

   Public ReadOnly Property sName As String
      Get
         Return m_sName
      End Get
   End Property

   Public Property KitMachList As List(Of KitMach)
      Get
         Return m_KitMachList
      End Get
      Set(value As List(Of KitMach))
         m_KitMachList = value
      End Set
   End Property

   Sub New(nId As Integer, sName As String)
      m_nId = nId
      m_sName = sName

      m_KitMachList = New List(Of KitMach)

   End Sub

   Sub New(nId As Integer, sName As String, kitSou As Kit)
      m_nId = nId
      m_sName = sName

      m_KitMachList = New List(Of KitMach)

      If IsNothing(kitSou) Then Return

      For Each kmItem As KitMach In kitSou.m_KitMachList
         AddKitMach(kmItem)
      Next

   End Sub

   Sub AddKitMach(kitMachItem As KitMach)
      m_KitMachList.Add(kitMachItem)
   End Sub

End Class

Class KitMach

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

   Private m_nId As Integer
   Private m_sToolUUID As String
   Private m_sToolName As String
   Private m_SelTool As Integer
   Friend Shared m_ToolList As List(Of String)

   Private m_bActive As Boolean
   Private m_nContour As Integer
   Private m_dOffsetCnt As Double

   Private m_nZigZagX As Integer
   Private m_dStepX As Double
   Private m_dOffZigZagX As Double
   Private m_dRadiusX As Double
   Private m_dDistanceX As Double

   Private m_nZigZagY As Integer
   Private m_dStepY As Double
   Private m_dOffZigZagY As Double
   Private m_dRadiusY As Double
   Private m_dDistanceY As Double

   Private m_nSpiral As Integer
   Private m_dOffSpiral As Double
   Private m_dStepSpiral As Double
   Private m_dRadiusSpiral As Double
   Private m_dDistanceSpiral As Double

   Private m_dLiLen As Double
   Private m_dLiHeight As Double
   Private m_dLiLoad As Double

   Public m_IsModifiedId As Boolean = False
   Public Property nId As Integer
      Get
         Return m_nId
      End Get
      Set(value As Integer)
         If value <> m_nId Then
            m_nId = value
            m_IsModifiedId = True
         End If
      End Set
   End Property

   Public m_IsModifiedToolUUID As Boolean = False
   Public Property sToolUUID As String
      Get
         Return m_sToolUUID
      End Get
      Set(value As String)
         If value <> m_sToolUUID Then
            m_sToolUUID = value
            m_IsModifiedToolUUID = True
            ' In base al ToolUUID appena modificato recupero il nome dell'utensile e aggiorno ToolName
            Dim sToolNameDummy As String = m_sToolName
            If (Not EgtTdbGetToolFromUUID(m_sToolUUID, m_sToolName)) Or
               (Not m_MainWindow.m_MachinePageUC.m_PolishingsPageUC.ToolList.Contains(sToolNameDummy)) Or
               (Not m_sToolName.Equals(sToolNameDummy)) Then
               ' Se non riesco a recuperare il nome o il nome recuperato non è presente nella lista dei Tool caricati
               ' cerco tra i ToolUuid caricati quello che restituisca un nome uguale al nome dell'utensile corrente.
               ' Se lo trovo assegno il ToolUuid che corrisponde al nome dell'utensile corrente all'utensile corrente.

               If m_MainWindow.m_MachinePageUC.m_PolishingsPageUC.ToolList.Contains(sToolNameDummy) And
                  (Not m_sToolName.Equals(sToolNameDummy)) Then
                  m_sToolName = sToolNameDummy
               End If

               For Each sToolUuidItem As String In m_MainWindow.m_MachinePageUC.m_PolishingsPageUC.ToolUuidList
                  EgtTdbGetToolFromUUID(sToolUuidItem, sToolNameDummy)
                  If sToolNameDummy.Equals(m_sToolName) Then
                     m_sToolUUID = sToolUuidItem
                     Exit For
                  End If
               Next
            End If
         End If
      End Set
   End Property

   Public m_IsModifiedToolName As Boolean = False
   Public Property sToolName As String
      Get
         Return m_sToolName
      End Get
      Set(value As String)
         If value <> m_sToolName Then
            m_sToolName = value
            m_IsModifiedToolName = True
         End If
      End Set
   End Property

   Public m_IsModifiedSelTool As Boolean = False
   Public Property SelTool As Integer
      Get
         Return m_SelTool
      End Get
      Set(value As Integer)
         If value <> m_SelTool Then
            m_SelTool = value
            m_IsModifiedSelTool = True
         End If
      End Set
   End Property

   Public Property ToolList As List(Of String)
      Get
         Return m_ToolList
      End Get
      Set(value As List(Of String))
         m_ToolList = value
      End Set
   End Property

#Region "Contorno"

   Public m_IsModifiedActive As Boolean = False
   Public Property bActive As Boolean
      Get
         Return m_bActive
      End Get
      Set(value As Boolean)
         If value <> m_bActive Then
            m_bActive = value
            m_IsModifiedActive = True
         End If
      End Set
   End Property

   Public m_IsModifiedContour As Boolean = False
   Public Property nContour As Integer
      Get
         Return m_nContour
      End Get
      Set(value As Integer)
         If value <> m_nContour Then
            m_nContour = value
            m_IsModifiedContour = True
         End If
      End Set
   End Property

   Public m_IsModifiedOffCnt As Boolean = False
   Public Property dOffsetCnt As Double
      Get
         Return m_dOffsetCnt
      End Get
      Set(value As Double)
         If value <> m_dOffsetCnt Then
            m_dOffsetCnt = value
            m_IsModifiedOffCnt = True
         End If
      End Set
   End Property

#End Region    ' Contorno

#Region "ZigZag X"

   Public m_IsModifiedZigZagX As Boolean = False
   Public Property nZigZagX As Integer
      Get
         Return m_nZigZagX
      End Get
      Set(value As Integer)
         If value <> m_nZigZagX Then
            m_nZigZagX = value
            m_IsModifiedZigZagX = True
         End If
      End Set
   End Property

   Public m_IsModifiedOffZigZagX As Boolean = False
   Public Property dOffZigZagX As Double
      Get
         Return m_dOffZigZagX
      End Get
      Set(value As Double)
         If value <> m_dOffZigZagX Then
            m_dOffZigZagX = value
            m_IsModifiedOffZigZagX = True
         End If
      End Set
   End Property

   Public m_IsModifiedStepX As Boolean = False
   Public Property dStepX As Double
      Get
         Return m_dStepX
      End Get
      Set(value As Double)
         If value <> m_dStepX Then
            m_dStepX = value
            m_IsModifiedStepX = True
         End If
      End Set
   End Property

   Public m_IsModifiedRadiusX As Boolean = False
   Public Property dRadiusX As Double
      Get
         Return m_dRadiusX
      End Get
      Set(value As Double)
         If value <> m_dRadiusX Then
            m_dRadiusX = value
            m_IsModifiedRadiusX = True
         End If
      End Set
   End Property

   Public m_IsModifiedDistanceX As Boolean = False
   Public Property dDistanceX As Double
      Get
         Return m_dDistanceX
      End Get
      Set(value As Double)
         If value <> m_dDistanceX Then
            m_dDistanceX = value
            m_IsModifiedDistanceX = True
         End If
      End Set
   End Property

#End Region    ' ZigZag X

#Region "ZigZag Y"

   Public m_IsModifiedZigZagY As Boolean = False
   Public Property nZigZagY As Integer
      Get
         Return m_nZigZagY
      End Get
      Set(value As Integer)
         If value <> m_nZigZagY Then
            m_nZigZagY = value
            m_IsModifiedZigZagY = True
         End If
      End Set
   End Property

   Public m_IsModifiedOffZigZagY As Boolean = False
   Public Property dOffZigZagY As Double
      Get
         Return m_dOffZigZagY
      End Get
      Set(value As Double)
         If value <> m_dOffZigZagY Then
            m_dOffZigZagY = value
            m_IsModifiedOffZigZagY = True
         End If
      End Set
   End Property

   Public m_IsModifiedStepY As Boolean = False
   Public Property dStepY As Double
      Get
         Return m_dStepY
      End Get
      Set(value As Double)
         If value <> m_dStepY Then
            m_dStepY = value
            m_IsModifiedStepY = True
         End If
      End Set
   End Property

   Public m_IsModifiedRadiusY As Boolean = False
   Public Property dRadiusY As Double
      Get
         Return m_dRadiusY
      End Get
      Set(value As Double)
         If value <> m_dRadiusY Then
            m_dRadiusY = value
            m_IsModifiedRadiusY = True
         End If
      End Set
   End Property

   Public m_IsModifiedDistanceY As Boolean = False
   Public Property dDistanceY As Double
      Get
         Return m_dDistanceY
      End Get
      Set(value As Double)
         If value <> m_dDistanceY Then
            m_dDistanceY = value
            m_IsModifiedDistanceY = True
         End If
      End Set
   End Property

#End Region    ' ZigZag Y

#Region "Spirale"

   Public m_IsModifiedSpiral As Boolean = False
   Public Property nSpiral As Integer
      Get
         Return m_nSpiral
      End Get
      Set(value As Integer)
         If value <> m_nSpiral Then
            m_nSpiral = value
            m_IsModifiedSpiral = True
         End If
      End Set
   End Property

   Public m_IsModifiedOffSpiral As Boolean = False
   Public Property dOffSpiral As Double
      Get
         Return m_dOffSpiral
      End Get
      Set(value As Double)
         If value <> m_dOffSpiral Then
            m_dOffSpiral = value
            m_IsModifiedOffSpiral = True
         End If
      End Set
   End Property

   Public m_IsModifiedStepSpiral As Boolean = False
   Public Property dStepSpiral As Double
      Get
         Return m_dStepSpiral
      End Get
      Set(value As Double)
         If value <> m_dStepSpiral Then
            m_dStepSpiral = value
            m_IsModifiedStepSpiral = True
         End If
      End Set
   End Property

   Public m_IsModifiedRadiusSpiral As Boolean = False
   Public Property dRadiusSpiral As Double
      Get
         Return m_dRadiusSpiral
      End Get
      Set(value As Double)
         If value <> m_dRadiusSpiral Then
            m_dRadiusSpiral = value
            m_IsModifiedRadiusSpiral = True
         End If
      End Set
   End Property

   Public m_IsModifiedDistanceSpiral As Boolean = False
   Public Property dDistanceSpiral As Double
      Get
         Return m_dDistanceSpiral
      End Get
      Set(value As Double)
         If value <> m_dDistanceSpiral Then
            m_dDistanceSpiral = value
            m_IsModifiedDistanceSpiral = True
         End If
      End Set
   End Property

#End Region    ' Spirale

#Region "Parametri lavorazione"

   Public m_IsModifiedLiLen As Boolean = False
   Public Property dLiLen As Double
      Get
         Return m_dLiLen
      End Get
      Set(value As Double)
         If value <> m_dLiLen Then
            m_dLiLen = value
            m_IsModifiedLiLen = True
         End If
      End Set
   End Property

   Public m_IsModifiedLiHeight As Boolean = False
   Public Property dLiHeight As Double
      Get
         Return m_dLiHeight
      End Get
      Set(value As Double)
         If value <> m_dLiHeight Then
            m_dLiHeight = value
            m_IsModifiedLiHeight = True
         End If
      End Set
   End Property

   Public m_IsModifiedLiLoad As Boolean = False
   Public Property dLiLoad As Double
      Get
         Return m_dLiLoad
      End Get
      Set(value As Double)
         If value <> m_dLiLoad Then
            m_dLiLoad = value
            m_IsModifiedLiLoad = True
         End If
      End Set
   End Property

#End Region    ' Parametri lavorazione

   Sub New()
      m_nId = 0
      m_SelTool = 0
      m_sToolUUID = "---"
      m_sToolName = "---"
      m_bActive = False
      m_nContour = 0
      m_dOffsetCnt = 0
      m_nZigZagX = 0
      m_dOffZigZagX = 0
      m_dStepX = 0
      m_dRadiusX = 0
      m_dDistanceX = 0
      m_nZigZagY = 0
      m_dOffZigZagY = 0
      m_dStepY = 0
      m_dRadiusY = 0
      m_dDistanceY = 0
      m_nSpiral = 0
      m_dOffSpiral = 0
      m_dStepSpiral = 0
      m_dRadiusSpiral = 0
      m_dDistanceSpiral = 0
      m_dLiLen = 0
      m_dLiHeight = 0
      m_dLiLoad = 0
   End Sub

   Sub New(nId As Integer)
      m_nId = nId
      m_SelTool = 0
      m_sToolUUID = "---"
      m_sToolName = "---"
      m_bActive = False
      m_nContour = 0
      m_dOffsetCnt = 0
      m_nZigZagX = 0
      m_dOffZigZagX = 0
      m_dStepX = 0
      m_dRadiusX = 0
      m_dDistanceX = 0
      m_nZigZagY = 0
      m_dOffZigZagY = 0
      m_dStepY = 0
      m_dRadiusY = 0
      m_dDistanceY = 0
      m_nSpiral = 0
      m_dOffSpiral = 0
      m_dStepSpiral = 0
      m_dRadiusSpiral = 0
      m_dDistanceSpiral = 0
      m_dLiLen = 0
      m_dLiHeight = 0
      m_dLiLoad = 0
   End Sub

   Sub New(kmSou As KitMach)
      m_nId = kmSou.nId
      m_SelTool = kmSou.SelTool
      m_sToolUUID = kmSou.sToolUUID
      m_sToolName = kmSou.sToolName
      m_bActive = kmSou.bActive
      m_nContour = kmSou.nContour
      m_dOffsetCnt = kmSou.dOffsetCnt
      m_nZigZagX = kmSou.nZigZagX
      m_dOffZigZagX = kmSou.dOffZigZagX
      m_dStepX = kmSou.dStepX
      m_dRadiusX = kmSou.dRadiusX
      m_dDistanceX = kmSou.dDistanceX
      m_nZigZagY = kmSou.nZigZagY
      m_dOffZigZagY = kmSou.dOffZigZagY
      m_dStepY = kmSou.dStepY
      m_dRadiusY = kmSou.dRadiusY
      m_dDistanceY = kmSou.dDistanceY
      m_nSpiral = kmSou.nSpiral
      m_dOffSpiral = kmSou.dOffSpiral
      m_dStepSpiral = kmSou.dStepSpiral
      m_dRadiusSpiral = kmSou.dRadiusSpiral
      m_dDistanceSpiral = kmSou.dDistanceSpiral
      m_dLiLen = kmSou.dLiLen
      m_dLiHeight = kmSou.dLiHeight
      m_dLiLoad = kmSou.m_dLiLoad
   End Sub

End Class
