Imports EgtWPFLib5
Imports EgtUILib
Imports System.Collections.ObjectModel
Imports System.Windows.Forms
Imports EgtBEAMWALL.Core

Public Class WarehouseWndVM
   Inherits VMBase

#Region "Messages"

   Public ReadOnly Property CurrentS_Msg As String
      Get
         Select Case Map.refProjectVM.BTLStructureVM.nPROJTYPE
            Case MachineType.BEAM
               Return EgtMsg(61752)
            Case MachineType.WALL
               Return EgtMsg(61751)
         End Select
         Return Nothing
      End Get
   End Property

   Public ReadOnly Property StartOffset_Msg As String
      Get
         Return EgtMsg(61753)
      End Get
   End Property

   Public ReadOnly Property Kerf_Msg As String
      Get
         Return EgtMsg(61754)
      End Get
   End Property

   Public ReadOnly Property Offset_Msg As String
      Get
         Return EgtMsg(61755)
      End Get
   End Property

   Public ReadOnly Property MinThickness_Msg As String
      Get
         Return EgtMsg(61775)
      End Get
   End Property

   Public ReadOnly Property MaxThickness_Msg As String
      Get
         Return EgtMsg(61776)
      End Get
   End Property

   Public ReadOnly Property SParams_Msg As String
      Get
         Select Case Map.refProjectVM.BTLStructureVM.nPROJTYPE
            Case MachineType.BEAM
               Return EgtMsg(61757)
            Case MachineType.WALL
               Return EgtMsg(61756)
         End Select
         Return Nothing
      End Get
   End Property

   Public ReadOnly Property NewParam_Msg As String
      Get
         Return EgtMsg(61758)
      End Get
   End Property

   Public ReadOnly Property SectionXMaterial_Msg As String
      Get
         Return EgtMsg(61771)
      End Get
   End Property

   Public ReadOnly Property W_Msg As String
      Get
         Return EgtMsg(61759)
      End Get
   End Property

   Public ReadOnly Property L_Msg As String
      Get
         Return EgtMsg(61760)
      End Get
   End Property

   Public ReadOnly Property Quantity_Msg As String
      Get
         Return EgtMsg(61774)
      End Get
   End Property

   Public ReadOnly Property OkAddParam_Msg As String
      Get
         Return EgtMsg(61761)
      End Get
   End Property

   Public ReadOnly Property Save_Msg As String
      Get
         Return EgtMsg(61762)
      End Get
   End Property

   Public ReadOnly Property Cancel_Msg As String
      Get
         Return EgtMsg(61763)
      End Get
   End Property

   Public ReadOnly Property Error_Msg As String
      Get
         Return EgtMsg(61764)
      End Get
   End Property

   Public ReadOnly Property ValueExists_Msg As String
      Get
         Return EgtMsg(61765)
      End Get
   End Property

   Public ReadOnly Property IncorrectValue_Msg As String
      Get
         Return EgtMsg(61766)
      End Get
   End Property

   Public ReadOnly Property ValuesExist_Msg As String
      Get
         Return EgtMsg(61767)
      End Get
   End Property

   Public ReadOnly Property IncorrectValues_Msg As String
      Get
         Return EgtMsg(61768)
      End Get
   End Property

   Public ReadOnly Property Attention_Msg As String
      Get
         Return EgtMsg(61769)
      End Get
   End Property

   Public ReadOnly Property RemoveCurrent_Msg As String
      Get
         Return EgtMsg(61770)
      End Get
   End Property

#End Region    ' Messages

#Region "FIELDS & PROPERTIES"

   ' Costanti per settare la larghezza delle finestra
   Private Const BASIC_WNDWIDTH As Integer = 400
   Private Const MEDIUM_WNDWIDTH As Integer = 600

   ' Contatore dei parametri L/S rimossi in finestra per successiva rimozione dal file INI
   Friend m_RemovedParamsCount As Integer = 0
   ' Contatore dei parametri Offset e Spessore Min/Max in finestra per successiva rimozione dal file INI
   Friend m_RemovedWhWallParamsCount As Integer = 0

   Protected m_SParamColumns As New ObservableCollection(Of EgtDataGridColumn)
   Public Property SParamColumns As ObservableCollection(Of EgtDataGridColumn)
      Get
         Return m_SParamColumns
      End Get
      Set(value As ObservableCollection(Of EgtDataGridColumn))
         m_SParamColumns = value
      End Set
   End Property

   Private m_WhType As WarehouseType
   Public ReadOnly Property WhType As WarehouseType
      Get
         Return m_WhType
      End Get
   End Property

   Private m_dStartOffset As Double
   Public Property dStartOffset As Double
      Get
         Return m_dStartOffset
      End Get
      Set(value As Double)
         m_dStartOffset = value
         NotifyPropertyChanged(NameOf(dStartOffset))
      End Set
   End Property

   Private m_dKerf As Double
   Public Property dKerf As Double
      Get
         Return m_dKerf
      End Get
      Set(value As Double)
         m_dKerf = value
         NotifyPropertyChanged(NameOf(dKerf))
      End Set
   End Property

   Private m_dOffset As Double
   Public Property dOffset As Double
      Get
         Return m_dOffset
      End Get
      Set(value As Double)
         If value >= CurrentMachine.dMinOffset Then
            m_dOffset = value
         Else
            m_dOffset = CurrentMachine.dMinOffset
         End If
         NotifyPropertyChanged(NameOf(dOffset))
      End Set
   End Property

   Private m_WhWallParamList As New ObservableCollection(Of WhWallParam)
   Public Property WhWallParamList As ObservableCollection(Of WhWallParam)
      Get
         Return m_WhWallParamList
      End Get
      Set(value As ObservableCollection(Of WhWallParam))
         m_WhWallParamList = value
         NotifyPropertyChanged(NameOf(WhWallParamList))
      End Set
   End Property

   Private m_nQuantity As Integer
   Public Property nQuantity As Integer
      Get
         Return m_nQuantity
      End Get
      Set(value As Integer)
         m_nQuantity = value
         NotifyPropertyChanged(NameOf(nQuantity))
      End Set
   End Property

   Private m_SelCurrentS As SParam
   Public Property SelCurrentS As SParam
      Get
         Return m_SelCurrentS
      End Get
      Set(value As SParam)
         m_SelCurrentS = value
      End Set
   End Property

   Private m_SParamList_View As CollectionView = Nothing
   Private m_SParamList As New ObservableCollection(Of SParam)
   Public Property SParamList As ObservableCollection(Of SParam)
      Get
         Return m_SParamList
      End Get
      Set(value As ObservableCollection(Of SParam))
         m_SParamList = value
      End Set
   End Property

   Private m_SectionXMaterialList_View As CollectionView = Nothing
   Private m_SectionXMaterialList As New ObservableCollection(Of SectionXMaterial)
   Public Property SectionXMaterialList As ObservableCollection(Of SectionXMaterial)
      Get
         Return m_SectionXMaterialList
      End Get
      Set(value As ObservableCollection(Of SectionXMaterial))
         m_SectionXMaterialList = value
      End Set
   End Property

   Private m_SelSParam As SParam
   Public Property SelSParam As SParam
      Get
         Return m_SelSParam
      End Get
      Set(value As SParam)
         m_SelSParam = value
      End Set
   End Property

   Private m_sNewW As String
   Public Property sNewW As String
      Get
         Return m_sNewW
      End Get
      Set(value As String)
         m_sNewW = value
         NotifyPropertyChanged(NameOf(sNewW))
      End Set
   End Property

   Private m_sNewL As String
   Public Property sNewL As String
      Get
         Return m_sNewL
      End Get
      Set(value As String)
         m_sNewL = value
         NotifyPropertyChanged(NameOf(sNewL))
      End Set
   End Property

   Private m_StartOffset_Visibility As Visibility = Visibility.Collapsed
   Public Property StartOffset_Visibility As Visibility
      Get
         Return m_StartOffset_Visibility
      End Get
      Set(value As Visibility)
         m_StartOffset_Visibility = value
         NotifyPropertyChanged(NameOf(StartOffset_Visibility))
      End Set
   End Property

   Private m_Kerf_Visibility As Visibility = Visibility.Collapsed
   Public Property Kerf_Visibility As Visibility
      Get
         Return m_Kerf_Visibility
      End Get
      Set(value As Visibility)
         m_Kerf_Visibility = value
         NotifyPropertyChanged(NameOf(Kerf_Visibility))
      End Set
   End Property

   Private m_Offset_Visibility As Visibility = Visibility.Collapsed
   Public Property Offset_Visibility As Visibility
      Get
         Return m_Offset_Visibility
      End Get
      Set(value As Visibility)
         m_Offset_Visibility = value
         NotifyPropertyChanged(NameOf(Offset_Visibility))
      End Set
   End Property

   Private m_Basic_Visibility As Visibility = Visibility.Collapsed
   Public Property Basic_Visibility As Visibility
      Get
         Return m_Basic_Visibility
      End Get
      Set(value As Visibility)
         m_Basic_Visibility = value
         NotifyPropertyChanged(NameOf(Basic_Visibility))
      End Set
   End Property

   Private m_Medium_Visibility As Visibility = Visibility.Collapsed
   Public Property Medium_Visibility As Visibility
      Get
         Return m_Medium_Visibility
      End Get
      Set(value As Visibility)
         m_Medium_Visibility = value
         NotifyPropertyChanged(NameOf(Medium_Visibility))
      End Set
   End Property

   Private m_MediumWall_Visibility As Visibility = Visibility.Collapsed
   Public Property MediumWall_Visibility As Visibility
      Get
         Return m_MediumWall_Visibility
      End Get
      Set(value As Visibility)
         m_MediumWall_Visibility = value
         NotifyPropertyChanged(NameOf(MediumWall_Visibility))
      End Set
   End Property

   Private m_AddRemoveBtns_Visibility As Visibility = Visibility.Collapsed
   Public Property AddRemoveBtns_Visibility As Visibility
      Get
         Return m_AddRemoveBtns_Visibility
      End Get
      Set(value As Visibility)
         m_AddRemoveBtns_Visibility = value
         NotifyPropertyChanged(NameOf(AddRemoveBtns_Visibility))
      End Set
   End Property

   Private m_NewWallParam_Visibility As Visibility = Visibility.Collapsed
   Public Property NewWallParam_Visibility As Visibility
      Get
         Return m_NewWallParam_Visibility
      End Get
      Set(value As Visibility)
         m_NewWallParam_Visibility = value
         NotifyPropertyChanged(NameOf(NewWallParam_Visibility))
      End Set
   End Property

   Private m_NewBeamParam_Visibility As Visibility = Visibility.Collapsed
   Public Property NewBeamParam_Visibility As Visibility
      Get
         Return m_NewBeamParam_Visibility
      End Get
      Set(value As Visibility)
         m_NewBeamParam_Visibility = value
         NotifyPropertyChanged(NameOf(NewBeamParam_Visibility))
      End Set
   End Property

   Private m_WarehouseWnd_Width As Integer
   Public Property WarehouseWnd_Width As Integer
      Get
         Return m_WarehouseWnd_Width
      End Get
      Set(value As Integer)
         m_WarehouseWnd_Width = value
         NotifyPropertyChanged(NameOf(WarehouseWnd_Width))
      End Set
   End Property

   Friend Event m_CloseWindow(bDialogResult As Boolean)

   ' Definizione comandi
   Private m_cmdSave As ICommand
   Private m_cmdCancel As ICommand
   Private m_cmdAddParam As ICommand
   Private m_cmdOkAddParam As ICommand
   Private m_cmdCancelAddParam As ICommand
   Private m_cmdRemoveParam As ICommand
   Private m_cmdAddMediumParam As ICommand
   Private m_cmdRemoveMediumParam As ICommand
   Private m_cmdAddWhWallParam As ICommand
   Private m_cmdRemoveWhWallParam As ICommand

#End Region    ' FIELDS & PROPERTIES

#Region "CONSTRUCTOR"

   Sub New()
      ' Creo riferimento a questa classe in Map
      Map.SetRefWarehouseWndVM(Me)

      ' setto il tipo in base alla macchina selezionata come macchina corrente
      Dim nMachType As MachineType = Map.refProjectVM.BTLStructureVM.nPROJTYPE
      SectionXMaterial.SetType(nMachType)

      ' carico le colonne della datagrid
      LoadColumns(nMachType)

      Dim sValue As String = String.Empty
      Dim dValue As Double = 0
      Dim ParamIndex As Integer = 1
      m_WhType = GetMainPrivateProfileInt(S_WAREHOUSE, EgtBEAMWALL.Core.ConstIni.K_TYPE, 2)

      Select Case WhType
         Case WarehouseType.BASIC

            WarehouseWnd_Width = BASIC_WNDWIDTH
            Basic_Visibility = Visibility.Visible
            Dim sWarehousePath As String = Map.refMainWindowVM.MainWindowM.sWarehouseDir & "\" & WH_BASIC_INI_FILE_NAME

            If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then
               StartOffset_Visibility = Visibility.Visible
               Offset_Visibility = Visibility.Visible

               EgtUILib.GetPrivateProfileString(S_BEAM, K_STARTOFFSET, String.Empty, sValue, sWarehousePath)
               StringToLenAdv(sValue, dStartOffset)
               EgtUILib.GetPrivateProfileString(S_BEAM, K_OFFSET, String.Empty, sValue, sWarehousePath)
               StringToLenAdv(sValue, dOffset)
               EgtUILib.GetPrivateProfileString(S_BEAM, K_QUANTITY, String.Empty, sValue, sWarehousePath)
               Integer.TryParse(sValue, nQuantity)

               SParamList = New ObservableCollection(Of SParam)
               While EgtUILib.GetPrivateProfileString(S_BEAM, "L" & ParamIndex, String.Empty, sValue, sWarehousePath)
                  SParamList.Add(GetSParamFromWarehouse(ParamIndex))
                  ParamIndex += 1
               End While

               EgtUILib.GetPrivateProfileString(S_BEAM, K_CURRENTL, String.Empty, sValue, sWarehousePath)
               SelCurrentS = If(CInt(sValue) > 0 AndAlso CInt(sValue) <= SParamList.Count, SParamList(CInt(sValue) - 1), Nothing)

            ElseIf Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL Then
               Kerf_Visibility = Visibility.Visible
               Offset_Visibility = Visibility.Visible

               EgtUILib.GetPrivateProfileString(S_WALL, K_KERF, String.Empty, sValue, sWarehousePath)
               StringToLenAdv(sValue, dKerf)
               EgtUILib.GetPrivateProfileString(S_WALL, K_OFFSET, String.Empty, sValue, sWarehousePath)
               StringToLenAdv(sValue, dOffset)
               EgtUILib.GetPrivateProfileString(S_WALL, K_QUANTITY, String.Empty, sValue, sWarehousePath)
               Integer.TryParse(sValue, nQuantity)

               SParamList = New ObservableCollection(Of SParam)
               While EgtUILib.GetPrivateProfileString(S_WALL, "S" & ParamIndex, String.Empty, sValue, sWarehousePath)
                  SParamList.Add(GetSParamFromWarehouse(ParamIndex))
                  ParamIndex += 1
               End While

               EgtUILib.GetPrivateProfileString(S_WALL, K_CURRENTS, String.Empty, sValue, sWarehousePath)
               SelCurrentS = If(CInt(sValue) > 0 AndAlso CInt(sValue) <= SParamList.Count, SParamList(CInt(sValue) - 1), Nothing)

            End If
            AddRemoveBtns_Visibility = Visibility.Visible

         Case WarehouseType.MEDIUM

            WarehouseWnd_Width = MEDIUM_WNDWIDTH
            Medium_Visibility = Visibility.Visible
            Dim sWarehousePath As String = Map.refMainWindowVM.MainWindowM.sWarehouseDir & "\" & WH_MEDIUM_INI_FILE_NAME

            If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then
               StartOffset_Visibility = Visibility.Visible
               Offset_Visibility = Visibility.Visible

               EgtUILib.GetPrivateProfileString(S_BEAM, K_STARTOFFSET, String.Empty, sValue, sWarehousePath)
               StringToLenAdv(sValue, dStartOffset)
               EgtUILib.GetPrivateProfileString(S_BEAM, K_OFFSET, String.Empty, sValue, sWarehousePath)
               StringToLenAdv(sValue, dOffset)

               SParamList = New ObservableCollection(Of SParam)
               SectionXMaterialList = New ObservableCollection(Of SectionXMaterial)(GetSectionXMaterialsFromWarehouse())
               While EgtUILib.GetPrivateProfileString(S_BEAM, "L" & ParamIndex, String.Empty, sValue, sWarehousePath)
                  Dim SParam = GetSParamFromWarehouse(ParamIndex)
                  If Not IsNothing(SParam) Then
                     SParamList.Add(SParam)
                  End If
                  ParamIndex += 1
               End While

               ' per ogni SParam seleziono il SectionXMaterial corrispondente dalla lista della Warehouse
               For Each SParam In SParamList
                  Dim i As Integer = 0
                  For Each SectXMat In SectionXMaterialList
                     If SectXMat.sSectionXMaterial = SParam.SectXMat.sSectionXMaterial Then
                        SParam.nSelSectionXMaterial = i
                     End If
                     i += 1
                  Next
               Next

            ElseIf Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL Then
               Kerf_Visibility = Visibility.Visible
               Offset_Visibility = Visibility.Collapsed
               MediumWall_Visibility = Visibility.Visible

               EgtUILib.GetPrivateProfileString(S_WALL, K_KERF, String.Empty, sValue, sWarehousePath)
               StringToLenAdv(sValue, dKerf)

               ' carico la lista degli Spessori Min/Max e Offset compresi tra di essi
               While EgtUILib.GetPrivateProfileString(S_WALL, K_RANGE & ParamIndex, String.Empty, sValue, sWarehousePath)
                  WhWallParamList.Add(GetRangeParamFromWarehouse(ParamIndex))
                  ParamIndex += 1
               End While

               SParamList = New ObservableCollection(Of SParam)
               SectionXMaterialList = New ObservableCollection(Of SectionXMaterial)(GetSectionXMaterialsFromWarehouse())
               ParamIndex = 1
               While EgtUILib.GetPrivateProfileString(S_WALL, "S" & ParamIndex, String.Empty, sValue, sWarehousePath)
                  Dim SParamItem As SParam = GetSParamFromWarehouse(ParamIndex)
                  If Not IsNothing(SParamItem) Then
                     SParamList.Add(SParamItem)
                  End If
                  ParamIndex += 1
               End While

               ' per ogni SParam seleziono il SectionXMaterial corrispondente dalla lista della Warehouse
               For Each SParam In SParamList
                  Dim i As Integer = 0
                  For Each SectXMat In SectionXMaterialList
                     If SectXMat.sSectionXMaterial = SParam.SectXMat.sSectionXMaterial Then
                        SParam.nSelSectionXMaterial = i
                     End If
                     i += 1
                  Next
               Next

            End If

         Case WarehouseType.ADVANCED

            ' CASO NON ANCORA PRESENTE

      End Select
      ' riduco lista SecXMat a solo quelli presenti nel progetto corrente
      m_SectionXMaterialList_View = CollectionViewSource.GetDefaultView(m_SectionXMaterialList)
      m_SectionXMaterialList_View.Filter = AddressOf SectionXMaterialFilter
      ' riduco lista SParam a solo quelli che utilizzano SecXMat presenti nel progetto corrente
      m_SParamList_View = CollectionViewSource.GetDefaultView(m_SParamList)
      m_SParamList_View.Filter = AddressOf SParamFilter

   End Sub

   Private Function SectionXMaterialFilter(SectionXMaterial As Object) As Boolean
      Dim CurrSectionXMaterial As SectionXMaterial = DirectCast(SectionXMaterial, SectionXMaterial)
      Return Map.refProjectVM.BTLStructureVM.SectionList.Contains(CurrSectionXMaterial)
   End Function

   Private Function SParamFilter(SParam As Object) As Boolean
      Dim CurrSParam As SParam = DirectCast(SParam, SParam)
      Return IsNothing(CurrSParam.SectXMat) OrElse Map.refProjectVM.BTLStructureVM.SectionList.Contains(CurrSParam.SectXMat)
   End Function

#End Region    ' CONSTRUCTOR

#Region "COMMANDS"

#Region "Save"

   Public ReadOnly Property Save_Command As ICommand
      Get
         If m_cmdSave Is Nothing Then
            m_cmdSave = New Command(AddressOf Save)
         End If
         Return m_cmdSave
      End Get
   End Property

   Public Sub Save()
      ' Scrivo i valori nell'INI
      Dim ParamIndex = 1
      Select Case WhType
         Case WarehouseType.BASIC

            Dim sWarehousePath As String = Map.refMainWindowVM.MainWindowM.sWarehouseDir & "\" & WH_BASIC_INI_FILE_NAME

            If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then
               ' Verifico che i valori siano tutti > 0
               If dStartOffset <= 0 OrElse dOffset <= 0 Then
                  MessageBox.Show(IncorrectValues_Msg, Error_Msg)
                  Return
               End If
               ' Se i valori sono tutti > 0 li scrivo nel file INI
               WritePrivateProfileString(S_BEAM, K_CURRENTL, SParamList.IndexOf(SelCurrentS) + 1, sWarehousePath)
               WritePrivateProfileString(S_BEAM, K_STARTOFFSET, LenToString(dStartOffset, 3), sWarehousePath)
               WritePrivateProfileString(S_BEAM, K_OFFSET, LenToString(dOffset, 3), sWarehousePath)
               WritePrivateProfileString(S_BEAM, K_QUANTITY, nQuantity, sWarehousePath)
               For Each SPItem In SParamList
                  WritePrivateProfileString(S_BEAM, K_L & ParamIndex, SParamList(ParamIndex - 1).sValue, sWarehousePath)
                  ParamIndex += 1
               Next
               ' Se ci sono parametri da rimuovere li rimuovo
               For Index = ParamIndex To ParamIndex + m_RemovedParamsCount
                  WritePrivateProfileString(S_BEAM, K_L & Index, Nothing, sWarehousePath)
               Next
            ElseIf Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL Then
               ' Verifico che i valori siano tutti > 0
               If dKerf <= 0 OrElse dOffset <= 0 Then
                  MessageBox.Show(IncorrectValues_Msg, Error_Msg)
                  Return
               End If
               ' Se i valori sono tutti > 0 li scrivo nel file INI
               WritePrivateProfileString(S_WALL, K_CURRENTS, SParamList.IndexOf(SelCurrentS) + 1, sWarehousePath)
               WritePrivateProfileString(S_WALL, K_KERF, LenToString(dKerf, 3), sWarehousePath)
               WritePrivateProfileString(S_WALL, K_OFFSET, LenToString(dOffset, 3), sWarehousePath)
               WritePrivateProfileString(S_WALL, K_QUANTITY, nQuantity, sWarehousePath)
               For Each SPItem In SParamList
                  WritePrivateProfileString(S_WALL, K_S & ParamIndex, SParamList(ParamIndex - 1).sValue, sWarehousePath)
                  ParamIndex += 1
               Next
               ' Se ci sono parametri da rimuovere li rimuovo
               For Index = ParamIndex To ParamIndex + m_RemovedParamsCount
                  WritePrivateProfileString(S_WALL, K_S & Index, Nothing, sWarehousePath)
               Next
            End If
         Case WarehouseType.MEDIUM

            Dim sWarehousePath As String = Map.refMainWindowVM.MainWindowM.sWarehouseDir & "\" & WH_MEDIUM_INI_FILE_NAME

            If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then
               ' Verifico che i valori siano tutti > 0
               If dStartOffset < 0 OrElse dOffset < 0 Then
                  MessageBox.Show(IncorrectValues_Msg, Error_Msg)
                  Return
               End If
               For Each SPItem In SParamList
                  If IsNothing(SParamList(ParamIndex - 1).SectXMat) OrElse SParamList(ParamIndex - 1).dL <= 0 Then
                     MessageBox.Show(IncorrectValues_Msg, Error_Msg)
                     Return
                  End If
                  ParamIndex += 1
               Next
               ' Se i valori sono tutti >= 0 li scrivo nel file INI
               WritePrivateProfileString(S_BEAM, K_STARTOFFSET, LenToString(dStartOffset, 3), sWarehousePath)
               WritePrivateProfileString(S_BEAM, K_OFFSET, LenToString(dOffset, 3), sWarehousePath)
               ParamIndex = 1
               For Each SPItem In SParamList
                  Dim sL As String = String.Empty
                  sL = LenToString(SParamList(ParamIndex - 1).dL, 3)


                  ' ricavo l'indice del SecionXMaterial dalla Beam_List
                  Dim SectXMatIndex As Integer = GetSectIndexFromSection(MachineType.BEAM, SParamList(ParamIndex - 1).SectXMat)
                  'Dim sSxMValue As String = ""
                  'While EgtUILib.GetPrivateProfileString(S_BEAM_LIST, SectXMatIndex, String.Empty, sSxMValue, sWarehousePath)
                  '   Dim sSectXMatValues() As String = sSxMValue.Split(","c)
                  '   ' creo parametro
                  '   Dim dW As Double = 0
                  '   Dim dH As Double = 0
                  '   Dim sMaterial As String = sSectXMatValues(2)
                  '   StringToLenAdv(sSectXMatValues(0), dW)
                  '   StringToLenAdv(sSectXMatValues(1), dH)
                  '   If SParamList(ParamIndex - 1).SectXMat.dW = dW AndAlso SParamList(ParamIndex - 1).SectXMat.dH = dH AndAlso SParamList(ParamIndex - 1).SectXMat.sMaterial(0) = sMaterial Then
                  '      ' se l'ho trovato esco dal while, il SectXMatIndex è l'indice che cercavamo
                  '      Exit While
                  '   End If
                  '   SectXMatIndex += 1
                  'End While
                  If SectXMatIndex > 0 Then
                     WritePrivateProfileString(S_BEAM, K_L & ParamIndex,
                                            SectXMatIndex & "," & sL & "," & SParamList(ParamIndex - 1).nQuantity & "," & If(SParamList(ParamIndex - 1).bActive, 1, 0),
                                            sWarehousePath)
                     ParamIndex += 1
                  Else
                     EgtOutLog("Error in material creation, section not found!")
                  End If
               Next
               ' Se ci sono parametri da rimuovere li rimuovo
               For Index = ParamIndex To ParamIndex + m_RemovedParamsCount
                  WritePrivateProfileString(S_BEAM, K_L & Index, Nothing, sWarehousePath)
               Next
            ElseIf Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL Then
               ' Verifico che i valori siano tutti > 0
               If dKerf < 0 OrElse dOffset < 0 Then
                  MessageBox.Show(IncorrectValues_Msg, Error_Msg)
                  Return
               End If
               For Each SPItem In SParamList
                  If IsNothing(SParamList(ParamIndex - 1).SectXMat) OrElse SParamList(ParamIndex - 1).dW <= 0 OrElse SParamList(ParamIndex - 1).dL <= 0 Then
                     MessageBox.Show(IncorrectValues_Msg, Error_Msg)
                     Return
                  End If
                  ParamIndex += 1
               Next
               ' Se i valori sono tutti > 0 li scrivo nel file INI
               WritePrivateProfileString(S_WALL, K_KERF, LenToString(dKerf, 3), sWarehousePath)
               ParamIndex = 1
               For Each WhWallParamtem In WhWallParamList
                  Dim sOffset As String = String.Empty
                  Dim sMaxThickness As String = String.Empty
                  sOffset = LenToString(WhWallParamList(ParamIndex - 1).dOffset, 3)
                  sMaxThickness = LenToString(WhWallParamList(ParamIndex - 1).dMaxThickness, 3)
                  WritePrivateProfileString(S_WALL, K_RANGE & ParamIndex, sOffset & "," & sMaxThickness, sWarehousePath)
                  ParamIndex += 1
               Next
               ' Se ci sono parametri da rimuovere li rimuovo
               For Index = ParamIndex To ParamIndex + m_RemovedWhWallParamsCount
                  WritePrivateProfileString(S_WALL, K_RANGE & Index, Nothing, sWarehousePath)
               Next
               ParamIndex = 1
               For Each SPItem In SParamList
                  Dim sW As String = String.Empty
                  Dim sL As String = String.Empty
                  sW = LenToString(SParamList(ParamIndex - 1).dW, 3)
                  sL = LenToString(SParamList(ParamIndex - 1).dL, 3)

                  ' ricavo l'indice del SecionXMaterial dalla Wall_List
                  Dim SectXMatIndex As Integer = GetSectIndexFromSection(MachineType.WALL, SParamList(ParamIndex - 1).SectXMat)
                  'Dim sSxMValue As String = ""
                  'While EgtUILib.GetPrivateProfileString(S_WALL_LIST, SectXMatIndex, String.Empty, sSxMValue, sWarehousePath)
                  '   If sSxMValue = SParamList(ParamIndex - 1).sValue Then
                  '      ' se l'ho trovato esco dal while, il SectXMatIndex è l'indice che cercavamo
                  '      Exit While
                  '   End If
                  '   SectXMatIndex += 1
                  'End While
                  If SectXMatIndex > 0 Then
                     WritePrivateProfileString(S_WALL, K_S & ParamIndex,
                                            SectXMatIndex & "," & sW & "," & sL & "," & SParamList(ParamIndex - 1).nQuantity & "," & If(SParamList(ParamIndex - 1).bActive, 1, 0),
                                            sWarehousePath)
                     ParamIndex += 1
                  Else
                     EgtOutLog("Error in material creation, section not found!")
                  End If
               Next
               ' Se ci sono parametri da rimuovere li rimuovo
               For Index = ParamIndex To ParamIndex + m_RemovedParamsCount
                  WritePrivateProfileString(S_WALL, K_S & Index, Nothing, sWarehousePath)
               Next
            End If

         Case WarehouseType.ADVANCED

            ' CASO NON ANCORA PRESENTE

      End Select

      RaiseEvent m_CloseWindow(True)
   End Sub

#End Region ' Save

#Region "Cancel"

   Public ReadOnly Property Cancel_Command As ICommand
      Get
         If m_cmdCancel Is Nothing Then
            m_cmdCancel = New Command(AddressOf Cancel)
         End If
         Return m_cmdCancel
      End Get
   End Property

   Public Sub Cancel()
      RaiseEvent m_CloseWindow(True)
   End Sub

#End Region ' Cancel

#Region "AddParam"

   Public ReadOnly Property AddParam_Command As ICommand
      Get
         If m_cmdAddParam Is Nothing Then
            m_cmdAddParam = New Command(AddressOf AddParam)
         End If
         Return m_cmdAddParam
      End Get
   End Property

   Public Sub AddParam()
      Select Case Map.refProjectVM.BTLStructureVM.nPROJTYPE
         Case MachineType.BEAM
            NewBeamParam_Visibility = Visibility.Visible
         Case MachineType.WALL
            NewWallParam_Visibility = Visibility.Visible
      End Select
   End Sub

#End Region ' AddParam

#Region "OkAddParam"

   Public ReadOnly Property OkAddParam_Command As ICommand
      Get
         If m_cmdOkAddParam Is Nothing Then
            m_cmdOkAddParam = New Command(AddressOf OkAddParam)
         End If
         Return m_cmdOkAddParam
      End Get
   End Property

   Public Sub OkAddParam()
      Dim dWValue As Double = 0
      Dim dLValue As Double = 0
      Select Case Map.refProjectVM.BTLStructureVM.nPROJTYPE
         Case MachineType.BEAM
            If Not IsNothing(sNewL) Then StringToLenAdv(sNewL, dLValue, True)
            If dLValue > 0 Then
               ' controllo il valore immesso non sia già presente
               Dim bExist As Boolean = False
               For Each SPItem In SParamList
                  If SPItem.dL = dLValue Then
                     bExist = True
                     Exit For
                  End If
               Next
               If bExist Then
                  MessageBox.Show(ValueExists_Msg, Error_Msg)
               Else
                  SParamList.Add(New SParam(dLValue))
                  sNewL = Nothing
                  NewBeamParam_Visibility = Visibility.Collapsed
               End If
            Else
               MessageBox.Show(IncorrectValue_Msg, Error_Msg)
            End If
         Case MachineType.WALL
            If Not IsNothing(sNewW) Then StringToLenAdv(sNewW, dWValue, True)
            If Not IsNothing(sNewL) Then StringToLenAdv(sNewL, dLValue, True)
            If dWValue > 0 AndAlso dLValue > 0 Then
               ' controllo che la coppia di valori immessa non sia già presente
               Dim bExist As Boolean = False
               For Each SPItem In SParamList
                  If SPItem.dW = dWValue And SPItem.dL = dLValue Then
                     bExist = True
                     Exit For
                  End If
               Next
               If bExist Then
                  MessageBox.Show(ValuesExist_Msg, Error_Msg)
               Else
                  SParamList.Add(New SParam(dWValue, dLValue))
                  sNewW = Nothing
                  sNewL = Nothing
                  NewWallParam_Visibility = Visibility.Collapsed
               End If
            Else
               MessageBox.Show(IncorrectValues_Msg, Error_Msg)
            End If
      End Select
   End Sub

#End Region ' OkAddParam

#Region "CancelAddParam"

   Public ReadOnly Property CancelAddParam_Command As ICommand
      Get
         If m_cmdCancelAddParam Is Nothing Then
            m_cmdCancelAddParam = New Command(AddressOf CancelAddParam)
         End If
         Return m_cmdCancelAddParam
      End Get
   End Property

   Public Sub CancelAddParam()
      sNewW = Nothing
      sNewL = Nothing
      NewBeamParam_Visibility = Visibility.Collapsed
      NewWallParam_Visibility = Visibility.Collapsed
   End Sub

#End Region ' CancelAddParam

#Region "RemoveParam"

   Public ReadOnly Property RemoveParam_Command As ICommand
      Get
         If m_cmdRemoveParam Is Nothing Then
            m_cmdRemoveParam = New Command(AddressOf RemoveParam)
         End If
         Return m_cmdRemoveParam
      End Get
   End Property

   Public Sub RemoveParam()
      If Not IsNothing(SelSParam) Then
         If SelSParam.Equals(SelCurrentS) Then
            If MessageBox.Show(RemoveCurrent_Msg, Attention_Msg, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
               SParamList.Remove(SelSParam)
               m_RemovedParamsCount += 1
               If SParamList.Count > 0 Then
                  SelCurrentS = SParamList(0)
                  NotifyPropertyChanged("SelCurrentS")
               End If
            End If
            Return
         End If
         SParamList.Remove(SelSParam)
         m_RemovedParamsCount += 1
      End If
   End Sub

#End Region ' RemoveParam

#Region "AddMediumParam"

   Public ReadOnly Property AddMediumParam_Command As ICommand
      Get
         If m_cmdAddMediumParam Is Nothing Then
            m_cmdAddMediumParam = New Command(AddressOf AddMediumParam)
         End If
         Return m_cmdAddMediumParam
      End Get
   End Property

   Public Sub AddMediumParam()
      Dim nQuantity As Integer = GetMainPrivateProfileInt(S_WAREHOUSE, K_DEFAULTQUANTITY, 1)
      Dim NewSParam As SParam = Nothing
      If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then
         NewSParam = New SParam(Nothing, 0, nQuantity, True)
      ElseIf Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL Then
         NewSParam = New SParam(Nothing, 0, 0, nQuantity, True)
      End If
      ' setto il valore selezionato della CmBx al primo elemento della lista e assegno NewSParam alla lista degli SParam 
      If Not IsNothing(NewSParam) Then
         NewSParam.nSelSectionXMaterial = -1
         SParamList.Add(NewSParam)
         m_SelSParam = NewSParam
         NotifyPropertyChanged(NameOf(SelSParam))
      End If
   End Sub

#End Region ' AddMediumParam

#Region "RemoveMediumParam"

   Public ReadOnly Property RemoveMediumParam_Command As ICommand
      Get
         If m_cmdRemoveMediumParam Is Nothing Then
            m_cmdRemoveMediumParam = New Command(AddressOf RemoveMediumParam)
         End If
         Return m_cmdRemoveMediumParam
      End Get
   End Property

   Public Sub RemoveMediumParam()
      SParamList.Remove(SelSParam)
      m_RemovedParamsCount += 1
   End Sub

#End Region ' RemoveMediumParam

#Region "AddWhWallParam"

   Public ReadOnly Property AddWhWallParam_Command As ICommand
      Get
         If m_cmdAddWhWallParam Is Nothing Then
            m_cmdAddWhWallParam = New Command(AddressOf AddWhWallParam)
         End If
         Return m_cmdAddWhWallParam
      End Get
   End Property

   Public Sub AddWhWallParam()
      If WhWallParamList.Count > 0 Then
         If WhWallParamList.Last().dMaxThickness > WhWallParamList.Last().dMinThickness Then
            WhWallParamList.Add(New WhWallParam(0, CurrentMachine.dMaxRawH))
         Else
            System.Windows.MessageBox.Show(EgtMsg(61777), "", MessageBoxButton.OK, MessageBoxImage.Warning)
         End If
      Else
         WhWallParamList.Add(New WhWallParam(0, CurrentMachine.dMaxRawH))
      End If
   End Sub

#End Region ' AddWhWallParam

#Region "RemoveWhWallParam"

   Public ReadOnly Property RemoveWhWallParam_Command As ICommand
      Get
         If m_cmdRemoveWhWallParam Is Nothing Then
            m_cmdRemoveWhWallParam = New Command(AddressOf RemoveWhWallParam)
         End If
         Return m_cmdRemoveWhWallParam
      End Get
   End Property

   Public Sub RemoveWhWallParam()
      If WhWallParamList.Count > 0 Then
         WhWallParamList.Remove(WhWallParamList.Last())
         m_RemovedWhWallParamsCount += 1
      End If
   End Sub

#End Region ' RemoveWhWallParam

#End Region    ' COMMANDS

#Region "METHODS"

   Protected Sub LoadColumns(ProjectType As BWType)
      If ProjectType = BWType.BEAM Then
         ' carico le colonne della datagrid
         GetPrivateProfileColumns(S_SPARAMLIST_BEAM, SParamColumns)
      ElseIf ProjectType = BWType.WALL Then
         ' carico le colonne della datagrid
         GetPrivateProfileColumns(S_SPARAMLIST_WALL, SParamColumns)
      End If
      ' aggiorno la visibilità delle colonne
      For Each col In SParamColumns
         col.ColumnVisibility = If(col.Visible, Visibility.Visible, Visibility.Collapsed)
      Next
   End Sub

   Public Shared Sub CheckExistingSectionXMaterial()
      Dim NewSectXMatList As New List(Of SectionXMaterialToBeAdded)
      ' Ricavo la lista dei SectionXMaterial presenti in Warehouse
      Dim WhSectXMatList As List(Of SectionXMaterial) = GetSectionXMaterialsFromWarehouse()
      For Each SectionListItem In Map.refProjectVM.BTLStructureVM.SectionList
         Dim Index As Integer = 0
         Dim bEqual As Boolean = False
         ' Confronto l'Item di SectionList con ciascuno di quelli presenti nella Warehouse
         For Each SectXMatListItem In WhSectXMatList
            If SectionListItem = WhSectXMatList(Index) Then
               ' Se l'Item di SectionList equivale ad uno presente nella Warehouse setto bEqual a True ed esco dal For
               bEqual = True
               Exit For
            End If
            Index += 1
         Next
         ' Se l'Item di SectionList non equivale a nessuno di quelli della Warehouse lo aggiungo alla lista
         If Not bEqual And IsSectionValid(SectionListItem) Then
            NewSectXMatList.Add(New SectionXMaterialToBeAdded(SectionListItem))
         End If
      Next
      ' Se nel BTL importato ci sono SectionXMaterial non presenti nell'INI della Warehouse mostro il dialog per la gestione di questi
      If NewSectXMatList.Count > 0 Then
         Dim AddSectionXMaterialWnd As New AddSectionXMaterialWndV(Application.Current.MainWindow, New AddSectionXMaterialWndVM(NewSectXMatList))
         AddSectionXMaterialWnd.ShowDialog()
      End If
   End Sub

   Public Shared Sub UpdateSectionXMaterial()
      ' aggiungo tutte le sezioni al magazzino (verifica se esiste gia' viene fatta all'interno della funzione)
      For Each SectionItem In Map.refProjectVM.BTLStructureVM.SectionList
         If SectionItem.dH <> -1 Then
            AddSectionXMaterialInWarehouse(SectionItem)
         End If
      Next
   End Sub

   Public Shared Function GetSectionXMaterialsFromWarehouse() As List(Of SectionXMaterial)
      Dim SectXMatList As New List(Of SectionXMaterial)
      Dim sWarehousePath As String = Map.refMainWindowVM.MainWindowM.sWarehouseDir & "\" & WH_MEDIUM_INI_FILE_NAME

      If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then
         ' ricavo SectionXMaterial tramite Beam_List
         Dim sSectXMatIndex As Integer = 1
         Dim sSxMValue As String = ""
         While EgtUILib.GetPrivateProfileString(S_BEAM_LIST, sSectXMatIndex, String.Empty, sSxMValue, sWarehousePath)
            Dim sSectXMatValues() As String = sSxMValue.Split(","c)
            ' creo parametro
            Dim dW As Double = 0
            Dim dH As Double = 0
            StringToLenAdv(sSectXMatValues(0), dW)
            StringToLenAdv(sSectXMatValues(1), dH)
            If Not SectXMatList.Any(Function(x) x.dW = dW AndAlso x.dH = dH AndAlso x.sMaterial(0) = sSectXMatValues(2)) Then
               SectXMatList.Add(New SectionXMaterial(dW, dH, 0, sSectXMatValues(2)))
            End If
            sSectXMatIndex += 1
         End While
      ElseIf Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL Then
         ' ricavo SectionXMaterial tramite Wall_List
         Dim sSectXMatIndex As Integer = 1
         Dim sSxMValue As String = ""
         While EgtUILib.GetPrivateProfileString(S_WALL_LIST, sSectXMatIndex, String.Empty, sSxMValue, sWarehousePath)
            Dim sSectXMatValues() As String = sSxMValue.Split(","c)
            ' creo parametro
            Dim dH As Double = 0
            StringToLenAdv(sSectXMatValues(0), dH)
            If Not SectXMatList.Any(Function(x) x.dH = dH AndAlso x.sMaterial(0) = sSectXMatValues(1)) Then
               SectXMatList.Add(New SectionXMaterial(0, dH, 0, sSectXMatValues(1)))
            End If
            sSectXMatIndex += 1
         End While
      End If
      Return SectXMatList
   End Function

   Public Shared Function AddSectionXMaterialInWarehouse(SectionXMaterial As SectionXMaterial) As Boolean
      Dim SectXMatList As New List(Of SectionXMaterial)
      Dim sSectXMatIndex As Integer = 1
      Dim sSxMValue As String = ""
      Dim sWarehousePath As String = Map.refMainWindowVM.MainWindowM.sWarehouseDir & "\" & WH_MEDIUM_INI_FILE_NAME

      If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then
         ' ricavo SectionXMaterial tramite Beam_List
         While EgtUILib.GetPrivateProfileString(S_BEAM_LIST, sSectXMatIndex, String.Empty, sSxMValue, sWarehousePath) > 0
            ' verifico se uguale
            Dim sSectXMatValues() As String = sSxMValue.Split(","c)
            ' creo parametro
            Dim dW As Double = 0
            Dim dH As Double = 0
            StringToLenAdv(sSectXMatValues(0), dW)
            StringToLenAdv(sSectXMatValues(1), dH)
            Dim ReadSection As New SectionXMaterial(dW, dH, 0, sSectXMatValues(2))
            If ReadSection = SectionXMaterial Then Return False
            sSectXMatIndex += 1
         End While
         WritePrivateProfileString(S_BEAM_LIST, sSectXMatIndex, LenToString(SectionXMaterial.dW, 3) & "," &
                                                                LenToString(SectionXMaterial.dH, 3) & "," &
                                                                SectionXMaterial.sMaterial(0), sWarehousePath)
      ElseIf Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL Then
         ' ricavo SectionXMaterial tramite Wall_List
         While EgtUILib.GetPrivateProfileString(S_WALL_LIST, sSectXMatIndex, String.Empty, sSxMValue, sWarehousePath)
            Dim sSectXMatValues() As String = sSxMValue.Split(","c)
            ' creo parametro
            Dim dH As Double = 0
            StringToLenAdv(sSectXMatValues(0), dH)
            Dim ReadSection As New SectionXMaterial(0, dH, 0, sSectXMatValues(1))
            If ReadSection = SectionXMaterial Then Return False
            sSectXMatIndex += 1
         End While
         WritePrivateProfileString(S_WALL_LIST, sSectXMatIndex, LenToString(SectionXMaterial.dH, 3) & "," &
                                                                SectionXMaterial.sMaterial(0), sWarehousePath)
      End If
      Return True
   End Function

   Public Shared Function GetSParamFromWarehouse(ParamIndex As Integer) As SParam
      Dim nDefault As Integer = 2
      Dim sValue As String = String.Empty
      Dim sSxMValue As String = String.Empty
      Dim dValue As Double = 0
      Dim WhType As Integer = GetMainPrivateProfileInt(S_WAREHOUSE, EgtBEAMWALL.Core.ConstIni.K_TYPE, nDefault)
      Select Case WhType
         Case WarehouseType.BASIC
            Dim sWarehousePath As String = Map.refMainWindowVM.MainWindowM.sWarehouseDir & "\" & WH_BASIC_INI_FILE_NAME
            If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then
               If EgtUILib.GetPrivateProfileString(S_BEAM, "L" & ParamIndex, String.Empty, sValue, sWarehousePath) Then
                  StringToLenAdv(sValue, dValue)
                  Return New SParam(dValue)
               End If
            ElseIf Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL Then
               If EgtUILib.GetPrivateProfileString(S_WALL, "S" & ParamIndex, String.Empty, sValue, sWarehousePath) Then
                  Dim sParamValues() As String = sValue.Split(","c)
                  ' verifico numero minimo di parametri
                  If sParamValues.Count >= 2 Then
                     ' cancello spazi
                     For Index = 0 To sParamValues.Count - 1
                        sParamValues(Index) = sParamValues(Index).Trim()
                     Next
                     ' creo parametro
                     Dim dW As Double = 0
                     Dim dL As Double = 0
                     StringToLenAdv(sParamValues(0), dW)
                     StringToLenAdv(sParamValues(1), dL)
                     Return New SParam(dW, dL)
                  End If
               End If
            End If
         Case WarehouseType.MEDIUM
            Dim sWarehousePath As String = Map.refMainWindowVM.MainWindowM.sWarehouseDir & "\" & WH_MEDIUM_INI_FILE_NAME
            If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then
               If EgtUILib.GetPrivateProfileString(S_BEAM, "L" & ParamIndex, String.Empty, sValue, sWarehousePath) Then
                  Dim sParamValues() As String = sValue.Split(","c)
                  ' verifico numero minimo di parametri
                  If sParamValues.Count >= 2 Then
                     ' cancello spazi
                     For Index = 0 To sParamValues.Count - 1
                        sParamValues(Index) = sParamValues(Index).Trim()
                     Next
                     ' ricavo SectionXMaterial tramite Beam_List
                     If EgtUILib.GetPrivateProfileString(S_BEAM_LIST, sParamValues(0), String.Empty, sSxMValue, sWarehousePath) Then
                        Dim sSectXMatValues() As String = sSxMValue.Split(","c)
                        ' creo parametro
                        Dim dW As Double = 0
                        Dim dH As Double = 0
                        Dim dL As Double = 0
                        Dim nQty As Integer = 0
                        Dim nActive As Integer = 1
                        StringToLenAdv(sSectXMatValues(0), dW)
                        StringToLenAdv(sSectXMatValues(1), dH)
                        StringToLenAdv(sParamValues(1), dL)
                        If sParamValues.Count >= 3 Then Integer.TryParse(sParamValues(2), nQty)
                        If sParamValues.Count >= 4 Then Integer.TryParse(sParamValues(3), nActive)
                        Return New SParam(New SectionXMaterial(dW, dH, dL, sSectXMatValues(2)), dL, nQty, nActive > 0)
                     End If
                  End If
               End If
            ElseIf Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL Then
               If EgtUILib.GetPrivateProfileString(S_WALL, "S" & ParamIndex, String.Empty, sValue, sWarehousePath) Then
                  Dim sParamValues() As String = sValue.Split(","c)
                  ' verifico numero minimo di parametri
                  If sParamValues.Count >= 3 Then
                     ' cancello spazi
                     For Index = 0 To sParamValues.Count - 1
                        sParamValues(Index) = sParamValues(Index).Trim()
                     Next
                     ' ricavo SectionXMaterial tramite Wall_List
                     If EgtUILib.GetPrivateProfileString(S_WALL_LIST, sParamValues(0), String.Empty, sSxMValue, sWarehousePath) Then
                        Dim sSectXMatValues() As String = sSxMValue.Split(","c)
                        ' creo parametro
                        Dim dH As Double = 0
                        Dim dW As Double = 0
                        Dim dL As Double = 0
                        Dim nQty As Integer = 0
                        Dim nActive As Integer = 1
                        StringToLenAdv(sSectXMatValues(0), dH)
                        StringToLenAdv(sParamValues(1), dW)
                        StringToLenAdv(sParamValues(2), dL)
                        If sParamValues.Count >= 4 Then Integer.TryParse(sParamValues(3), nQty)
                        If sParamValues.Count >= 5 Then Integer.TryParse(sParamValues(4), nActive)
                        Return New SParam(New SectionXMaterial(dW, dH, dL, sSectXMatValues(1)), dW, dL, nQty, nActive > 0)
                     End If
                  End If
               End If
            End If
         Case WarehouseType.ADVANCED
            ' CASO NON ANCORA PRESENTE
      End Select
      Return Nothing
   End Function

   Public Shared Function GetRangeParamFromWarehouse(ParamIndex As Integer) As WhWallParam
      Dim nDefault As Integer = 2
      Dim sValue As String = String.Empty
      Dim sSxMValue As String = String.Empty
      Dim dValue As Double = 0
      Dim WhType As Integer = GetMainPrivateProfileInt(S_WAREHOUSE, EgtBEAMWALL.Core.ConstIni.K_TYPE, nDefault)
      Select Case WhType
         Case WarehouseType.BASIC
            ' NON DISPONIBILE
         Case WarehouseType.MEDIUM
            Dim sWarehousePath As String = Map.refMainWindowVM.MainWindowM.sWarehouseDir & "\" & WH_MEDIUM_INI_FILE_NAME
            If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then
               ' NON DISPONIBILE
            ElseIf Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL Then
               If EgtUILib.GetPrivateProfileString(S_WALL, K_RANGE & ParamIndex, String.Empty, sValue, sWarehousePath) Then
                  Dim sParamValues() As String = sValue.Split(","c)
                  ' verifico numero minimo di parametri
                  If sParamValues.Count >= 2 Then
                     ' cancello spazi
                     For Index = 0 To sParamValues.Count - 1
                        sParamValues(Index) = sParamValues(Index).Trim()
                     Next
                     ' creo parametro
                     Dim dOffset As Double = 0
                     Dim dMaxThickness As Double = 0
                     StringToLenAdv(sParamValues(0), dOffset)
                     StringToLenAdv(sParamValues(1), dMaxThickness)
                     Return New WhWallParam(dOffset, dMaxThickness)
                  End If
               End If
            End If
         Case WarehouseType.ADVANCED
            ' CASO NON ANCORA PRESENTE
      End Select
      Return Nothing
   End Function

#End Region    ' METHODS

End Class

Public Class SParam
   Inherits VMBase
   Implements IComparable

   Public ReadOnly Property W_Msg As String
      Get
         Return EgtMsg(61759)
      End Get
   End Property

   Public ReadOnly Property L_Msg As String
      Get
         Return EgtMsg(61760)
      End Get
   End Property

   Public ReadOnly Property Quantity_Msg As String
      Get
         Return EgtMsg(61774)
      End Get
   End Property

   Private m_bActive As Boolean
   Public Property bActive As Boolean
      Get
         Return m_bActive
      End Get
      Set(value As Boolean)
         m_bActive = value
      End Set
   End Property

   Private m_SectXMat As SectionXMaterial
   Public Property SectXMat As SectionXMaterial
      Get
         Return m_SectXMat
      End Get
      Set(value As SectionXMaterial)
         m_SectXMat = value
      End Set
   End Property

   Private m_dW As Double
   Public Property dW As Double
      Get
         Return m_dW
      End Get
      Set(value As Double)
         m_dW = value
      End Set
   End Property

   Private m_dL As Double
   Public Property dL As Double
      Get
         Return m_dL
      End Get
      Set(value As Double)
         m_dL = value
      End Set
   End Property

   Private m_nQuantity As Integer
   Public Property nQuantity As Integer
      Get
         Return m_nQuantity
      End Get
      Set(value As Integer)
         m_nQuantity = value
      End Set
   End Property

   Private m_nSelSectionXMaterial As Integer
   Public Property nSelSectionXMaterial As Integer
      Get
         Return m_nSelSectionXMaterial
      End Get
      Set(value As Integer)
         m_nSelSectionXMaterial = value
         ' aggiorno SectXMat del SParam
         If value < 0 Then
            SectXMat = Nothing
         Else
            SectXMat = Map.refWarehouseWndVM.SectionXMaterialList(m_nSelSectionXMaterial)
         End If
      End Set
   End Property

   Public ReadOnly Property sValue As String
      Get
         Select Case WarehouseHelper.GetWarehouseType()
            Case WarehouseType.BASIC
               If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then Return LenToString(dL, 3)
               If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL Then Return LenToString(dW, 3) & "," & LenToString(dL, 3)
            Case WarehouseType.MEDIUM
               Dim sAlias As String = String.Empty
               For Each sMatItem In SectXMat.sMaterial
                  sAlias &= sMatItem & ";"
               Next
               sAlias = sAlias.TrimEnd(";")
               If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then Return LenToString(SectXMat.dW, 3) & "," & LenToString(SectXMat.dH, 3) & "," & sAlias
               If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL Then Return LenToString(SectXMat.dH, 3) & "," & sAlias
         End Select
         Return Nothing
      End Get
   End Property

   ' proprietà per il sorting del SectionXMaterial in base alla prima dimensione della sezione
   Public ReadOnly Property dFirstDimension As Double
      Get
         If SectionXMaterial.nType = MachineType.BEAM AndAlso Not IsNothing(SectXMat) Then
            Return SectXMat.dW
         ElseIf SectionXMaterial.nType = MachineType.WALL AndAlso Not IsNothing(SectXMat) Then
            Return SectXMat.dH
         Else
            Return 0
         End If
      End Get
   End Property

   ' proprietà per il sorting del SectionXMaterial in base alla seconda dimensione della sezione
   Public ReadOnly Property dSecondDimension As Double
      Get
         If SectionXMaterial.nType = MachineType.BEAM AndAlso Not IsNothing(SectXMat) Then
            Return SectXMat.dH
         Else
            Return 0
         End If
      End Get
   End Property

   ' proprieta' per finestra di aggiunta nuovo grezzo
   Public ReadOnly Property sDimension As String
      Get
         If SectionXMaterial.nType = MachineType.BEAM Then
            Return LenToString(dL, 3)
         ElseIf SectionXMaterial.nType = MachineType.WALL Then
            Return LenToString(dW, 3) & " x " & LenToString(dL, 3)
         Else
            Return 0
         End If
      End Get
   End Property

   Private m_Quantity_Visibility As Visibility = Visibility.Hidden
   Public Property Quantity_Visibility As Visibility
      Get
         Return m_Quantity_Visibility
      End Get
      Set(value As Visibility)
         m_Quantity_Visibility = value
      End Set
   End Property

   Private m_MediumW_Visibility As Visibility = Visibility.Hidden
   Public Property MediumW_Visibility As Visibility
      Get
         Return m_MediumW_Visibility
      End Get
      Set(value As Visibility)
         m_MediumW_Visibility = value
      End Set
   End Property

#Region "For Statistics"

   Public ReadOnly Property sW As Double
      Get
         Select Case Map.refProjectVM.BTLStructureVM.nPROJTYPE
            Case BWType.BEAM
               Return SectXMat.dW
            Case BWType.WALL
               Return dW
            Case Else
               Return dW
         End Select
      End Get
   End Property

   Public ReadOnly Property sH As Double
      Get
         Return SectXMat.dH
      End Get
   End Property

   Public ReadOnly Property sL As Double
      Get
         Return dL
      End Get
   End Property

   Public ReadOnly Property sMaterial As String
      Get
         Return If(IsNothing(SectXMat), "", SectXMat.sMaterial(0))
      End Get
   End Property

#End Region    ' For Statistics

   Sub New(W As Double, L As Double)
      m_dW = W
      m_dL = L
   End Sub

   Sub New(L As Double)
      m_dL = L
   End Sub

   Sub New(SxM As SectionXMaterial, W As Double, L As Double, Qty As Integer, Active As Boolean)
      m_SectXMat = SxM
      m_dW = W
      m_dL = L
      m_nQuantity = Qty
      m_bActive = Active
      MediumW_Visibility = Visibility.Visible
      Quantity_Visibility = Visibility.Visible
   End Sub

   Sub New(SxM As SectionXMaterial, L As Double, Qty As Integer, Active As Boolean)
      m_SectXMat = SxM
      m_dL = L
      m_nQuantity = Qty
      m_bActive = Active
      Quantity_Visibility = Visibility.Visible
   End Sub

   ' definizione di CompareTo per il Sort degli SParam (Materiale -> 1^ dimensione -> 2^ dimensione)
   Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
      Dim SParamItem As SParam = DirectCast(obj, SParam)
      If sMaterial.CompareTo(SParamItem.sMaterial) <> 0 Then
         Return sMaterial.CompareTo(SParamItem.sMaterial)
      Else
         If dFirstDimension.CompareTo(SParamItem.dFirstDimension) <> 0 Then
            Return dFirstDimension.CompareTo(SParamItem.dFirstDimension)
         Else
            Return dSecondDimension.CompareTo(SParamItem.dSecondDimension)
         End If
      End If
   End Function

   ' proprietà usata come SortMemberPath della colonna SectionXMaterial
   Public ReadOnly Property SectionXMaterialDisplayOrder As SParam
      Get
         Return Me
      End Get
   End Property

End Class

Public Class WhWallParam
   Inherits VMBase

   Public ReadOnly Property dMinThickness As Double
      Get
         If Map.refWarehouseWndVM.WhWallParamList.IndexOf(Me) > 0 Then
            Return Map.refWarehouseWndVM.WhWallParamList(Map.refWarehouseWndVM.WhWallParamList.IndexOf(Me) - 1).dMaxThickness
         Else
            Return CurrentMachine.dMinRawH
         End If
      End Get
   End Property

   Private m_dOffset As Double
   Public Property dOffset As Double
      Get
         Return m_dOffset
      End Get
      Set(value As Double)
         m_dOffset = value
      End Set
   End Property

   Private m_dMaxThickness As Double
   Public Property dMaxThickness As Double
      Get
         Return m_dMaxThickness
      End Get
      Set(value As Double)
         If Map.refWarehouseWndVM.WhWallParamList.IndexOf(Me) = Map.refWarehouseWndVM.WhWallParamList.Count - 1 Then
            m_dMaxThickness = CurrentMachine.dMaxRawH
            NotifyPropertyChanged(NameOf(dMaxThickness))
         ElseIf value >= Map.refWarehouseWndVM.WhWallParamList(Map.refWarehouseWndVM.WhWallParamList.IndexOf(Me)).dMinThickness And
         ((Map.refWarehouseWndVM.WhWallParamList.IndexOf(Me) < Map.refWarehouseWndVM.WhWallParamList.Count - 1 AndAlso
         value <= Map.refWarehouseWndVM.WhWallParamList(Map.refWarehouseWndVM.WhWallParamList.IndexOf(Me) + 1).dMaxThickness) Or
         Map.refWarehouseWndVM.WhWallParamList.IndexOf(Me) = Map.refWarehouseWndVM.WhWallParamList.Count - 1) Then
            m_dMaxThickness = value
            If Map.refWarehouseWndVM.WhWallParamList.IndexOf(Me) < Map.refWarehouseWndVM.WhWallParamList.Count - 1 Then
               Map.refWarehouseWndVM.WhWallParamList(Map.refWarehouseWndVM.WhWallParamList.IndexOf(Me) + 1).NotifyPropertyChanged(NameOf(dMinThickness))
            End If
         Else
            NotifyPropertyChanged(NameOf(dMaxThickness))
         End If
      End Set
   End Property

   Sub New(Offset As Double, MaxThickness As Double)
      m_dOffset = Offset
      m_dMaxThickness = MaxThickness
   End Sub

End Class

Public Enum WarehouseType
   BASIC = 1
   MEDIUM = 2
   ADVANCED = 3
End Enum
