Imports System.Windows.Threading
Imports EgtBEAMWALL.Core
Imports EgtBEAMWALL.DataLayer.DatabaseModels
Imports EgtUILib
Imports EgtWPFLib5

Public Class SupervisorMachGroupPanelVM
   Inherits MyMachGroupPanelVM

#Region "FIELDS & PROPERTIES"

   Private m_ReloadProj_Timer As New DispatcherTimer
   Private m_bReloadProj As Boolean = False
   Private m_bReloadingProj As Boolean = False
   Public ReadOnly Property bReloadingProj As Boolean
      Get
         Return m_bReloadingProj
      End Get
   End Property

   Private m_MachGroupVMList_View As ListCollectionView = Nothing
   Public ReadOnly Property MachGroupVMList_View As ListCollectionView
      Get
         Return m_MachGroupVMList_View
      End Get
   End Property

   ' lock per aggiunta elemento a lista MachGroup
   Private m_Lock_AddMachGroup As New Object

   ' Dictionari delle modifiche sui machgroup
   ' key => LastModificationIndex, LastFileLoadIndex
   Private m_MachGroupFullList As New Dictionary(Of Integer, Integer())
   Public Property MachGroupFullList As Dictionary(Of Integer, Integer())
      Get
         Return m_MachGroupFullList
      End Get
      Set(value As Dictionary(Of Integer, Integer()))
         m_MachGroupFullList = value
      End Set
   End Property

#End Region    ' FIELDS & PROPERTIES

#Region "CONSTRUCTOR"
   Private m_MachGroupVMListComparer As MyComparer
   Friend ReadOnly Property MachGroupVMListComparer As MyComparer
      Get
         Return m_MachGroupVMListComparer
      End Get
   End Property

   Friend Sub SetMachGroupVMListView()
      m_MachGroupVMList_View = CollectionViewSource.GetDefaultView(m_MachGroupVMList)
      m_MachGroupVMListComparer = New MyComparer
      m_MachGroupVMList_View.CustomSort = m_MachGroupVMListComparer
      NotifyPropertyChanged(NameOf(MachGroupVMList_View))
   End Sub

   Friend Sub RefreshMachGroupVMSort()
      m_MachGroupVMList_View.CustomSort = m_MachGroupVMListComparer
   End Sub

   Sub New(MachGroupPanelM As MachGroupPanelM)
      MyBase.New(MachGroupPanelM)
      ' riferimento su map
      Map.SetRefSupervisorMachGroupPanelVM(Me)
      BindingOperations.EnableCollectionSynchronization(m_MachGroupVMList, m_Lock_AddMachGroup)
      m_ReloadProj_Timer.Interval = TimeSpan.FromMilliseconds(500)
      AddHandler m_ReloadProj_Timer.Tick, AddressOf ReloadProject_Tick
      m_ReloadProj_Timer.Start()
   End Sub

#End Region    ' CONSTRUCTOR

#Region "METHODS"

   Public Sub AddMachGroupToSupervisor(MachGroup As MyMachGroupVM)
      If MachGroupVMList.Count = 0 Then
         ManageBTLParts.ShowAll()
      End If
      If Not MachGroupVMList.Contains(MachGroup) Then
         SyncLock (m_Lock_AddMachGroup)
            MachGroupVMList.Add(MachGroup)
         End SyncLock
      End If
      ' notifico stato
      MachGroup.NotifyPropertyChanged(NameOf(MachGroup.Produce_IsEnabled))
   End Sub

   Public Sub ReloadProjectFile()
      m_bReloadProj = True
   End Sub

   Public Sub ReloadProject_Tick()
      If Not m_bReloadProj Then Return
      m_bReloadingProj = True
      m_bReloadProj = False
      Map.refSceneHostVM.MainController.ResetModified()
      Map.refSceneHostVM.MainController.OpenProject(Map.refSupervisorManagerVM.CurrProd.sProdPath, False)
      RefreshSupervisorMachGroups(Map.refMachinePanelVM.MachineList.ToList())
      m_bReloadingProj = False
   End Sub

#End Region    ' CONSTRUCTOR

#Region "METHODS"

   Public Shared Sub RefreshSupervisorMachGroups(MachineList As List(Of Machine))
      ' Carico tutti i gruppi di lavorazione aggiunti al progetto
      Dim nId = EgtGetFirstMachGroup()
      While nId <> GDB_ID.NULL
         '  se gia' presente, vado al prossimo
         If Map.refProjectVM.ProdMachGroupPanelVM.MachGroupVMList.Any(Function(x) x.Id = nId) Then
            nId = EgtGetNextMachGroup(nId)
            Continue While
         End If
         ' altrimenti lo carico
         EgtSetCurrMachGroup(nId)
         Dim sName As String = String.Empty
         Dim sMachine As String = String.Empty
         EgtGetMachGroupName(nId, sName)
         EgtGetMachGroupMachineName(nId, sMachine)
         ' cerco la macchina tra quelle presenti
         Dim UsedMachine As MyMachine = Nothing
         Dim bOk As Boolean = Machine.SearchMachine(sMachine, MachineList, UsedMachine)
         If Not bOk OrElse UsedMachine.nType = MachineType.NULL Then
            EgtOutLog("Machine incompatible with beam & wall machining!!")
         Else
            Map.refProjectVM.ProdMachGroupPanelVM.m_MyMachGroupPanelM.AddMachGroup(MyMachGroupM.CreateMyMachGroup(UsedMachine.nType, nId, sName, sMachine))
         End If
         nId = EgtGetNextMachGroup(nId)
      End While
      EgtResetCurrMachGroup()
      ' carico solo elementi passati al supervisore
      Dim TempList As List(Of MachGroupModel) = DbControllers.m_MachGroupController.GetByProdSupervisor(Map.refSupervisorManagerVM.CurrProd.nProdId, m_SupervisorId)
      For Each DBMachGroup In TempList
         If Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.Any(Function(x) x.Id = DBMachGroup.MachGroupId) Then Continue For
         Dim MachGroup As MyMachGroupVM = Map.refProjectVM.ProdMachGroupPanelVM.MachGroupVMList.FirstOrDefault(Function(x) x.Id = DBMachGroup.MachGroupId)
         ' leggo dati da Db
         MachGroup.dtStartTime = DBMachGroup.DtStart
         MachGroup.dtEndTime = DBMachGroup.DtEnd
         MachGroup.MyMachGroupM.SetProductionState(DBMachGroup.State)
         MachGroup.SetProductionIndex(DBMachGroup.ProdIndex)
         For Each Part In MachGroup.PartVMList
            Dim DBPart As PartModel = DbControllers.m_PartController.FindByPartId(Map.refSupervisorManagerVM.CurrProd.nProdId, Part.nPartId)
            Part.dtStartTime = DBPart.DtStart
            Part.dtEndTime = DBPart.DtEnd
            Part.nProduction_State = DBPart.State
         Next
         Map.refProjectVM.SupervisorMachGroupPanelVM.AddMachGroupToSupervisor(MachGroup)
      Next
   End Sub

#End Region    ' METHODS

End Class