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 MachGroupVM
      Get
         Return m_SelectedMachGroup
      End Get
      Set(value As MachGroupVM)
         m_SelectedMachGroup = value
         If Not IsNothing(value) Then
            OnPreSetCurrMachGroup()
            EgtSetCurrMachGroup(value.Id)
            OnPostSetCurrMachGroup()
         End If
         ' aggiorno stato bottoni in LeftPanel
         Map.refLeftPanelVM.UpdateButtonIsEnabledState()
         NotifyPropertyChanged(NameOf(SelectedMachGroup))
      End Set
   End Property

#End Region    ' FIELDS & PROPERTIES

#Region "CONSTRUCTOR"

   Sub New(MachGroupPanelM As MachGroupPanelM)
      MyBase.New(MachGroupPanelM)
      ' Recupero la macchina di default
      Dim sDefaultMachine As String = String.Empty
      GetMainPrivateProfileString(S_MACH, K_CURRMACH, "", sDefaultMachine)
      ' 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

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

   Public Function FirstNotToBeProducedIndex() As Integer
      Return MachGroupVMList.IndexOf(MachGroupVMList.FirstOrDefault(Function(x) DirectCast(x, MyMachGroupVM).bToBeProduced = False))
   End Function

   Friend Sub ResetAllMachGroups()
      For Each Machgroup As MyMachGroupVM In MachGroupVMList
         ' se iniziato ma non finito
         If Machgroup.dtStartTime <> DateTime.MinValue AndAlso Machgroup.dtEndTime = DateTime.MinValue Then
            ' segno come resettato
            Machgroup.SetResetWhileCutting(True)
         End If
         Machgroup.ResetProduce()
         Machgroup.NotifyPropertyChanged(NameOf(Machgroup.Produce_IsEnabled))
      Next
   End Sub

   Public Overrides Sub AddMachGroup()
      Dim SelMyMachine As MyMachine = Map.refMachinePanelVM.SelectedMachine
      Dim MyMachGroupM As MyMachGroupM = m_MyMachGroupPanelM.NewMyMachGroup(SelMyMachine.Name, SelMyMachine.nType)
      If Not IsNothing(MyMachGroupM) Then Return
      ' 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
         ManageBTLParts.HideAll()
      End If
   End Sub

#End Region    ' METHODS

End Class
