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

Public MustInherit Class MyMachGroupPanelVM
   Inherits NewMachGroupPanelVM

#Region "FIELDS & PROPERTIES"

   Public ReadOnly Property m_MyMachGroupPanelM As MyMachGroupPanelM
      Get
         Return m_MachGroupPanelM
      End Get
   End Property

   Public ReadOnly Property sTotMachGroups As String
      Get
         Return m_MachGroupVMList.Count()
      End Get
   End Property

   Public ReadOnly Property sTotVolume As String
      Get
         Dim dMMTotVolume As Double = m_MachGroupVMList.Sum(CType(Function(x As MyMachGroupVM) x.dUnitVolume, Func(Of MachGroupVM, Double)))
         Return If(EgtUiUnitsAreMM(), DoubleToString(dMMTotVolume / 1000000000, 3), DoubleToString(dMMTotVolume * (12 / ONEINCH), 3))
      End Get
   End Property

   Public ReadOnly Property sTotEstimatedTime As String
      Get
         Return TimeSpan.FromSeconds(m_MachGroupVMList.Sum(CType(Function(x As MyMachGroupVM) x.nCALC_TIME, Func(Of MachGroupVM, Double)))).ToString()
      End Get
   End Property

   Public ReadOnly Property dTotTime As String
      Get
         Return m_MachGroupVMList.Sum(CType(Function(x As MyMachGroupVM) If(x.nProduction_State >= ItemState.Produced AndAlso Not IsNothing(x.dtEndTime) AndAlso x.dtEndTime <> DateTime.MinValue, (x.dtEndTime - x.dtStartTime).TotalSeconds(), x.nCALC_TIME), Func(Of MachGroupVM, Double)))
      End Get
   End Property

   Public ReadOnly Property sTotTime As String
      Get
         Return TimeSpan.FromSeconds(dTotTime).ToString()
      End Get
   End Property

   Public ReadOnly Property dDoneTime As String
      Get
         Return m_MachGroupVMList.Sum(CType(Function(x As MyMachGroupVM) If(x.nProduction_State >= ItemState.Produced AndAlso Not IsNothing(x.dtEndTime) AndAlso x.dtEndTime <> DateTime.MinValue, (x.dtEndTime - x.dtStartTime).TotalSeconds(), 0), Func(Of MachGroupVM, Double)))
      End Get
   End Property

   Public ReadOnly Property sDoneTime As String
      Get
         Return TimeSpan.FromSeconds(dDoneTime).ToString()
      End Get
   End Property

   Public ReadOnly Property dRemainingTime As String
      Get
         Return m_MachGroupVMList.Sum(CType(Function(x As MyMachGroupVM) If(x.nProduction_State < ItemState.Produced, x.nCALC_TIME, 0), Func(Of MachGroupVM, Double)))
      End Get
   End Property

   Public ReadOnly Property sRemainingTime As String
      Get
         Return TimeSpan.FromSeconds(dRemainingTime).ToString()
      End Get
   End Property

#End Region    ' FIELDS & PROPERTIES

#Region "CONSTRUCTOR"

   Sub New(MachGroupPanelM As MachGroupPanelM)
      MyBase.New(MachGroupPanelM)
      ' Creo riferimento a questa classe in Map
      CoreMap.SetRefMachGroupPanelVM(Me)
   End Sub

#End Region    ' CONSTRUCTOR

#Region "METHODS"

   Protected MustOverride Overrides Sub CreateMachGroupVMList()

   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(CoreMap.refMachinePanelVM.MachineList.ToList())
      ' elimino copie da cancellare
      MyMachGroupPanelM.DeleteDuplo()
      If bOk Then SelectedMachGroup = MachGroupVMList(0)
      EgtZoom(ZM.ALL)
      Return True
   End Function

   Private Sub RedrawModifiedMachGroup(RedrawList As List(Of Integer))
      For Each nMachGroupId In RedrawList
         EgtSetCurrMachGroup(nMachGroupId)
         ' elimino vecchio grezzo ed eseguo script creazione nuovo
         Dim nRawId As Integer = EgtGetFirstRawPart()
         While nRawId <> GDB_ID.NULL
            EgtRemovePartFromRawPart(EgtGetFirstPartInRawPart(nRawId))
            EgtRemoveRawPart(nRawId)
            nRawId = EgtGetFirstRawPart()
         End While
         'If Not ExecBeam(Map.refMainWindowVM.MainWindowM.sTempDir, Map.refMachinePanelVM.SelectedMachine.Name, CalcIntegration.CmdTypes.RAWPART, False) Then Return False

      Next
   End Sub

   '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

   Public Overrides Function OnPreSetCurrMachGroup() As Boolean
      EgtSetCurrentContext(CoreMap.refSceneHostVM.MainScene.GetCtx())
      Return True
   End Function

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

   ' 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

   ' funzione che seleziona ultimo gruppo
   Public Sub SelLastMachGroup()
      If Not IsNothing(Me) AndAlso Not IsNothing(MachGroupVMList) AndAlso MachGroupVMList.Count > 0 Then
         SelectedMachGroup = MachGroupVMList(MachGroupVMList.Count - 1)
      End If
   End Sub

   Public Function GetLastMachGroup() As MachGroupVM
      If Not IsNothing(Me) AndAlso Not IsNothing(MachGroupVMList) AndAlso MachGroupVMList.Count > 0 Then
         Return MachGroupVMList(MachGroupVMList.Count - 1)
      End If
      Return Nothing
   End Function

   ' funzione che aggiorna totale tempi a fine calcolo
   Public Sub CalcGlobalTime()
      NotifyPropertyChanged(NameOf(sTotTime))
      NotifyPropertyChanged(NameOf(sRemainingTime))
   End Sub

   ' funzione che aggiorna numero gruppi di lavorazione e volume totale
   Public Sub CalcGlobalUpdate()
      NotifyPropertyChanged(NameOf(sTotMachGroups))
      NotifyPropertyChanged(NameOf(sTotVolume))
      NotifyPropertyChanged(NameOf(sTotTime))
      NotifyPropertyChanged(NameOf(sRemainingTime))
   End Sub

   Public Shared Sub DuploRemoveProjCalc(nPartDuploId As Integer)
      EgtSetInfo(nPartDuploId, ITG_PROJ_ERR, "")
      EgtSetInfo(nPartDuploId, ITG_PROJ_MSG, "")
      EgtSetInfo(nPartDuploId, ITG_PROJ_ROT, "")
      EgtSetInfo(nPartDuploId, ITG_PROJ_FALL, "")
      EgtSetInfo(nPartDuploId, ITG_PROJ_TIME, "")
   End Sub

#End Region    ' METHODS

#Region "EVENTS"

   Protected Overrides Sub OnMachGroupVMPropertyChanged(sender As Object, e As PropertyChangedEventArgs)
      Select Case e.PropertyName
         Case NameOf(sender.nProduction_State)
            NotifyPropertyChanged(NameOf(sTotTime))
            NotifyPropertyChanged(NameOf(sRemainingTime))
      End Select
   End Sub

#End Region    ' EVENTS

End Class
