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

Public Class MyMachGroupPanelVM
   Inherits Core.MyMachGroupPanelVM

#Region "FIELDS & PROPERTIES"

   Public Shadows Property SelectedMachGroup As MyMachGroupVM
      Get
         Return m_SelectedMachGroup
      End Get
      Set(value As MyMachGroupVM)
         m_SelectedMachGroup = value
         If Not IsNothing(value) Then
            OnPreSetCurrMachGroup()
            EgtSetCurrMachGroup(value.Id)
            OnPostSetCurrMachGroup()
         End If
         NotifyPropertyChanged(NameOf(SelectedMachGroup))
      End Set
   End Property

#End Region    ' FIELDS & PROPERTIES

#Region "CONSTRUCTOR"

   Sub New(MachGroupPanelM As MachGroupPanelM)
      MyBase.New(MachGroupPanelM)
      ' elimino copie da cancellare
      DeleteDuplo()
      ' verifico se volume grezzi calcolato
      For Each MachGroup As MyMachGroupVM In m_MachGroupVMList
         Dim bIsCalculated As Boolean = False
         For Each Part In MachGroup.PartVMList
            If Part.dVolume > 0 Then Continue For
            Part.PartM.ReadVolume()
            bIsCalculated = True
         Next
         If bIsCalculated Then
            MachGroup.UpdateUsage()
         End If
      Next
   End Sub

#End Region    ' CONSTRUCTOR

#Region "METHODS"

   Protected Overrides Sub CreateMachGroupVMList()
      Dim all As New List(Of MachGroupVM)
      For Each MachGroupM In m_MachGroupPanelM.GetMachGroups()
         all.Add(New MyMachGroupVM(MachGroupM))
      Next

      For Each MachGroupvM As MachGroupVM In all
         AddHandler MachGroupvM.PropertyChanged, AddressOf OnMachGroupVMPropertyChanged
      Next

      MachGroupVMList = New ObservableCollection(Of MachGroupVM)(all)
      AddHandler MachGroupVMList.CollectionChanged, AddressOf OnMachGroupVMListChanged
   End Sub

   Protected Overrides Sub OnMachGroupAdded(sender As Object, e As MachGroupAddedEventArgs)
      Dim MachGroupVM As MachGroupVM = Nothing
      Select Case DirectCast(e.NewMachGroupM, MyMachGroupM).nType
         Case Core.ConstBeam.MachineType.BEAM, Core.ConstBeam.MachineType.WALL
            MachGroupVM = New MyMachGroupVM(e.NewMachGroupM)
         Case Core.ConstBeam.MachineType.NULL
            Return
      End Select
      MachGroupVMList.Add(MachGroupVM)
      NotifyPropertyChanged(NameOf(MachGroupVMList))
   End Sub

   'Protected Overrides Sub CreateMachGroupVMList()
   '   Dim all As New List(Of MachGroupVM)
   '   For Each MachGroupM In m_MachGroupPanelM.GetMachGroups()
   '      If DirectCast(MachGroupM, MyMachGroupM).nMachineType = MachineType.BEAM Then
   '         all.Add(New BeamMachGroupVM(MachGroupM))
   '      Else
   '         all.Add(New WallMachGroupVM(MachGroupM))
   '      End If
   '   Next

   '   For Each MachGroupvM As MachGroupVM In all
   '      AddHandler MachGroupvM.PropertyChanged, AddressOf OnMachGroupVMPropertyChanged
   '   Next

   '   MachGroupVMList = New ObservableCollection(Of MachGroupVM)(all)
   '   AddHandler MachGroupVMList.CollectionChanged, AddressOf OnMachGroupVMListChanged
   'End Sub

   'Public Overrides Function InitMachGroupList(Optional bUseDefaults As Boolean = False) As Boolean
   '   ' Svuoto precedente lista di MachGroup
   '   MachGroupVMList.Clear()
   '   ' aggiorno copie
   '   MyMachGroupPanelM.UpdateDuplo()
   '   ' verifico se esistono già gruppi di lavorazione o se devo creare il primo
   '   Dim bOk As Boolean = False
   '   Dim nId = EgtGetFirstMachGroup()
   '   If nId <> GDB_ID.NULL Then
   '      bOk = EgtSetCurrMachGroup(nId)
   '   End If
   '   If Not bOk Then Return True
   '   ' carico lista dei gruppi di lavorazione
   '   m_MachGroupPanelM.MachGroupMList = MyMachGroupPanelM.LoadMyMachGroups(Map.refMachinePanelVM.MachineList.ToList())
   '   ' elimino copie da cancellare
   '   MyMachGroupPanelM.DeleteDuplo()
   '   If bOk Then SelectedMachGroup = MachGroupVMList(0)
   '   EgtZoom(ZM.ALL)
   '   Return True
   'End Function

   Public Function RefreshMachGroupList() As Boolean
      ' carico lista dei gruppi di lavorazione
      For Each MachGroup As MyMachGroupM In MyMachGroupPanelM.UpdateFromNestingMyMachGroups(Map.refMachinePanelVM.MachineList.ToList())
         m_MachGroupPanelM.AddMachGroup(MachGroup)
      Next
      ' aggiorno contatore pezzi in produzione
      For Each MachGroup As MyMachGroupVM In MachGroupVMList
         For Each Part In MachGroup.PartVMList
            ' aggiorno calcolo pezzi in produzione
            Dim BTLPart As BTLPartVM = BTLPartVM.RetrieveBTLPartFromPart(Part.nPartId)
            If Not IsNothing(BTLPart) Then BTLPart.RefreshPartInProd()
         Next
      Next
      ' seleziono primo MachGroup
      If MachGroupVMList.Count > 0 Then SelectedMachGroup = MachGroupVMList(0)
      EgtZoom(ZM.ALL)
      Return True
   End Function

   ' funzione utilizzata per caricare in lista gruppo aggiunto al progetto dal supervisore
   Public Function AddMachGroupToList(nId As Integer) As Boolean
      ' carico  gruppo di lavorazione
      Dim MachGroup As MyMachGroupM = MyMachGroupPanelM.LoadMyMachGroupFromId(nId, Map.refMachinePanelVM.MachineList.ToList())
      If Not IsNothing(MachGroup) Then
         m_MachGroupPanelM.AddMachGroup(MachGroup)
         Return True
      End If
      Return False
   End Function

   ' funzione che cancella tutti i pezzi segnati da eliminare
   Public Sub DeleteDuplo()
      ' reset necessario per poter accedere direttamente al grezzo dalle info pezzo e al MachGroup tramite la gerarchia Db geometrico
      EgtResetCurrMachGroup()
      Dim bDuploToDeleteFound As Boolean = False
      Dim nPartId As Integer = EgtGetFirstPart()
      While nPartId <> GDB_ID.NULL
         Dim bIsToDelete As Boolean = False
         If DuploGetToDelete(nPartId, bIsToDelete) AndAlso bIsToDelete Then
            ' verifico se ci sono copie
            Dim nDuploCount As Integer = 0
            EgtDuploCount(nPartId, nDuploCount)
            Dim DuploList As New List(Of Integer)
            If nDuploCount > 0 AndAlso EgtDuploList(nPartId, DuploList) Then
               bDuploToDeleteFound = True
               ' cancello tutti i pezzi copia nelle barre
               For Each nDuploId In DuploList
                  ' recupero grezzo cui appartiene
                  Dim nRawPartId As Integer = MyMachGroupPanelM.DuploGetRawPart(nDuploId)
                  ' recupero gruppo di lavorazione 
                  Dim nMachGroupId As Integer = EgtGetParent(EgtGetParent(nRawPartId))
                  ' lo setto come corrente
                  EgtSetCurrMachGroup(nMachGroupId)
                  ' elimino pezzo copia
                  EgtRemovePartFromRawPart(nDuploId)
                  EgtErase(nDuploId)
                  ' recupero gruppo di lavorazione e trave dell'interfaccia
                  Dim BeamMachGroup As MyMachGroupVM = MachGroupVMList.FirstOrDefault(Function(x) x.Id = nMachGroupId)
                  If Not IsNothing(BeamMachGroup) Then
                     Dim Beam As PartVM = BeamMachGroup.PartVMList.FirstOrDefault(Function(x) x.nPartId = nDuploId)
                     EgtSetCurrMachGroup(BeamMachGroup.Id)
                     Beam.DeletePart()
                  End If
                  ' reset necessario per poter ottenere nMachGroupId corretto
                  EgtResetCurrMachGroup()
                  ' aggiorno dati utilizzo barra
                  BeamMachGroup.UpdateUsage()
               Next
               ' aggiorno quantita' in prod
               Dim BTLPart As BTLPartVM = CALCPanelVM.GetBTLPartVMFromBTLPartId(nPartId)
               BTLPart.RefreshPartInProd()
            End If
            DuploResetToDelete(nPartId)
         End If
         nPartId = EgtGetNextPart(nPartId)
      End While
      ' se cancellato almeno un pezzo
      If bDuploToDeleteFound Then
         Map.refProdManagerVM.Save()
      End If
      EgtResetCurrMachGroup()
   End Sub

   Friend Shared Function DuploGetToDelete(nSouId As Integer, ByRef bToDelete As Boolean) As Boolean
      If IsNothing(nSouId) Then Return False
      Return EgtGetInfo(nSouId, DUPLO_TODELETE, bToDelete)
   End Function

   Public Shared Function DuploSetToDelete(nSouId As Integer) As Boolean
      If IsNothing(nSouId) Then Return False
      Return EgtSetInfo(nSouId, DUPLO_TODELETE, True)
   End Function

   Private Shared Function DuploResetToDelete(nSouId As Integer) As Boolean
      If IsNothing(nSouId) Then Return False
      Return EgtSetInfo(nSouId, DUPLO_TODELETE, "")
   End Function

   Public Overrides Sub AddMachGroup()
      Dim MyMachGroupM As MyMachGroupM = m_MyMachGroupPanelM.NewMyMachGroup(CurrentMachine.sMachineName, Map.refProjectVM.BTLStructureVM.nPROJTYPE)
      ' e lo seleziono
      SelectedMachGroup = MachGroupVMList.FirstOrDefault(Function(x) x.MachGroupM Is MyMachGroupM)
   End Sub

   ' funzione che seleziona primo gruppo
   Friend Sub SelFirstMachGroup()
      If Not IsNothing(Me) AndAlso Not IsNothing(MachGroupVMList) AndAlso MachGroupVMList.Count > 0 Then
         SelectedMachGroup = MachGroupVMList(0)
      Else
         SelectedMachGroup = Nothing
         EgtResetCurrMachGroup()
         ' nascondo tutti i pezzi
         Map.refProjectVM.BTLStructureVM.HideAll()
      End If
   End Sub

   Public Overrides Function OnPostSetCurrMachGroup() As Boolean
      ' resetto eventuale selezione pezzo
      Dim SelMachGroup As MyMachGroupVM = DirectCast(SelectedMachGroup, MyMachGroupVM)
      SelMachGroup.SetSelPart(Nothing)
      ' Imposto vista solo tavola
      EgtSetMachineLook(MCH_LOOK.TAB)
      Core.ViewPanelVM.BWSetView(If(Core.ViewPanelVM.Type = BWType.BEAM, VT.ISO_SW, VT.TOP), False)
      EgtZoom(ZM.ALL)
      Return True
   End Function

#End Region    ' METHODS

End Class
