Imports System.Collections.ObjectModel
Imports System.IO
Imports EgtUILib
Imports EgtWPFLib5
Imports EgtBEAMWALL.Core

Public Class AddSectionXMaterialWndVM
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

   Friend Event m_CloseWindow(bDialogResult As Boolean)

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

   ' Lista dei nuovi SectionXMaterial in BTLSTructure non presenti in Warehouse
   Private m_NewSectionXMaterialList As New ObservableCollection(Of SectionXMaterialToBeAdded)
   Public Property NewSectionXMaterialList As ObservableCollection(Of SectionXMaterialToBeAdded)
      Get
         Return m_NewSectionXMaterialList
      End Get
      Set(value As ObservableCollection(Of SectionXMaterialToBeAdded))
         m_NewSectionXMaterialList = value
      End Set
   End Property

   ' Lista dei SectionXMaterial presenti in Warehouse
   Private m_WhSectXMatList As New List(Of SectionXMaterial)
   Public Property WhSectXMatList As List(Of SectionXMaterial)
      Get
         Return m_WhSectXMatList
      End Get
      Set(value As List(Of SectionXMaterial))
         m_WhSectXMatList = value
      End Set
   End Property

   ' SectionXMaterial correntemente selezionato nella Datagrid 
   Private m_SelNewSectionXMaterial As SectionXMaterialToBeAdded
   Public Property SelNewSectionXMaterial As SectionXMaterialToBeAdded
      Get
         Return m_SelNewSectionXMaterial
      End Get
      Set(value As SectionXMaterialToBeAdded)
         m_SelNewSectionXMaterial = value
      End Set
   End Property

   ' Definizione comandi
   Private m_cmdOk As ICommand
   Private m_cmdCancel As ICommand

#End Region    ' FIELDS & PROPERTIES

#Region "MESSAGES"

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

   Public ReadOnly Property Alias_Msg As String
      Get
         Return EgtMsg(61772)
      End Get
   End Property

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

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

   Public ReadOnly Property Material_Msg As String
      Get
         Return EgtMsg(61607)
      End Get
   End Property

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

   Public ReadOnly Property Ok_Msg As String
      Get
         Return EgtMsg(61761)
      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 IncorrectValues_Msg As String
      Get
         Return EgtMsg(61768)
      End Get
   End Property

#End Region    ' MESSAGES

#Region "CONSTRUCTOR"

   Sub New(SectXMatList As List(Of SectionXMaterialToBeAdded))
      ' Aggiungo riferimento a Map
      Map.SetRefAddSectionXMaterialWndVM(Me)
      ' carico le colonne della datagrid
      LoadColumns(Map.refProjectVM.BTLStructureVM.nPROJTYPE)
      NewSectionXMaterialList = New ObservableCollection(Of SectionXMaterialToBeAdded)(SectXMatList)
      WhSectXMatList = WarehouseWndVM.GetSectionXMaterialsFromWarehouse()
      ' Prendo i SectionXMaterial appena messi nella Lista di questa finestra e li confronto con ciascuno di quelli presenti in Warehouse.
      ' Se le sezioni sono uguali carico i materiali relativi alla medesima sezione.
      ' Se i materiali di quella medesima sezione non ci sono disabilito la CheckBox Alias.
      For Each NewSectXMatItem In NewSectionXMaterialList
         NewSectXMatItem.Alias_IsChecked = False
         For Each SectionListItem In WhSectXMatList
            If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then
               If NewSectXMatItem.dW = SectionListItem.dW AndAlso NewSectXMatItem.dH = SectionListItem.dH Then
                  ' Se le sezioni sono uguali allora indago sui materiali
                  For Index As Integer = 0 To SectionListItem.sMaterial.Count - 1
                     If Not NewSectXMatItem.MaterialForSameSection_List.Contains(SectionListItem.sMaterial(Index)) And
                        Not NewSectXMatItem.sMaterial.Contains(SectionListItem.sMaterial(Index)) Then
                        NewSectXMatItem.MaterialForSameSection_List.Add(SectionListItem.sMaterial(Index))
                     End If
                     If NewSectXMatItem.MaterialForSameSection_List.Count > 0 Then
                        NewSectXMatItem.Alias_IsEnabled = True
                     End If
                  Next
               End If
            ElseIf Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL Then
               If NewSectXMatItem.dH = SectionListItem.dH Then
                  ' Se le sezioni sono uguali allora indago sui materiali
                  For Index As Integer = 0 To SectionListItem.sMaterial.Count - 1
                     If Not NewSectXMatItem.MaterialForSameSection_List.Contains(SectionListItem.sMaterial(Index)) And
                        Not NewSectXMatItem.sMaterial.Contains(SectionListItem.sMaterial(Index)) Then
                        NewSectXMatItem.MaterialForSameSection_List.Add(SectionListItem.sMaterial(Index))
                     End If
                     If NewSectXMatItem.MaterialForSameSection_List.Count > 0 Then
                        NewSectXMatItem.Alias_IsEnabled = True
                     End If
                  Next
               End If
            End If
         Next
      Next
   End Sub

#End Region    ' CONSTRUCTOR

#Region "COMMANDS"

#Region "Ok"

   Public ReadOnly Property Ok_Command As ICommand
      Get
         If m_cmdOk Is Nothing Then
            m_cmdOk = New Command(AddressOf Ok)
         End If
         Return m_cmdOk
      End Get
   End Property

   Public Sub Ok()
      ' Se i valori non sono corretti segnalo l'errore e impedisco il salvataggio
      For Index As Integer = 0 To NewSectionXMaterialList.Count - 1
         If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM AndAlso
            (Not NewSectionXMaterialList(Index).Alias_IsChecked And NewSectionXMaterialList(Index).dNewL < 0) OrElse
            (NewSectionXMaterialList(Index).Alias_IsChecked And IsNothing(NewSectionXMaterialList(Index).SelMaterialForSameSection)) OrElse
            (Not NewSectionXMaterialList(Index).Alias_IsChecked And (NewSectionXMaterialList(Index).nQuantity < 0 Or NewSectionXMaterialList(Index).nQuantity > 99)) Then
            MessageBox.Show(IncorrectValues_Msg, Error_Msg)
            Return
         End If
         If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL AndAlso
            (Not NewSectionXMaterialList(Index).Alias_IsChecked And (NewSectionXMaterialList(Index).dNewW < 0 Or NewSectionXMaterialList(Index).dNewL < 0)) OrElse
            (NewSectionXMaterialList(Index).Alias_IsChecked And IsNothing(NewSectionXMaterialList(Index).SelMaterialForSameSection)) OrElse
            (Not NewSectionXMaterialList(Index).Alias_IsChecked And (NewSectionXMaterialList(Index).nQuantity < 0 Or NewSectionXMaterialList(Index).nQuantity > 99)) Then
            MessageBox.Show(IncorrectValues_Msg, Error_Msg)
            Return
         End If
      Next
      ' Se i valori sono corretti salvo
      Save()
   End Sub

#End Region ' Ok

#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()
      ' Resetto i valori prima di salvarli
      For Index As Integer = 0 To NewSectionXMaterialList.Count - 1
         NewSectionXMaterialList(Index).Alias_IsChecked = False
         NewSectionXMaterialList(Index).dNewL = 0
         NewSectionXMaterialList(Index).nQuantity = 0
         If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL Then NewSectionXMaterialList(Index).dNewW = 0
      Next
      Save()
   End Sub

#End Region ' Cancel

#End Region    ' CONSTRUCTOR

#Region "METHODS"

#Region "LoadColumns"

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

#End Region     ' LoadColumns

#Region "Save"

   Public Sub Save()

      ' Indice per scansionare NewSectionXMaterialList
      Dim Index = 0
      ' Indice da incrementare in modo da scrivere i nuovi parametri L/S a partire dal giusto numero
      Dim ParamIndex = 1
      ' Indice da incrementare in modo da scrivere i nuovi SectionXMaterial a partire dal giusto numero
      Dim SxMParamIndex = 1

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

      If Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.BEAM Then

         ' Ciclo sui parametri L esistenti nell'INI in modo da avere l'indice di partenza per gli ulteriori parametri da scrivere
         Dim sValue As String = String.Empty
         While EgtUILib.GetPrivateProfileString(S_BEAM, "L" & ParamIndex, String.Empty, sValue, sWarehousePath)
            ParamIndex += 1
         End While
         ' Ciclo sui SectionXMaterial BEAM esistenti nell'INI in modo da avere l'indice di partenza per gli ulteriori parametri da scrivere
         While EgtUILib.GetPrivateProfileString(S_BEAM_LIST, SxMParamIndex, String.Empty, sValue, sWarehousePath)
            SxMParamIndex += 1
         End While

         ' Se i valori sono tutti >= 0 li scrivo nel file INI
         Index = 0
         For Each NewSxMItem In NewSectionXMaterialList
            Dim sAlias As String = String.Empty
            For Each Mat In NewSectionXMaterialList(Index).sMaterial
               sAlias &= Mat & ";"
            Next
            sAlias = sAlias.TrimEnd(";")
            ' Se Alias è checkato aggiungo l'Alias scelto dalla ComboBox alla stringa e setto NewL e Quantity a 0 e aggiorno il SectionXmaterial in BeamList
            If NewSxMItem.Alias_IsChecked Then
               sAlias &= ";" & NewSxMItem.SelMaterialForSameSection
               NewSectionXMaterialList(Index).dNewL = 0
               NewSectionXMaterialList(Index).nQuantity = 0
               ' Ricavo l'indice del SectionXMaterial da aggiornare
               Dim UpdateSxMIndex = 1
               For Each WhSectXMat In WhSectXMatList
                  If WhSectXMat.dW = NewSxMItem.dW AndAlso WhSectXMat.dH = NewSxMItem.dH Then
                     Exit For
                  End If
                  UpdateSxMIndex += 1
               Next
               ' Aggiorno il SectionXMaterial
               WritePrivateProfileString(S_BEAM_LIST, UpdateSxMIndex, LenToString(NewSectionXMaterialList(Index).dW, 3) & "," &
                                                                      LenToString(NewSectionXMaterialList(Index).dH, 3) & "," &
                                                                      sAlias, sWarehousePath)
            Else
               ' Altrimenti aggiungo il nuovo SectionXMaterial e il parametro
               WritePrivateProfileString(S_BEAM_LIST, SxMParamIndex, LenToString(NewSectionXMaterialList(Index).dW, 3) & "," &
                                                                     LenToString(NewSectionXMaterialList(Index).dH, 3) & "," &
                                                                     sAlias, sWarehousePath)
               WritePrivateProfileString(S_BEAM, K_L & ParamIndex, SxMParamIndex & "," &
                                                                   LenToString(NewSectionXMaterialList(Index).dNewL, 3) & "," &
                                                                   NewSectionXMaterialList(Index).nQuantity, sWarehousePath)
               ParamIndex += 1
               SxMParamIndex += 1
            End If

            Index += 1
         Next
      ElseIf Map.refProjectVM.BTLStructureVM.nPROJTYPE = MachineType.WALL Then

         ' Ciclo sui parametri S esistenti nell'INI in modo da avere l'indice di partenza per gli ulteriori parametri da scrivere
         Dim sValue As String = String.Empty
         While EgtUILib.GetPrivateProfileString(S_WALL, "S" & ParamIndex, String.Empty, sValue, sWarehousePath)
            ParamIndex += 1
         End While
         ' Ciclo sui SectionXMaterial WALL esistenti nell'INI in modo da avere l'indice di partenza per gli ulteriori parametri da scrivere
         While EgtUILib.GetPrivateProfileString(S_WALL_LIST, SxMParamIndex, String.Empty, sValue, sWarehousePath)
            SxMParamIndex += 1
         End While

         ' Se i valori sono tutti > 0 li scrivo nel file INI
         Index = 0
         For Each NewSxMItem In NewSectionXMaterialList
            Dim sAlias As String = String.Empty
            For Each Mat In NewSectionXMaterialList(Index).sMaterial
               sAlias &= Mat & ";"
            Next
            sAlias = sAlias.TrimEnd(";")
            ' Se Alias è checkato aggiungo l'Alias scelto dalla ComboBox alla stringa
            If NewSxMItem.Alias_IsChecked Then
               sAlias &= ";" & NewSxMItem.SelMaterialForSameSection
               NewSectionXMaterialList(Index).dNewW = 0
               NewSectionXMaterialList(Index).dNewL = 0
               NewSectionXMaterialList(Index).nQuantity = 0
               ' Ricavo l'indice del SectionXMaterial da aggiornare
               Dim UpdateSxMIndex = 1
               For Each WhSectXMat In WhSectXMatList
                  If WhSectXMat.dH = NewSxMItem.dH Then
                     Exit For
                  End If
                  UpdateSxMIndex += 1
               Next
               ' Aggiorno il SectionXMaterial
               WritePrivateProfileString(S_WALL_LIST, UpdateSxMIndex, LenToString(NewSectionXMaterialList(Index).dH, 3) & "," &
                                                                      sAlias, sWarehousePath)
            Else
               ' Altrimenti aggiungo il nuovo SectionXMaterial e il parametro
               WritePrivateProfileString(S_WALL_LIST, SxMParamIndex, LenToString(NewSectionXMaterialList(Index).dH, 3) & "," &
                                                                     sAlias, sWarehousePath)
               WritePrivateProfileString(S_WALL, K_S & ParamIndex, SxMParamIndex & "," &
                                                                   LenToString(NewSectionXMaterialList(Index).dNewW, 3) & "," &
                                                                   LenToString(NewSectionXMaterialList(Index).dNewL, 3) & "," &
                                                                   NewSectionXMaterialList(Index).nQuantity, sWarehousePath)
               ParamIndex += 1
               SxMParamIndex += 1
            End If

            Index += 1
         Next
      End If

      RaiseEvent m_CloseWindow(True)
   End Sub

#End Region ' Save

#End Region    ' METHODS

End Class