Imports System.Collections.ObjectModel
Imports System.IO
Imports System.Windows.Input
Imports System.Windows.Media
Imports EgtUILib
Imports EgtWPFLib5

Public Class MyMachGroupM
   Inherits MachGroupM

#Region "FIELDS & PROPERTIES"

#Region "General"

   Public Event PartAdded As EventHandler(Of PartAddedEventArgs)
   Public Event PartRemoved As EventHandler(Of PartAddedEventArgs)

   Protected m_nType As BWType
   Public ReadOnly Property nType As BWType
      Get
         Return m_nType
      End Get
   End Property

   Protected m_dL As Double
   Public Property dL As Double
      Get
         Return m_dL
      End Get
      Set(value As Double)
         m_dL = value
      End Set
   End Property

   Protected m_dW As Double
   Public Property dW As Double
      Get
         Return m_dW
      End Get
      Set(value As Double)
         m_dW = value
      End Set
   End Property

   Protected m_dH As Double
   Public Property dH As Double
      Get
         Return m_dH
      End Get
      Set(value As Double)
         m_dH = value
      End Set
   End Property

   Protected m_dtStartTime As DateTime
   Public Property dtStartTime As DateTime
      Get
         Return m_dtStartTime
      End Get
      Set(value As DateTime)
         m_dtStartTime = value
      End Set
   End Property

   Protected m_dtEndTime As DateTime
   Public Property dtEndTime As DateTime
      Get
         Return m_dtEndTime
      End Get
      Set(value As DateTime)
         m_dtEndTime = value
      End Set
   End Property

   Protected m_dMatForPart As Double = 0
   Public ReadOnly Property dMatForPart As Double
      Get
         Return m_dMatForPart
      End Get
   End Property

   Protected m_nProdIndex As Integer = 0
   Public Property nProdIndex As Integer
      Get
         Return m_nProdIndex
      End Get
      Set
         m_nProdIndex = Value
      End Set
   End Property

   Public Sub SetMatForPart(dMatForPart As Double)
      m_dMatForPart = dMatForPart
   End Sub

   Protected m_dTotMat As Double = 0
   Public ReadOnly Property dTotMat As Double
      Get
         Return m_dTotMat
      End Get
   End Property
   Public Sub SetTotMat(TotMat As Double)
      m_dTotMat = TotMat
   End Sub

   Protected m_sMATERIAL As String
   Public Property sMATERIAL As String
      Get
         Return m_sMATERIAL
      End Get
      Set(value As String)
         m_sMATERIAL = value
      End Set
   End Property

   ' lista dei pezzi che sono nel grezzo
   Protected m_PartMList As New List(Of PartM)
   Public Property PartMList As List(Of PartM)
      Get
         Return m_PartMList
      End Get
      Set(value As List(Of PartM))
         m_PartMList = value
      End Set
   End Property

#End Region    ' General

#Region "Beam"

   Protected m_dStartCut As Double
   Public Property dStartCut As Double
      Get
         Return m_dStartCut
      End Get
      Set(value As Double)
         m_dStartCut = value
      End Set
   End Property

#End Region    ' Beam

#Region "Wall"

   Protected m_nRawPartId As Integer = GDB_ID.NULL
   Public ReadOnly Property nRawPartId As Integer
      Get
         Return m_nRawPartId
      End Get
   End Property

   Protected m_dPosZ As Double = 0
   Public Property dPosZ As Double
      Get
         Return m_dPosZ
      End Get
      Set(value As Double)
         m_dPosZ = value
      End Set
   End Property

#End Region    ' Wall

#Region "Calc"

   Private m_nCALC_ERR As Integer
   Public Property nCALC_ERR As Integer
      Get
         Return m_nCALC_ERR
      End Get
      Set(value As Integer)
         m_nCALC_ERR = value
      End Set
   End Property

   Private m_nCALC_FALL As Integer
   Public Property nCALC_FALL As Integer
      Get
         Return m_nCALC_FALL
      End Get
      Set(value As Integer)
         m_nCALC_FALL = value
      End Set
   End Property

   Private m_sCALC_MSG As String
   Public Property sCALC_MSG As String
      Get
         Return MessageFromLuaMsg(m_sCALC_MSG)
      End Get
      Set(value As String)
         m_sCALC_MSG = value
      End Set
   End Property

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

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

   Public m_nCALC_GlobalState As CalcStates = CalcStates.NOTCALCULATED
   Public Property nCALC_GlobalState As CalcStates
      Get
         Return m_nCALC_GlobalState
      End Get
      Set(value As CalcStates)
         m_nCALC_GlobalState = value
      End Set
   End Property

   Private m_bCALC_PartsGlobalState As CalcStates
   Public Property bCALC_PartsGlobalState As CalcStates
      Get
         Return m_bCALC_PartsGlobalState
      End Get
      Set(value As CalcStates)
         m_bCALC_PartsGlobalState = value
      End Set
   End Property

   Private m_nCALC_GlobalERR As Integer
   Public Property nCALC_GlobalERR As Integer
      Get
         Return m_nCALC_GlobalERR
      End Get
      Set(value As Integer)
         m_nCALC_GlobalERR = value
      End Set
   End Property

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

#End Region    ' Calc

#Region "Supervisor"

   Protected m_sSupervisorId As String
   Public Property sSupervisorId As String
      Get
         Return m_sSupervisorId
      End Get
      Set(value As String)
         m_sSupervisorId = value
      End Set
   End Property

   Protected m_nPriorityIndex As Integer
   Public Property nPriorityIndex As Integer
      Get
         Return m_nPriorityIndex
      End Get
      Set(value As Integer)
         m_nPriorityIndex = value
      End Set
   End Property

   Protected m_nProductionState As ItemState = ItemState.ND
   Public ReadOnly Property nProductionState As ItemState
      Get
         Return m_nProductionState
      End Get
   End Property
   Public Sub SetProductionState(nState As ItemState)
      m_nProductionState = nState
   End Sub

#End Region    ' Supervisor

#End Region    ' FIELDS & PROPERTIES

#Region "CONSTRUCTOR"

   Protected Sub New()
   End Sub

   Public Overloads Shared Function CreateNewMachGroup() As MyMachGroupM
      Return New MyMachGroupM
   End Function

   Public Overloads Shared Function CreateMyMachGroup(Type As BWType, nId As Integer, sName As String, sMachine As String) As MachGroupM
      Dim NewMachGroupM As New MyMachGroupM
      NewMachGroupM.m_Id = nId
      NewMachGroupM.m_Name = sName
      NewMachGroupM.m_Machine = sMachine
      NewMachGroupM.m_nType = Type
      If NewMachGroupM.m_nType = MachineType.BEAM Then
         EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_BARLEN, NewMachGroupM.m_dL)
         EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_BARHEIGHT, NewMachGroupM.m_dH)
         EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_BARWIDTH, NewMachGroupM.m_dW)
         NewMachGroupM.m_PartMList = LoadParts(NewMachGroupM)
         For Each Part In NewMachGroupM.m_PartMList
            Part.UpdateOffset()
            NewMachGroupM.m_dTotMat += Part.dL + Part.dOffset
            NewMachGroupM.m_dMatForPart += Part.dL
         Next
      Else
         NewMachGroupM.m_nRawPartId = EgtGetFirstRawPart()
         EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_PANELLEN, NewMachGroupM.m_dL)
         EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_PANELWIDTH, NewMachGroupM.m_dW)
         EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_PANELHEIGHT, NewMachGroupM.m_dH)
         EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_PANELPOSZ, NewMachGroupM.m_dPosZ)
         NewMachGroupM.m_dTotMat = NewMachGroupM.m_dL * NewMachGroupM.m_dW * NewMachGroupM.m_dH
         NewMachGroupM.m_PartMList = LoadParts(NewMachGroupM)
         For Each Part In NewMachGroupM.m_PartMList
            NewMachGroupM.m_dMatForPart += Part.dVolume
         Next
      End If
      ReadMachGroupData(NewMachGroupM)
      Return NewMachGroupM
   End Function

   Public Overloads Shared Function CreateMyMachGroup(Type As BWType, sName As String, sMachine As String) As MachGroupM
      Dim NewMachGroupM As New MyMachGroupM
      NewMachGroupM.m_Id = EgtAddMachGroup(sName, sMachine)
      If NewMachGroupM.m_Id = GDB_ID.NULL Then Return Nothing
      NewMachGroupM.m_Name = sName
      NewMachGroupM.m_Machine = sMachine
      NewMachGroupM.m_nType = Type
      If NewMachGroupM.m_nType = MachineType.BEAM Then
         EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_BARLEN, NewMachGroupM.m_dL)
         EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_BARHEIGHT, NewMachGroupM.m_dH)
         EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_BARWIDTH, NewMachGroupM.m_dW)
         NewMachGroupM.m_dTotMat = NewMachGroupM.m_dL
         NewMachGroupM.m_PartMList = LoadParts(NewMachGroupM)
         For Each Part In NewMachGroupM.m_PartMList
            Part.UpdateOffset()
            NewMachGroupM.m_dMatForPart += Part.dL + Part.dOffset
         Next
      Else
         NewMachGroupM.m_nRawPartId = EgtGetFirstRawPart()
         EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_PANELLEN, NewMachGroupM.m_dL)
         EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_PANELWIDTH, NewMachGroupM.m_dW)
         EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_PANELHEIGHT, NewMachGroupM.m_dH)
         EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_PANELPOSZ, NewMachGroupM.m_dPosZ)
         NewMachGroupM.m_dTotMat = NewMachGroupM.m_dL * NewMachGroupM.m_dW
         NewMachGroupM.m_PartMList = LoadParts(NewMachGroupM)
         For Each Part In NewMachGroupM.m_PartMList
            NewMachGroupM.m_dMatForPart += Part.dVolume
         Next
      End If
      ReadMachGroupData(NewMachGroupM)
      Return NewMachGroupM
   End Function

#End Region    ' CONSTRUCTOR

#Region "METHODS"

#Region "Machgroup"

   Public Shared Sub ReadMachGroupData(NewMachgroup As MyMachGroupM)
      ' leggo materiale
      EgtGetInfo(NewMachgroup.Id, MGR_RPT_MATERIAL, NewMachgroup.sMATERIAL)
      ' leggo calc error
      Dim nErr As Integer = 0
      Dim nRot As Integer = 0
      Dim nFall As Integer = 0
      Dim sMsg As String = ""
      Dim bCalc As Boolean = False
      bCalc = EgtGetInfo(NewMachgroup.Id, ITG_PROD_ERR, nErr)
      EgtGetInfo(NewMachgroup.Id, ITG_PROD_ROT, nRot)
      EgtGetInfo(NewMachgroup.Id, ITG_PROD_FALL, nFall)
      EgtGetInfo(NewMachgroup.Id, ITG_PROD_MSG, sMsg)
      If Not bCalc Then
         NewMachgroup.m_nCALC_State = CalcStates.NOTCALCULATED
         NewMachgroup.m_nCALC_ERR = 0
         NewMachgroup.m_nCALC_ROT = 0
         NewMachgroup.m_nCALC_FALL = 0
         NewMachgroup.m_sCALC_MSG = ""
      Else
         NewMachgroup.m_nCALC_ERR = nErr
         NewMachgroup.m_nCALC_ROT = nRot
         NewMachgroup.m_nCALC_FALL = nFall
         NewMachgroup.m_sCALC_MSG = sMsg
         Select Case nErr
            Case 0
               NewMachgroup.nCALC_State = CalcStates.OK
            Case 22
               NewMachgroup.nCALC_State = CalcStates.COLLISION
            Case 17, 19
               NewMachgroup.nCALC_State = CalcStates.WARNING
            Case < 0
               NewMachgroup.nCALC_State = CalcStates.INFO
            Case > 0
               NewMachgroup.nCALC_State = CalcStates.ERROR_
         End Select
      End If
      EgtGetInfo(NewMachgroup.Id, ITG_PROD_TIME, NewMachgroup.m_nCALC_TIME)
   End Sub

   Public Overridable Sub RefreshPartList()
      If m_nType = MachineType.BEAM Then
         ' leggo StartCut da posizione primo pezzo
         Dim sInfo As String = ""
         Dim sSplitInfo() As String
         Dim bFound As Boolean = False
         EgtGetInfo(Id, MGR_RPT_PART & 1, sInfo)
         If Not String.IsNullOrWhiteSpace(sInfo) Then
            sSplitInfo = sInfo.Split(","c)
            StringToDoubleAdv(sSplitInfo(1), m_dStartCut)
         Else
            m_dStartCut = 0
         End If
         ' aggiorno lista pezzi
         RemoveAllParts()
         Dim nRawPartId As Integer = EgtGetFirstRawPart()
         While nRawPartId <> GDB_ID.NULL
            Dim nPartId As Integer = EgtGetFirstPartInRawPart(nRawPartId)
            If nPartId <> GDB_ID.NULL Then
               Dim NewBeam As PartM = PartM.CreatePart(BWType.BEAM, Me, nPartId, nRawPartId)
               AddPart(NewBeam)
               NewBeam.UpdateOffset()
            End If
            nRawPartId = EgtGetNextRawPart(nRawPartId)
         End While
      Else
         ' aggiorno lista pezzi
         RemoveAllParts()
         Dim nRawPartId As Integer = EgtGetFirstRawPart()
         If nRawPartId <> GDB_ID.NULL Then
            Me.m_nRawPartId = nRawPartId
         End If
         Dim nWallId As Integer = EgtGetFirstPartInRawPart(m_nRawPartId)
         While nWallId <> GDB_ID.NULL
            If nWallId <> GDB_ID.NULL Then
               Dim NewWall As PartM = PartM.CreatePart(BWType.WALL, Me, nWallId, m_nRawPartId)
               AddPart(NewWall)
            End If
            nWallId = EgtGetNextPartInRawPart(nWallId)
         End While
      End If
   End Sub

   Public Overridable Sub RefreshGroupData()
      If m_nType = MachineType.BEAM Then
         EgtGetInfo(Id, MGR_RPT_BARLEN, dL)
         EgtGetInfo(Id, MGR_RPT_BARHEIGHT, dH)
         EgtGetInfo(Id, MGR_RPT_BARWIDTH, dW)
         EgtGetInfo(Id, MGR_RPT_MATERIAL, sMATERIAL)
      Else
         EgtGetInfo(Id, MGR_RPT_PANELLEN, dL)
         EgtGetInfo(Id, MGR_RPT_PANELWIDTH, dW)
         EgtGetInfo(Id, MGR_RPT_PANELHEIGHT, dH)
         EgtGetInfo(Id, MGR_RPT_MATERIAL, sMATERIAL)
      End If
   End Sub

   Public Overridable Sub DeleteMachGroup()
      ' elimino tutte le copie
      Dim nRawPartId As Integer = EgtGetFirstRawPart()
      Dim nBeamId As Integer = EgtGetFirstPartInRawPart(nRawPartId)
      While nRawPartId <> GDB_ID.NULL
         EgtRemovePartFromRawPart(nBeamId)
         EgtErase(nBeamId)
         nRawPartId = EgtGetNextRawPart(nRawPartId)
         nBeamId = EgtGetFirstPartInRawPart(nRawPartId)
      End While
      ' elimino MachGroup
      EgtRemoveMachGroup(Me.Id)
   End Sub

#End Region    ' Machgroup

#Region "Parts"

   Private Shared Function LoadParts(NewMachGroupM As MyMachGroupM) As List(Of PartM)
      Dim TempList As New List(Of PartM)
      If NewMachGroupM.m_nType = MachineType.BEAM Then
         Dim nRawPartId As Integer = EgtGetFirstRawPart()
         While nRawPartId <> GDB_ID.NULL
            Dim nPartId As Integer = EgtGetFirstPartInRawPart(nRawPartId)
            If nPartId <> GDB_ID.NULL Then
               Dim NewBeam As PartM = PartM.CreatePart(BWType.BEAM, NewMachGroupM, nPartId, nRawPartId)
               TempList.Add(NewBeam)
            End If
            nRawPartId = EgtGetNextRawPart(nRawPartId)
         End While
      Else
         Dim nWallId As Integer = EgtGetFirstPartInRawPart(NewMachGroupM.m_nRawPartId)
         While nWallId <> GDB_ID.NULL
            If nWallId <> GDB_ID.NULL Then
               Dim NewWall As PartM = PartM.CreatePart(BWType.WALL, NewMachGroupM, nWallId, NewMachGroupM.m_nRawPartId)
               TempList.Add(NewWall)
            End If
            nWallId = EgtGetNextPartInRawPart(nWallId)
         End While
      End If
      Return TempList
   End Function

   Public Sub AddPart(PartM As PartM)
      If IsNothing(PartM) Then Return
      If Not m_PartMList.Contains(PartM) Then
         m_PartMList.Add(PartM)
         RaiseEvent PartAdded(Me, New PartAddedEventArgs(PartM))
      End If
   End Sub

   Public Sub RemovePart(PartM As PartM)
      If IsNothing(PartM) Then Return
      If m_PartMList.Contains(PartM) AndAlso m_PartMList.Remove(PartM) Then
         RaiseEvent PartRemoved(Me, New PartAddedEventArgs(PartM))
      End If
   End Sub

   Public Sub RemoveAllParts()
      If IsNothing(m_PartMList) Then Return
      For PartIndex = m_PartMList.Count - 1 To 0 Step -1
         Dim PartM As PartM = m_PartMList(PartIndex)
         If m_PartMList.Remove(PartM) Then
            RaiseEvent PartRemoved(Me, New PartAddedEventArgs(PartM))
         End If
      Next
   End Sub

   Public Function GetParts() As List(Of PartM)
      Return New List(Of PartM)(m_PartMList)
   End Function

#End Region    ' Parts

#End Region    ' METHODS

End Class
