Imports EgtUILib
Imports EgtWPFLib5
Imports EgtBEAMWALL.Core

Public Class LogEvent
   Inherits VMBase

   Public Const cSeparator As Char = ";"c

   Public Enum MachLogTypes
      PART_STATUS = 1
      MACHGROUP_STATUS = 2
      MACHINE_MODE = 3
      MACHINE_STATUS = 4
      MACHINE_COMMAND = 5
      READ_VAR = 6
      WRITE_VAR = 7
      ALARM = 8
      OPERATOR_MSG = 9
      PROGRAM_SEND = 10
   End Enum

   Public Enum MachineCommands As Integer
      CONNECTION = 1
      DISCONNECTION = 2
      PROGRAM_ACTIVATION = 3
   End Enum

   Private m_EventDateTime As DateTime
   Public ReadOnly Property EventDateTime As DateTime
      Get
         Return m_EventDateTime
      End Get
   End Property
   Private m_EventType As MachLogTypes
   Public ReadOnly Property EventType As MachLogTypes
      Get
         Return m_EventType
      End Get
   End Property
   Private m_SupervisorId As String
   Public ReadOnly Property SupervisorId As String
      Get
         Return m_SupervisorId
      End Get
   End Property
   Private m_Value As String
   Public ReadOnly Property Value As String
      Get
         Return m_Value
      End Get
   End Property
   Private m_ProdId As Integer
   Public ReadOnly Property ProdId As Integer
      Get
         Return m_ProdId
      End Get
   End Property
   Private m_MachGroupId As Integer
   Public ReadOnly Property MachGroupId As Integer
      Get
         Return m_MachGroupId
      End Get
   End Property
   Private m_PartId As Integer
   Public ReadOnly Property PartId As Integer
      Get
         Return m_PartId
      End Get
   End Property
   Private m_Status As Integer
   Public ReadOnly Property nStatus As Integer
      Get
         Return m_Status
      End Get
   End Property
   Public ReadOnly Property Status As String
      Get
         Select Case m_Status
            Case 1
               Return EgtMsg(62527)
            Case 2
               Return EgtMsg(62528)
            Case Else
               Return ""
         End Select
      End Get
   End Property

   Public ReadOnly Property sEventType As String
      Get
         Select Case m_EventType
            Case MachLogTypes.PART_STATUS
               Return String.Format("({0}) Stato pezzo", 1)
            Case MachLogTypes.MACHGROUP_STATUS
               Return String.Format("({0}) Stato barra", 2)
            Case MachLogTypes.MACHINE_MODE = 3
               Return String.Format("({0}) Modalità macchina", 3)
            Case MachLogTypes.MACHINE_STATUS = 4
               Return String.Format("({0}) Stato macchina", 4)
            Case MachLogTypes.MACHINE_COMMAND = 5
               Return String.Format("({0}) Comando macchina", 5)
            Case MachLogTypes.READ_VAR = 6
               Return String.Format("({0}) Lettura variabile", 6)
            Case MachLogTypes.WRITE_VAR = 7
               Return String.Format("({0}) Scrittura variabile", 7)
            Case MachLogTypes.ALARM = 8
               Return String.Format("({0}) Allarme", 8)
            Case MachLogTypes.OPERATOR_MSG = 9
               Return String.Format("({0}) Messaggio opertore", 9)
            Case MachLogTypes.PROGRAM_SEND = 10
               Return String.Format("({0}) Invio programma", 10)
            Case Else
               Return "Altro"
         End Select
      End Get
   End Property
   Public ReadOnly Property sEventDateTime As String
      Get
         Return m_EventDateTime
      End Get
   End Property
   Public ReadOnly Property sEventValue As String
      Get
         Return m_Value
      End Get
   End Property
   Public ReadOnly Property sEventTick As Double
      Get
         Return m_EventDateTime.Ticks
      End Get
   End Property

   Public ReadOnly Property sPROD As String
      Get

         Return m_SupervisorId
      End Get
   End Property

   Sub New(EventDateTime As DateTime, EventType As MachLogTypes, SupervisorId As String, Value As String)
      m_EventDateTime = EventDateTime
      m_EventType = EventType
      m_SupervisorId = SupervisorId
      m_Value = Value
      If Not String.IsNullOrWhiteSpace(m_Value) Then
         Dim sValues() As String = Value.Split(cSeparator)
         Select Case m_EventType
            Case MachLogTypes.PART_STATUS
               m_ProdId = sValues(0)
               Dim MachGroup As DataLayer.DatabaseModels.MachGroupModel = DbControllers.m_MachGroupController.FindByMachGroupId(m_ProdId, sValues(1))
               If Not IsNothing(MachGroup) Then
                  m_MachGroupId = MachGroup.Name
               Else
                  m_MachGroupId = -1
               End If
               Dim Part As DataLayer.DatabaseModels.PartModel = DbControllers.m_PartController.FindByPartId(m_ProdId, sValues(2))
               If Not IsNothing(Part) Then
                  m_PartId = Part.PDN
               Else
                  m_PartId = -1
               End If
               m_Status = sValues(3)
            Case MachLogTypes.MACHGROUP_STATUS
               m_ProdId = sValues(0)
               Dim MachGroup As DataLayer.DatabaseModels.MachGroupModel = DbControllers.m_MachGroupController.FindByMachGroupId(m_ProdId, sValues(1))
               If Not IsNothing(MachGroup) Then
                  m_MachGroupId = MachGroup.Name
               Else
                  m_MachGroupId = -1
               End If
               m_Status = sValues(2)
            Case MachLogTypes.MACHINE_COMMAND

         End Select
      End If
   End Sub

   Sub New(MachLog As MachLog)
      Me.New(MachLog.AlarmDateTime, MachLog.ResultType, MachLog.VarAddress, MachLog.VarValue)
      'm_EventDateTime = MachLog.AlarmDateTime
      'm_EventType = MachLog.ResultType
      'm_SupervisorId = MachLog.VarAddress
      'm_Value = MachLog.VarValue
   End Sub

   Public Shared Function CreateMachLog(EventType As MachLogTypes, EventDateTime As DateTime, Value As String, SupervisorID As String)
      Return MachLog.CreateMachLog(EventType, EventDateTime, Value, SupervisorID)
   End Function

   Public Shared Function CreatePartStateLog(dtEvent As DateTime, Prod As Integer, MachGroup As Integer, Part As Integer, State As Integer, SupervisorID As String)
      Return CreateMachLog(MachLogTypes.PART_STATUS, dtEvent, Prod & cSeparator & MachGroup & cSeparator & Part & cSeparator & State, SupervisorID)
   End Function

   Public Shared Function CreateMachGroupStateLog(dtEvent As DateTime, Prod As Integer, MachGroup As Integer, State As Integer, SupervisorID As String)
      Return CreateMachLog(MachLogTypes.MACHGROUP_STATUS, dtEvent, Prod & cSeparator & MachGroup & cSeparator & State, SupervisorID)
   End Function

   Public Shared Function CreateMachCommandLog(Command As MachineCommands, sProgramId As String, nError As Integer, SupervisorID As String)
      Return CreateMachLog(MachLogTypes.MACHINE_COMMAND, DateTime.Now(), Command & cSeparator & sProgramId & cSeparator & nError, SupervisorID)
   End Function

   Public Shared Function CreateAlarmLog(dtEvent As DateTime, Alarm As String, SupervisorID As String)
      Return CreateMachLog(MachLogTypes.ALARM, dtEvent, Alarm, SupervisorID)
   End Function

   Public Shared Function CreateMachineModeLog(MachineMode As Integer, SupervisorID As String)
      Return CreateMachLog(MachLogTypes.MACHINE_MODE, DateTime.Now(), MachineMode, SupervisorID)
   End Function

   Public Shared Function CreateMachineStateLog(MachineStatus As Integer, SupervisorID As String)
      Return CreateMachLog(MachLogTypes.MACHINE_STATUS, DateTime.Now(), MachineStatus, SupervisorID)
   End Function

   Public Shared Function CreateReadVarLog(VarAddress As String, VarValue As String, SupervisorID As String)
      Return CreateMachLog(MachLogTypes.READ_VAR, DateTime.Now(), VarAddress & cSeparator & VarValue, SupervisorID)
   End Function

End Class
