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

Public Class WallVM
   Inherits Core.WallVM

   Public Overrides Property sPOSX As String
      Get
         Return LenToString(m_WallM.dPOSX, 3)
      End Get
      Set(value As String)
         Dim dValue As Double
         If StringToLen(value, dValue) Then
            Dim dOldValue As Double = m_WallM.dPOSX
            m_WallM.dPOSX = dValue
            If EgtMovePartInRawPart(nPartId, New Vector3d(dValue - dOldValue, 0, 0)) Then
               EgtSetInfo(nPartId, MGR_PRT_POSX, dValue)
            Else
               ' rispristino vecchio valore
               m_WallM.dPOSX = dOldValue
            End If
            EgtDraw()
         Else
            NotifyPropertyChanged(NameOf(sPOSX))
         End If
      End Set
   End Property

   Public Overrides Property sPOSY As String
      Get
         Return LenToString(m_WallM.dPOSY, 3)
      End Get
      Set(value As String)
         Dim dValue As Double
         If StringToLen(value, dValue) Then
            Dim dOldValue As Double = m_WallM.dPOSY
            m_WallM.dPOSY = dValue
            If EgtMovePartInRawPart(nPartId, New Vector3d(0, dValue - dOldValue, 0)) Then
               EgtSetInfo(nPartId, MGR_PRT_POSY, dValue)
            Else
               ' rispristino vecchio valore
               m_WallM.dPOSY = dOldValue
            End If
            EgtDraw()
         Else
            NotifyPropertyChanged(NameOf(sPOSY))
         End If
      End Set
   End Property

   Public Overrides Property bFLIP As Boolean
      Get
         Return m_WallM.bFLIP
      End Get
      Set(value As Boolean)
         If value <> m_WallM.bFLIP Then
            ' tolgo il pezzo dal grezzo
            EgtRemovePartFromRawPart(nPartId)
            ' recupero box del pezzo
            Dim b3Part As New BBox3d
            EgtGetBBoxGlob(nPartId, GDB_BB.STANDARD, b3Part)
            If EgtRotate(nPartId, b3Part.Center, Vector3d.X_AX, 180) Then
               m_WallM.bFLIP = value
               EgtSetInfo(nPartId, MGR_PRT_FLIP, value)
            Else
               NotifyPropertyChanged(NameOf(bFLIP))
            End If
            ' reinserisco il pezzo nel grezzo
            EgtAddPartToRawPart(nPartId, New Point3d(dPOSX, m_WallM.dPOSY, 0), DirectCast(ParentMachGroupVM, WallMachGroupVM).m_WallMachGroupM.nRawPartId)
            ' lo riseleziono
            EgtSelectObj(nPartId)
            EgtDraw()
         End If
      End Set
   End Property

   ' Definizione comandi
   Private m_cmdBackRotation As ICommand
   Private m_cmdForwardRotation As ICommand

#Region "CONSTRUCTOR"

   Sub New(WallM As WallM, ParentMachGroupVM As WallMachGroupVM)
      MyBase.New(WallM, 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

   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)
      ' aggiorno contatore pezzi usati in Prod
      If Not IsNothing(BTLPart) Then BTLPart.RefreshPartInProd()
      EgtDraw()
   End Sub

   Private Sub Rotate(IsPositive As Boolean)
      ' tolgo il pezzo dal grezzo
      EgtRemovePartFromRawPart(nPartId)
      ' recupero box del pezzo
      Dim b3Part As New BBox3d
      EgtGetBBoxGlob(nPartId, GDB_BB.STANDARD, b3Part)
      ' se invertito lo reinverto
      Dim bFliped As Boolean = bFLIP
      If bFliped Then EgtRotate(nPartId, b3Part.Center, Vector3d.X_AX, 180)
      Dim dAng As Double = If(IsPositive, 90, -90)
      ' eseguo rotazione
      Dim bOk As Boolean = EgtRotate(nPartId, b3Part.Center, -Vector3d.Z_AX, dAng)
      If bOk Then
         Dim TempRot = m_WallM.dROT
         TempRot += dAng
         If TempRot >= 360 Then
            TempRot -= 360
         ElseIf TempRot < 0 Then
            TempRot += 360
         End If
         m_WallM.dROT = TempRot
         EgtSetInfo(nPartId, MGR_PRT_ROT, m_WallM.dROT)
      End If
      ' ripristino eventuale inversione
      If bFliped Then EgtRotate(nPartId, b3Part.Center, Vector3d.X_AX, 180)
      ' reinserisco il pezzo nel grezzo
      EgtAddPartToRawPart(nPartId, New Point3d(dPOSX, m_WallM.dPOSY, 0), DirectCast(ParentMachGroupVM, WallMachGroupVM).m_WallMachGroupM.nRawPartId)
      ' lo riseleziono
      EgtSelectObj(nPartId)
      NotifyPropertyChanged(NameOf(sROT))
   End Sub

#End Region    ' METHODS

#Region "COMMANDS"

#Region "BackRotation"

   Public ReadOnly Property BackRotation_Command As ICommand
      Get
         If m_cmdBackRotation Is Nothing Then
            m_cmdBackRotation = New Command(AddressOf BackRotation)
         End If
         Return m_cmdBackRotation
      End Get
   End Property

   Public Sub BackRotation()
      Rotate(False)
      EgtDraw()
   End Sub

#End Region ' BackRotation

#Region "ForwardRotation"

   Public ReadOnly Property ForwardRotation_Command As ICommand
      Get
         If m_cmdForwardRotation Is Nothing Then
            m_cmdForwardRotation = New Command(AddressOf ForwardRotation)
         End If
         Return m_cmdForwardRotation
      End Get
   End Property

   Public Sub ForwardRotation()
      Rotate(True)
      EgtDraw()
   End Sub

#End Region ' ForwardRotation

#End Region    ' COMMANDS

End Class
