Imports EgtBEAMWALL.Core
Imports EgtBEAMWALL.DataLayer.DatabaseModels

Public Class ViewerOptimizerComm

   Private m_bTickInExecution As Boolean = False

   Sub New()
      ' imposto in Map
      'Map.SetRefMachManaging(Me)
   End Sub

   Friend Sub Timer_Tick()
      If Not IsNothing(Map.refProjectVM.SupervisorMachGroupPanelVM) AndAlso Map.refProjectVM.SupervisorMachGroupPanelVM.bReloadingProj Then
         EgtUILib.EgtOutLog("Ricaricamento programma in esecuzione!!")
         Return
      End If
      If m_bTickInExecution Then
         EgtUILib.EgtOutLog("Tick gia' in esecuzione!!")
         Return
      End If
      m_bTickInExecution = True
      ' se c'e' un progetto aperto o in caricamento
      If IsNothing(Map.refSupervisorManagerVM.CurrProd) OrElse Map.refSupervisorManagerVM.bLoadingProd Then
         m_bTickInExecution = False
         Return
      End If
      ' recupero indice modifica del prod
      Dim nModificationIndex As Integer = 0
      Dim ActiveSessionList As List(Of StatusMapModel) = DbControllers.m_StatusMapController.GetProd(m_SupervisorId)
      For Each ActiveSession In ActiveSessionList
         If ActiveSession.ItemId = Map.refSupervisorManagerVM.CurrProd.nProdId Then
            nModificationIndex = ActiveSession.Index
         End If
      Next
      ' se indice letto diverso da indice gia' presente
      If nModificationIndex <> Map.refSupervisorManagerVM.CurrProd.nModificationIndex Then
         ' recupero elementi modificati
         Dim MachGroupList As List(Of StatusMapModel) = DbControllers.m_StatusMapController.GetFrom(m_SupervisorId, Map.refSupervisorManagerVM.CurrProd.nModificationIndex + 1)
         Dim bReloadFile As Boolean = False
         For Each MachGroupModification In MachGroupList
            If MachGroupModification.ItemType = Core.StatusMapItemType.MachGroup Then
               Select Case MachGroupModification.Operation
                  Case Core.StatusMapOpType.MachGroupAdd
                     Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupFullList.Add(MachGroupModification.ItemId, {MachGroupModification.Index, MachGroupModification.Index})
                  Case Core.StatusMapOpType.MachGroupMod
                     If Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupFullList.ContainsKey(MachGroupModification.ItemId) Then
                        Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupFullList(MachGroupModification.ItemId)(0) = MachGroupModification.Index
                     Else
                        Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupFullList.Add(MachGroupModification.ItemId, {MachGroupModification.Index, 0})
                     End If
                  Case Core.StatusMapOpType.MachGroupRem
                     If Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupFullList.ContainsKey(MachGroupModification.ItemId) Then
                        Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupFullList.Remove(MachGroupModification.ItemId)
                     End If
                  Case Core.StatusMapOpType.MachGroupAssignedToSupervisor
                     Dim MachgroupIndexes As Integer() = Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupFullList(MachGroupModification.ItemId)
                     ' se indice di aggiornamento file diverso da indice ultima modifica
                     If MachgroupIndexes(0) <> MachgroupIndexes(1) Then
                        ' devo ricaricare il file
                        bReloadFile = True
                        ' ricarico il progetto
                        Map.refProjectVM.SupervisorMachGroupPanelVM.ReloadProjectFile()
                        ' esco aggiornando l'indice a quello corrente
                        Map.refSupervisorManagerVM.CurrProd.SetModificationIndex(MachGroupModification.Index)
                        Exit For
                     Else
                        Dim ProdMachGroup As MyMachGroupVM = Map.refProjectVM.ProdMachGroupPanelVM.MachGroupVMList.FirstOrDefault(Function(x) x.Id = MachGroupModification.ItemId)
                        If Not IsNothing(ProdMachGroup) Then
                           ProdMachGroup.dtStartTime = DateTime.MinValue
                           ProdMachGroup.dtEndTime = DateTime.MinValue
                           ProdMachGroup.MyMachGroupM.SetProductionState(Core.ItemState.Assigned)
                           Dim DBMachGroup As MachGroupModel = DbControllers.m_MachGroupController.FindByMachGroupId(Map.refSupervisorManagerVM.CurrProd.nProdId, ProdMachGroup.Id)
                           If Not IsNothing(DBMachGroup) Then ProdMachGroup.SetProductionIndex(DBMachGroup.ProdIndex)
                           For Each Part In ProdMachGroup.PartVMList
                              Part.dtStartTime = DateTime.MinValue
                              Part.dtEndTime = DateTime.MinValue
                              Part.nProduction_State = Core.ItemState.Assigned
                           Next
                           ' lo aggiungo alla lista di MachGroup
                           Map.refProjectVM.SupervisorMachGroupPanelVM.AddMachGroupToSupervisor(ProdMachGroup)
                        End If
                     End If
               End Select
            ElseIf MachGroupModification.ItemType = Core.StatusMapItemType.Comm Then
               If MachGroupModification.Operation = Core.StatusMapOpType.ViewOptimStoped Then
                  ViewerOptimizerCommThread.SetViewerOptimizerBlocked(True)
               ElseIf MachGroupModification.Operation = Core.StatusMapOpType.ChangeProdInSupervisorRequest Then
                  ' lo apro
                  Map.refSupervisorManagerVM.OpenProjectFromOptimizer(MachGroupModification.ItemId)
               End If
            End If
         Next
         ' se devo ricaricare il file
         If bReloadFile Then
            ' aggiorno indici in dictionary
            For Each MachGroupModification In Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupFullList.Where(Function(x) x.Value(0) <> x.Value(1))
               MachGroupModification.Value(1) = MachGroupModification.Value(0)
            Next
         End If
         If Not bReloadFile Then
            ' aggiorno indice del prod
            Map.refSupervisorManagerVM.CurrProd.SetModificationIndex(nModificationIndex)
         End If
      End If
      m_bTickInExecution = False
   End Sub

End Class
