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

Public Class BeamVM
   Inherits Core.BeamVM

   Public Overrides Property sOffset As String
      Get
         Return LenToString(m_BeamM.dOffset, 3)
      End Get
      Set(value As String)
         Dim dValue As Double
         If StringToLen(value, dValue) Then
            Dim dOldValue As Double = m_BeamM.dOffset
            m_BeamM.dOffset = dValue
            ' se prima trave della barra sostituisco valore anche a startoffset
            Dim BeamMachGroup As BeamMachGroupVM = DirectCast(ParentMachGroupVM, BeamMachGroupVM)
            If ParentMachGroupVM.PartVMList(0) Is Me Then
               BeamMachGroup.dStartCut = dValue
            End If
            If Not BeamMachGroup.ReDrawBeamMachgroup() Then
               ' rispristino vecchio valore
               m_BeamM.dOffset = dOldValue
               BeamMachGroup.dStartCut = dOldValue
               BeamMachGroup.ReDrawBeamMachgroup()
            End If
            ' resetto validazione del pezzo
            ResetCalcPart()
         Else
            NotifyPropertyChanged(NameOf(sOffset))
         End If
      End Set
   End Property

   Friend Sub ResetOffset()
      m_BeamM.dOffset = RawPartConfiguration.dBeamDist
      NotifyPropertyChanged(NameOf(sOffset))
   End Sub

#Region "CONSTRUCTOR"

   Sub New(BeamM As BeamM, ParentMachGroupVM As BeamMachGroupVM)
      MyBase.New(BeamM, ParentMachGroupVM)
   End Sub

#End Region    ' CONSTRUCTOR

#Region "METHODS"

   Protected Overrides Sub CreateBTLFeatureVMList()
      Dim all As List(Of BTLFeatureVM) = (From BTLFeatureM In m_PartM.GetBTLFeatures()
                                          Select New BTLFeatureVM(BTLFeatureM)).ToList()

      For Each BTLFeatureVM As BTLFeatureVM In all
         AddHandler BTLFeatureVM.PropertyChanged, AddressOf OnBTLFeatureVMPropertyChanged
      Next

      m_FeatureVMList = New ObservableCollection(Of Core.BTLFeatureVM)(all)
      AddHandler m_FeatureVMList.CollectionChanged, AddressOf OnBTLFeatureVMListChanged

   End Sub

#End Region    ' METHODS

   ' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded.
   Public Overrides Sub DeletePart()
      ' elimino pezzo copia
      EgtRemovePartFromRawPart(Me.nPartId)
      ' Recupero Id BTLPart originale
      Dim BTLPart As BTLPartVM = BTLPartVM.RetrieveBTLPartFromPart(nPartId)
      EgtErase(Me.nPartId)
      ' rimuovo dalla lista pezzi
      Dim Index As Integer = ParentMachGroupVM.PartVMList.IndexOf(ParentMachGroupVM.PartVMList.FirstOrDefault(Function(x) x.nPartId = nPartId))
      If Index = 0 Then
         If ParentMachGroupVM.PartVMList.Count > 1 Then
            ParentMachGroupVM.SelPart = ParentMachGroupVM.PartVMList(0)
         Else
            ParentMachGroupVM.SelPart = Nothing
         End If
      ElseIf Index = ParentMachGroupvm.PartvMList.Count - 1 Then
         If ParentMachGroupVM.PartVMList.Count > 1 Then
            ParentMachGroupVM.SelPart = ParentMachGroupVM.PartVMList(ParentMachGroupVM.PartVMList.Count - 2)
         Else
            ParentMachGroupVM.SelPart = Nothing
         End If
      Else
         ParentMachGroupVM.SelPart = ParentMachGroupVM.PartVMList(Index - 1)
      End If
      ParentMachGroupVM.PartVMList.Remove(Me)
      Dim BeamParentMachGroup As BeamMachGroupVM = DirectCast(ParentMachGroupVM, BeamMachGroupVM)
      If Not IsNothing(BeamParentMachGroup) Then BeamParentMachGroup.ReDrawBeamMachgroup()
      ' aggiorno contatore pezzi usati in Prod
      If Not IsNothing(BTLPart) Then BTLPart.RefreshPartInProd()
      EgtDraw()
   End Sub

End Class
