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

Public MustInherit Class BeamMachGroupVM
   Inherits MyMachGroupVM

   Public ReadOnly Property m_BeamMachGroupM As BeamMachGroupM
      Get
         Return m_MachGroupM
      End Get
   End Property

   Public Overridable Property sStartCut As String
      Get
         Return LenToString(m_BeamMachGroupM.dStartCut, 3)
      End Get
      Set(value As String)
         StringToLen(value, m_BeamMachGroupM.dStartCut)
      End Set
   End Property
   Public Property dStartCut As Double
      Get
         Return m_BeamMachGroupM.dStartCut
      End Get
      Set(value As Double)
         m_BeamMachGroupM.dStartCut = value
         NotifyPropertyChanged(NameOf(sStartCut))
      End Set
   End Property

   Public Property SelBeam As BeamVM
      Get
         Return m_SelPart
      End Get
      Set(value As BeamVM)
         m_SelPart = value
      End Set
   End Property

#Region "CONSTRUCTORS"

   Sub New(BeamMachGroupM As BeamMachGroupM)
      MyBase.New(BeamMachGroupM)
      AddHandler m_BeamMachGroupM.PartAdded, AddressOf OnBeamAdded
      AddHandler m_BeamMachGroupM.PartRemoved, AddressOf OnBeamRemoved
      CreateBeamVMList()
      ' Aggiorno stato da stati feature
      CalcGlobalUpdate()
      NotifyPropertyChanged(NameOf(CALC_FALL_Visibility))
   End Sub

#End Region    ' CONSTRUCTORS

#Region "METHODS"

   Protected MustOverride Sub CreateBeamVMList()

   Public Overrides Sub RefreshPartList()
   End Sub

   Public Overrides Sub RefreshGroupData()
   End Sub

#End Region    ' METHODS

#Region "COMMANDS"

#End Region    ' COMMANDS

#Region "EVENTS"

   Protected MustOverride Sub OnBeamAdded(sender As Object, e As PartAddedEventArgs)
   Private Sub OnBeamRemoved(sender As Object, e As PartAddedEventArgs)
      Dim BeamVM As BeamVM = PartVMList.FirstOrDefault(Function(x) x.PartM Is e.m_NewPart)
      If Not IsNothing(BeamVM) Then PartVMList.Remove(BeamVM)
   End Sub

   Protected Sub OnBeamVMListChanged(sender As Object, e As NotifyCollectionChangedEventArgs)
      If e.Action = NotifyCollectionChangedAction.Add Then
         If Not IsNothing(e.NewItems) AndAlso e.NewItems.Count > 0 Then
            For Each BeamVM As BeamVM In e.NewItems
               AddHandler BeamVM.PropertyChanged, AddressOf OnBeamVMPropertyChanged
            Next
         End If
      End If
      If e.Action = NotifyCollectionChangedAction.Remove Then
         If Not IsNothing(e.OldItems) AndAlso e.OldItems.Count > 0 Then
            For Each BeamVM As BeamVM In e.OldItems
               RemoveHandler BeamVM.PropertyChanged, AddressOf OnBeamVMPropertyChanged
            Next
         End If
      End If
      If e.Action = NotifyCollectionChangedAction.Move Then
         Dim ItemVM As PartVM = e.OldItems(0)
         m_BeamMachGroupM.PartMList.RemoveAt(e.OldStartingIndex)
         m_BeamMachGroupM.PartMList.Insert(e.NewStartingIndex, ItemVM.PartM)
      End If
   End Sub

   Protected Overridable Sub OnBeamVMPropertyChanged(sender As Object, e As PropertyChangedEventArgs)
      'Select Case e.PropertyName
      '   Case NameOf(sender.nSelGRP), NameOf(sender.nSelSIDE)
      'End Select
   End Sub

#End Region

End Class
