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

Public Class WallMachGroupVM
   Inherits Core.WallMachGroupVM

#Region "CONSTRUCTORS"

   Sub New(WallMachGroupM As WallMachGroupM)
      MyBase.New(WallMachGroupM)
   End Sub

#End Region    ' CONSTRUCTORS

#Region "METHODS"

   Friend Function ReDrawWallMachgroup() As Boolean
      ' scrivo dati di tutti i pezzi
      Dim nIndex As Integer
      For nIndex = 0 To PartVMList.Count - 1
         Dim Part As WallVM = PartVMList(nIndex)
         EgtSetInfo(Id, MGR_RPT_PART & nIndex + 1, PartVMList(nIndex).nPartId & "," & Part.sPOSX & "," &
                                                   Part.sPOSY & "," & Part.PartM.nROTATED & "," & Part.PartM.nINVERTED)
      Next
      ' elimino eventuali successive info pezzi di troppo
      nIndex = PartVMList.Count + 1
      Dim sTemp As String = ""
      While EgtGetInfo(Id, MGR_RPT_PART & nIndex, sTemp)
         EgtSetInfo(Id, MGR_RPT_PART & nIndex, "")
         nIndex += 1
      End While
      ' elimino vecchio grezzo ed eseguo script creazione nuovo
      Dim nRawId As Integer = EgtGetFirstRawPart()
      While nRawId <> GDB_ID.NULL
         EgtRemoveRawPart(nRawId)
         nRawId = EgtGetFirstRawPart()
      End While
      If Not ExecWall(Map.refMainWindowVM.MainWindowM.sTempDir, Map.refMachinePanelVM.SelectedMachine.Name, CalcIntegration.CmdType.RAWPART, False) Then Return False
      Return True
   End Function

   Public Overrides Function Copy() As Core.MyMachGroupVM
      Return Nothing
   End Function

#End Region    ' METHODS

   Protected Overrides Sub CreateWallVMList()
      Dim all As List(Of WallVM) = (From WallM In m_WallMachGroupM.GetParts()
                                    Select New WallVM(WallM, Me)).ToList()

      For Each WallVM As WallVM In all
         AddHandler WallVM.PropertyChanged, AddressOf OnWallVMPropertyChanged
      Next

      m_PartVMList = New ObservableCollection(Of Core.PartVM)(all)
      AddHandler m_PartVMList.CollectionChanged, AddressOf OnWallVMListChanged
   End Sub

   Protected Overrides Sub OnWallAdded(sender As Object, e As PartAddedEventArgs)
      Dim WallVM As WallVM = New WallVM(e.NewPart, Me)
      PartVMList.Add(WallVM)
   End Sub

   Public Overrides Sub RefreshPartList()
   End Sub

   Public Overrides Sub RefreshGroupData()
   End Sub

   Friend Sub UpdateUsage()
      m_WallMachGroupM.SetTotMat(dL * dW)
      m_WallMachGroupM.SetMatForPart(0)
      For Each Part In PartVMList
         m_WallMachGroupM.SetMatForPart(m_WallMachGroupM.dMatForPart + (Part.dL * Part.dW))
      Next
      NotifyPropertyChanged(NameOf(dUsage))
      NotifyPropertyChanged(NameOf(dWaste))
   End Sub

#Region "COMMANDS"

#Region "DeleteMachGroup"

   ' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded.
   Public Overrides Sub DeleteMachGroup()
      ' elimino tutte le copie
      Dim nRawPartId As Integer = EgtGetFirstRawPart()
      Dim nWallId As Integer = EgtGetFirstPartInRawPart(nRawPartId)
      While nWallId <> GDB_ID.NULL
         If EgtRemovePartFromRawPart(nWallId) Then
            Dim BTLPart As BTLPartVM = BTLPartVM.RetrieveBTLPartFromPart(nWallId)
            EgtErase(nWallId)
            If Not IsNothing(BTLPart) Then BTLPart.RefreshPartInProd()
         End If
         nWallId = EgtGetFirstPartInRawPart(nRawPartId)
      End While
      ' elimino MachGroup
      EgtRemoveMachGroup(Me.Id)
      ' rimuovo dalla lista grezzi
      Dim Index As Integer = Map.refMachGroupPanelVM.MachGroupVMList.IndexOf(Me)
      If Index = 0 Then
         If Map.refMachGroupPanelVM.MachGroupVMList.Count > 1 Then
            Map.refMachGroupPanelVM.SelectedMachGroup = Map.refMachGroupPanelVM.MachGroupVMList(0)
         Else
            Map.refMachGroupPanelVM.SelectedMachGroup = Nothing
            EgtDraw()
         End If
      ElseIf Index = Map.refMachGroupPanelVM.MachGroupVMList.Count - 1 Then
         If Map.refMachGroupPanelVM.MachGroupVMList.Count > 1 Then
            Map.refMachGroupPanelVM.SelectedMachGroup = Map.refMachGroupPanelVM.MachGroupVMList(Map.refMachGroupPanelVM.MachGroupVMList.Count - 2)
         Else
            Map.refMachGroupPanelVM.SelectedMachGroup = Nothing
            EgtDraw()
         End If
      Else
         Map.refMachGroupPanelVM.SelectedMachGroup = Map.refMachGroupPanelVM.MachGroupVMList(Index - 1)
      End If
      Map.refMachGroupPanelVM.MachGroupVMList.Remove(Me)
   End Sub

#End Region ' DeleteMachGroup

#End Region    ' COMMANDS

End Class
