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

Public MustInherit Class PartVM
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

#Region "General"

   Protected m_PartM As PartM
   Public ReadOnly Property PartM As PartM
      Get
         Return m_PartM
      End Get
   End Property

   Protected m_ParentMachGroupVM As MyMachGroupVM
   Public ReadOnly Property ParentMachGroupVM As MyMachGroupVM
      Get
         Return m_ParentMachGroupVM
      End Get
   End Property

   ' Id geometrico del pezzo
   Public ReadOnly Property nPartId As Integer
      Get
         Return m_PartM.nPartId
      End Get
   End Property

   ' Proj del pezzo
   Public Property nProjId As Integer
      Get
         Return m_PartM.nProjId
      End Get
      Set(value As Integer)
         m_PartM.nProjId = value
      End Set
   End Property

   ' Info del pezzo
   Public ReadOnly Property nPDN As Integer
      Get
         Return m_PartM.nPDN
      End Get
   End Property

   Public ReadOnly Property sNAM As String
      Get
         Return m_PartM.sNAM
      End Get
   End Property

   Public ReadOnly Property sL As String
      Get
         Return LenToString(m_PartM.dL, 3)
      End Get
   End Property
   Public Property dL As Double
      Get
         Return m_PartM.dL
      End Get
      Set(value As Double)
         m_PartM.dL = value
      End Set
   End Property

   Public ReadOnly Property sW As String
      Get
         Return LenToString(m_PartM.dW, 3)
      End Get
   End Property
   Public Property dW As Double
      Get
         Return m_PartM.dW
      End Get
      Set(value As Double)
         m_PartM.dW = value
      End Set
   End Property

   Public ReadOnly Property sH As String
      Get
         Return LenToString(m_PartM.dH, 3)
      End Get
   End Property
   Public Property dH As Double
      Get
         Return m_PartM.dH
      End Get
      Set(value As Double)
         m_PartM.dH = value
      End Set
   End Property

   Public Overridable Property sPOSX As String
      Get
         Return LenToString(m_PartM.dPOSX, 3)
      End Get
      Set(value As String)
         StringToLenAdv(value, m_PartM.dPOSX, True)
      End Set
   End Property
   Public Property dPOSX As Double
      Get
         Return m_PartM.dPOSX
      End Get
      Set(value As Double)
         m_PartM.dPOSX = value
         NotifyPropertyChanged(NameOf(sPOSX))
      End Set
   End Property

   Public Property nCNT As Integer
      Get
         Return m_PartM.nCNT
      End Get
      Set(value As Integer)
         m_PartM.nCNT = value
      End Set
   End Property

   Public ReadOnly Property sINVERTED As String
      Get
         Return m_PartM.nINVERTED & "°"
      End Get
   End Property

   Public ReadOnly Property nINVERTED As Integer
      Get
         Return m_PartM.nINVERTED
      End Get
   End Property

   Public ReadOnly Property sROTATED As String
      Get
         Return m_PartM.nROTATED & "°"
      End Get
   End Property

   Public ReadOnly Property nROTATED As Integer
      Get
         Return m_PartM.nROTATED
      End Get
   End Property

   Public Property sMATERIAL As String
      Get
         Return m_PartM.sMATERIAL
      End Get
      Set(value As String)
         m_PartM.sMATERIAL = value
      End Set
   End Property

   Public ReadOnly Property sSTOREY As String
      Get
         Return m_PartM.sSTOREY
      End Get
   End Property

   Public ReadOnly Property sGROUP As String
      Get
         Return m_PartM.sGROUP
      End Get
   End Property

   Public ReadOnly Property dVolume As Double
      Get
         Return m_PartM.dVolume
      End Get
   End Property

   ' lista delle feature del pezzo
   Protected m_FeatureVMList As ObservableCollection(Of BTLFeatureVM)
   Public Property FeatureVMList As ObservableCollection(Of BTLFeatureVM)
      Get
         Return m_FeatureVMList
      End Get
      Set(value As ObservableCollection(Of BTLFeatureVM))
         m_FeatureVMList = value
      End Set
   End Property

   Protected m_SelFeatureVM As BTLFeatureVM
   Public Property SelFeatureVM As BTLFeatureVM
      Get
         Return m_SelFeatureVM
      End Get
      Set(value As BTLFeatureVM)
         m_SelFeatureVM = value
         If Not IsNothing(m_SelFeatureVM) Then
            DirectCast(m_SelFeatureVM, BTLFeatureVM).SelGeomFeature()
         Else
            EgtDeselectAll()
         End If
         EgtDraw()
         NotifyPropertyChanged(NameOf(SelFeatureVM))
      End Set
   End Property

#End Region    ' General

#Region "Beam"

   Public Overridable Property sOffset As String
      Get
         Return LenToString(m_PartM.dOffset, 3)
      End Get
      Set(value As String)
         StringToLenAdv(value, m_PartM.dOffset, True)
      End Set
   End Property
   Public ReadOnly Property dOffset As Double
      Get
         Return m_PartM.dOffset
      End Get
   End Property

#End Region    ' Beam

#Region "Wall"

   Public Overridable Property sPOSY As String
      Get
         Return LenToString(m_PartM.dPOSY, 3)
      End Get
      Set(value As String)
         StringToLenAdv(value, m_PartM.dPOSY, True)
      End Set
   End Property

   Public ReadOnly Property dPOSY As Double
      Get
         Return m_PartM.dPOSY
      End Get
   End Property

   Public Property sROT As String
      Get
         Return LenToString(m_PartM.dROT, 3) & "°"
      End Get
      Set(value As String)
         StringToLenAdv(value, m_PartM.dROT, True)
      End Set
   End Property

   Public ReadOnly Property dROT As Double
      Get
         Return m_PartM.dROT
      End Get
   End Property

   Public Overridable Property nFLIP As Integer
      Get
         Return m_PartM.nFLIP
      End Get
      Set(value As Integer)
         m_PartM.nFLIP = value
      End Set
   End Property

#End Region    ' Wall

#Region "Calc"

   Public ReadOnly Property nGlobalState As CalcStates
      Get
         Return m_PartM.nCALC_GlobalState
      End Get
   End Property

   Public Property nCALC_GlobalERR As Integer
      Get
         Return m_PartM.nCALC_GlobalERR
      End Get
      Set(value As Integer)
         m_PartM.nCALC_GlobalERR = value
      End Set
   End Property

   Public Property nCALC_State As CalcStates
      Get
         Return m_PartM.nCALC_State
      End Get
      Set(value As CalcStates)
         m_PartM.nCALC_State = value
      End Set
   End Property

   Public ReadOnly Property Calc_Background As SolidColorBrush
      Get
         Select Case m_PartM.nCALC_GlobalState
            Case CalcStates.ERROR_, CalcStates.COLLISION
               Return Brushes.Red
            Case CalcStates.WARNING
               Return Brushes.Orange
            Case CalcStates.INFO, CalcStates.OK
               Return Brushes.Green
            Case CalcStates.NOTCALCULATED
               Return Brushes.LightGray
            Case Else
               Return Brushes.Red
         End Select
      End Get
   End Property

   Public ReadOnly Property CALC_ROT_Visibility As Visibility
      Get
         Return If(m_PartM.nCALC_ROT <> 0, Visibility.Visible, Visibility.Collapsed)
      End Get
   End Property

   Public ReadOnly Property CALC_FALL_Visibility As Visibility
      Get
         Return If(m_PartM.nCALC_FALL <> 0, Visibility.Visible, Visibility.Collapsed)
      End Get
   End Property

   Public ReadOnly Property CALC_ERR_Letter As String
      Get
         Select Case m_PartM.nCALC_GlobalState
            Case CalcStates.COLLISION
               Return "c"
            Case CalcStates.ERROR_
               Return "e"
            Case CalcStates.WARNING
               Return "w"
            Case CalcStates.INFO
               Return "i"
            Case Else
               Return ""
         End Select
      End Get
   End Property

   Public ReadOnly Property CALC_ERR_Foreground As SolidColorBrush
      Get
         Select Case m_PartM.nCALC_GlobalState
            Case CalcStates.ERROR_, CalcStates.COLLISION
               Return Brushes.Red
            Case CalcStates.WARNING
               Return Brushes.Orange
            Case CalcStates.INFO
               Return Brushes.Green
            Case Else
               Return Brushes.Red
         End Select
      End Get
   End Property

   Public ReadOnly Property nCALC_ERR As Integer
      Get
         Return m_PartM.nCALC_ERR
      End Get
   End Property

   Public ReadOnly Property sCALC_MSG As String
      Get
         Return m_PartM.sCALC_MSG
      End Get
   End Property

   Public Property nCALC_ROT As Integer
      Get
         Return m_PartM.nCALC_ROT
      End Get
      Set(value As Integer)
         m_PartM.nCALC_ROT = value
      End Set
   End Property

   Public Property nCALC_FALL As Integer
      Get
         Return m_PartM.nCALC_FALL
      End Get
      Set(value As Integer)
         m_PartM.nCALC_FALL = value
      End Set
   End Property

   Public Property nCALC_TIME As Integer
      Get
         Return m_PartM.nCALC_TIME
      End Get
      Set(value As Integer)
         m_PartM.nCALC_TIME = value
      End Set
   End Property

#End Region    ' Calc

#Region "Supervisor"

   Public Property nProduction_State As ItemState
      Get
         Return m_PartM.nProductionState
      End Get
      Set(value As ItemState)
         m_PartM.SetProductionState(value)
      End Set
   End Property

   Public ReadOnly Property Production_Background As SolidColorBrush
      Get
         Select Case nProduction_State
            Case ItemState.ND
               Return Brushes.White
            Case ItemState.Assigned, ItemState.Confirmed
               Return Brushes.White
            Case ItemState.WIP
               Return Brushes.Green
            Case ItemState.Produced
               Return Brushes.Yellow
            Case Else         ' ItemState.Scrapped
               Return Brushes.Aqua
         End Select
      End Get
   End Property

   Public Property dtStartTime As DateTime
      Get
         Return m_PartM.dtStartTime
      End Get
      Set(value As DateTime)
         m_PartM.dtStartTime = value
         NotifyPropertyChanged(NameOf(Production_Background))
      End Set
   End Property

   Public Property dtEndTime As DateTime
      Get
         Return m_PartM.dtEndTime
      End Get
      Set(value As DateTime)
         m_PartM.dtEndTime = value
         NotifyPropertyChanged(NameOf(Production_Background))
      End Set
   End Property

   ' variabile che indica se rifare il pezzo
   Private m_bRedo As Boolean = True
   Public Property bRedo As Boolean
      Get
         Return m_bRedo
      End Get
      Set(value As Boolean)
         For Each Feature In FeatureVMList
            Feature.bRedo = value
         Next
         m_bRedo = value
      End Set
   End Property

   Public ReadOnly Property Background As SolidColorBrush
      Get
         Select Case nProduction_State
            Case ItemState.ND
               Return Brushes.White
            Case ItemState.Assigned
               Return Brushes.LightGray
            Case ItemState.Confirmed
               Return Brushes.LightGray
            Case ItemState.WIP
               Return Brushes.Green
            Case ItemState.Produced
               Return Brushes.Yellow
            Case Else         ' ItemState.Scrapped
               Return Brushes.Aqua
         End Select
      End Get
   End Property

#End Region    ' Supervisor

   ' definizione comandi
   Private m_cmdDeletePart As ICommand

#End Region    ' FIELDS & PROPERTIES

#Region "CONSTRUCTOR"

   Sub New(PartM As PartM)
      m_PartM = PartM
      AddHandler m_PartM.BTLFeatureAdded, AddressOf OnBTLFeatureAdded
      CreateBTLFeatureVMList()
   End Sub

   Sub New(PartM As PartM, ParentMachGroupVM As MyMachGroupVM)
      m_PartM = PartM
      m_ParentMachGroupVM = ParentMachGroupVM
      AddHandler m_PartM.BTLFeatureAdded, AddressOf OnBTLFeatureAdded
      CreateBTLFeatureVMList()
      ' Aggiorno stato da stati feature
      CalcGlobalUpdate()
      NotifyPropertyChanged(NameOf(CALC_FALL_Visibility))
   End Sub

#End Region    ' CONSTRUCTOR

#Region "METHODS"

#Region "Feature"

   Protected MustOverride Sub CreateBTLFeatureVMList()

#End Region    ' Feature

#Region "Calc"

   ' funzione che aggiorna lo stato e gli errori dopo calcolo
   Public Sub CalcPartUpdate(ERR As Integer, ROT As Integer, MSG As String)
      EgtSetInfo(nPartId, ITG_PROD_ERR, ERR, True)
      EgtSetInfo(nPartId, ITG_PROD_MSG, MSG, True)
      EgtSetInfo(nPartId, ITG_PROD_ROT, ROT, True)
      m_PartM.nCALC_ERR = ERR
      m_PartM.nCALC_ROT = ROT
      m_PartM.sCALC_MSG = MSG
      Select Case ERR
         Case 0
            nCALC_State = CalcStates.OK
         Case 22
            nCALC_State = CalcStates.COLLISION
         Case 17, 19
            nCALC_State = CalcStates.WARNING
         Case < 0
            nCALC_State = CalcStates.INFO
         Case > 0
            nCALC_State = CalcStates.ERROR_
      End Select
      NotifyPropertyChanged(NameOf(Calc_Background))
   End Sub

   Public Sub ResetCalcPart()
      EgtRemoveInfo(nPartId, ITG_PROD_ERR)
      EgtRemoveInfo(nPartId, ITG_PROD_MSG)
      EgtRemoveInfo(nPartId, ITG_PROD_ROT)
      EgtRemoveInfo(nPartId, ITG_PROD_FALL)
      m_PartM.nCALC_GlobalState = CalcStates.NOTCALCULATED
      m_PartM.nCALC_State = CalcStates.NOTCALCULATED
      m_PartM.nCALC_GlobalERR = 0
      m_PartM.nCALC_ERR = 0
      m_PartM.nCALC_ROT = 0
      m_PartM.nCALC_FALL = 0
      m_PartM.sCALC_MSG = ""
      NotifyPropertyChanged(NameOf(Calc_Background))
      NotifyPropertyChanged(NameOf(CALC_FALL_Visibility))
      NotifyPropertyChanged(NameOf(CALC_ROT_Visibility))
      NotifyPropertyChanged(NameOf(CALC_ERR_Letter))
      NotifyPropertyChanged(NameOf(CALC_ERR_Foreground))
      ParentMachGroupVM.ResetCalcMachGroup()
   End Sub

   Public Sub CalcFallUpdate(FALL As Integer)
      EgtSetInfo(nPartId, ITG_PROD_FALL, FALL, True)
      m_PartM.nCALC_FALL = FALL
      NotifyPropertyChanged(NameOf(nCALC_FALL))
      NotifyPropertyChanged(NameOf(CALC_FALL_Visibility))
   End Sub

   ' funzione che aggiorna lo stato e gli errori dopo calcolo
   Public Sub CalcGlobalUpdate()
      m_PartM.nCALC_GlobalState = nCALC_State
      For Each Feature In FeatureVMList
         If Not Feature.bDO Then
            If Feature.nState = CalcStates.COLLISION AndAlso Feature.nState > m_PartM.nCALC_GlobalState Then
               m_PartM.nCALC_GlobalState = Feature.nState
               m_PartM.nCALC_GlobalERR = Feature.nCALC_ERR
            End If
            Continue For
         End If
         If Feature.nState > m_PartM.nCALC_GlobalState Then
            m_PartM.nCALC_GlobalState = Feature.nState
            m_PartM.nCALC_GlobalERR = Feature.nCALC_ERR
         End If
         If Feature.nCALC_ROT <> 0 Then
            m_PartM.nCALC_ROT = Feature.nCALC_ROT
         End If
      Next
      NotifyPropertyChanged(NameOf(Calc_Background))
      NotifyPropertyChanged(NameOf(CALC_ROT_Visibility))
      NotifyPropertyChanged(NameOf(CALC_ERR_Letter))
      NotifyPropertyChanged(NameOf(CALC_ERR_Foreground))
   End Sub

#End Region    ' Calc

#End Region    ' METHODS

#Region "COMMANDS"

#Region "DeletePart"

   ' Returns a command that manage the MainWindow_Unloaded command
   Public ReadOnly Property DeletePart_Command() As ICommand
      Get
         If m_cmdDeletePart Is Nothing Then
            m_cmdDeletePart = New Command(AddressOf DeletePart)
         End If
         Return m_cmdDeletePart
      End Get
   End Property

   ' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded.
   Public Overridable Sub DeletePart()
   End Sub

#End Region ' DeletePart

#End Region    ' COMMANDS

#Region "EVENTS"

   Public Sub OnBTLFeatureAdded(sender As Object, e As BTLFeatureAddedEventArgs)
      Dim BTLFeatureVM As BTLFeatureVM = New BTLFeatureVM(e.NewBTLFeature)
      FeatureVMList.Add(BTLFeatureVM)
   End Sub

   Protected Sub OnBTLFeatureVMListChanged(sender As Object, e As NotifyCollectionChangedEventArgs)
      If Not IsNothing(e.NewItems) AndAlso e.NewItems.Count > 0 Then
         For Each BTLFeatureVM As BTLFeatureVM In e.NewItems
            AddHandler BTLFeatureVM.PropertyChanged, AddressOf OnBTLFeatureVMPropertyChanged
         Next
      End If
      If Not IsNothing(e.OldItems) AndAlso e.OldItems.Count > 0 Then
         For Each BTLFeatureVM As BTLFeatureVM In e.OldItems
            RemoveHandler BTLFeatureVM.PropertyChanged, AddressOf OnBTLFeatureVMPropertyChanged
            'm_PartM.FeatureMList.Remove(BTLFeatureVM.BTLFeatureM)
         Next
      End If
   End Sub

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

#End Region    ' EVENTS

End Class
