Imports System.Globalization
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Converters
Imports Effector.Plugin.StoneCut.Door
Imports Effector.Plugin.Lib

Public Class JsonDoor

   Private m_nListIndex As Integer
   Public ReadOnly Property nListIndex As Integer
      Get
         Return m_nListIndex
      End Get
   End Property

   Private m_nId As Integer
   Public ReadOnly Property nId As Integer
      Get
         Return m_nId
      End Get
   End Property

   Private m_nTable As Integer
   Public ReadOnly Property nTable As Integer
      Get
         Return m_nTable
      End Get
   End Property

   Private m_sFileName As String
   Public Property sFileName As String
      Get
         Return m_sFileName
      End Get
      Set(value As String)
         m_sFileName = value
      End Set
   End Property

   Private m_dFileDimension As Double
   Public Property dFileDimension As Double
      Get
         Return m_dFileDimension
      End Get
      Set(value As Double)
         m_dFileDimension = value
      End Set
   End Property

   Private m_nState As PartProgramStates = PartProgramStates.NULL
   <JsonProperty>
   <JsonConverter(GetType(StringEnumConverter))>
   Public ReadOnly Property nState As PartProgramStates
      Get
         Return m_nState
      End Get
   End Property

#Region "Statistics"

   Private m_dtMachiningStart As DateTime = DateTime.MinValue
   Public ReadOnly Property dtMachiningStart As DateTime
      Get
         Return m_dtMachiningStart
      End Get
   End Property

   Private m_dtMachiningEnd As DateTime = DateTime.MinValue
   Public ReadOnly Property dtMachiningEnd As DateTime
      Get
         Return m_dtMachiningEnd
      End Get
   End Property

#End Region    ' Statistics

   <JsonConstructor>
   Sub New(nId As Integer, sFileName As String, nTable As Integer, dFileDimension As Double)
      m_nId = nId
      m_sFileName = sFileName
      m_nTable = nTable
      m_dFileDimension = dFileDimension
   End Sub

   Sub New(nListIndex As Integer, Door As Door)
      m_nListIndex = nListIndex
      m_nId = Door.nId
      m_sFileName = Door.sFileName
      m_nTable = Door.nTable
      m_dFileDimension = Door.dFileDimension
   End Sub

End Class

Public Class JsonLuaDoor
   Inherits VMBase

   Private m_nId As Integer = -1
   Public ReadOnly Property nId As Integer
      Get
         Return m_nId
      End Get
   End Property

   Private m_nCircIndex As Integer = -1
   Public ReadOnly Property nCircIndex As Integer
      Get
         Return m_nCircIndex
      End Get
   End Property

   Private m_nState As Integer = 0
   Public ReadOnly Property nState As Integer
      Get
         Return m_nState
      End Get
   End Property

   Private m_DoorStateList As New List(Of IdNameStruct)({New IdNameStruct(0, "Not On Machine"),
                                                         New IdNameStruct(1, "Imported"),
                                                         New IdNameStruct(2, "On Load"),
                                                         New IdNameStruct(3, "On Machine 1"),
                                                         New IdNameStruct(4, "On Medium Station"),
                                                         New IdNameStruct(6, "On Machine 2"),
                                                         New IdNameStruct(7, "On Unload"),
                                                         New IdNameStruct(8, "Produced"),
                                                         New IdNameStruct(100, "Scrap")})
   Public ReadOnly Property DoorStateList As List(Of IdNameStruct)
      Get
         Return m_DoorStateList
      End Get
   End Property

   Private m_SelDoorState As IdNameStruct
   Public Property SelDoorState As IdNameStruct
      Get
         Return m_SelDoorState
      End Get
      Set(value As IdNameStruct)
         m_SelDoorState = value
      End Set
   End Property

   Private m_sDoorCode As String = ""
   Public ReadOnly Property sDoorCode As String
      Get
         Return m_sDoorCode
      End Get
   End Property

   Private m_sDoorDescription As String = ""
   Public ReadOnly Property sDoorDescription As String
      Get
         Return m_sDoorDescription
      End Get
   End Property

   Private m_sDDFName As String = ""
   Public ReadOnly Property sDDFName As String
      Get
         Return m_sDDFName
      End Get
   End Property

   Private m_sCSVName As String = ""
   Public ReadOnly Property sCSVName As String
      Get
         Return m_sCSVName
      End Get
   End Property

   Private m_sM1CNPath As String = ""
   Public ReadOnly Property sM1CNPath As String
      Get
         Return m_sM1CNPath
      End Get
   End Property

   Private m_sM2CNPath As String = ""
   Public ReadOnly Property sM2CNPath As String
      Get
         Return m_sM2CNPath
      End Get
   End Property

   Private m_dtLoadTime As DateTime
   Public ReadOnly Property sLoadTime As String
      Get
         Return m_dtLoadTime.ToString("yy/MM/dd HH:mm:ss")
      End Get
   End Property
   Public ReadOnly Property dtLoadTime As DateTime
      Get
         Return m_dtLoadTime
      End Get
   End Property

   Private m_dtMachining1Start As DateTime
   Public ReadOnly Property sMachining1Start As String
      Get
         Return m_dtMachining1Start.ToString("yy/MM/dd HH:mm:ss")
      End Get
   End Property
   Public ReadOnly Property dtMachining1Start As DateTime
      Get
         Return m_dtMachining1Start
      End Get
   End Property

   Private m_dtMachining1End As DateTime
   Public ReadOnly Property sMachining1End As String
      Get
         Return m_dtMachining1End.ToString("yy/MM/dd HH:mm:ss")
      End Get
   End Property
   Public ReadOnly Property dtMachining1End As DateTime
      Get
         Return m_dtMachining1End
      End Get
   End Property

   Private m_dtMachining2Start As DateTime
   Public ReadOnly Property sMachining2Start As String
      Get
         Return m_dtMachining2Start.ToString("yy/MM/dd HH:mm:ss")
      End Get
   End Property
   Public ReadOnly Property dtMachining2Start As DateTime
      Get
         Return m_dtMachining2Start
      End Get
   End Property

   Private m_dtMachining2End As DateTime
   Public ReadOnly Property sMachining2End As String
      Get
         Return m_dtMachining2End.ToString("yy/MM/dd HH:mm:ss")
      End Get
   End Property
   Public ReadOnly Property dtMachining2End As DateTime
      Get
         Return m_dtMachining2End
      End Get
   End Property

   Private m_dtUnloadTime As DateTime
   Public ReadOnly Property sUnloadTime As String
      Get
         Return m_dtUnloadTime.ToString("yy/MM/dd HH:mm:ss")
      End Get
   End Property
   Public ReadOnly Property dtUnloadTime As DateTime
      Get
         Return m_dtUnloadTime
      End Get
   End Property

   <JsonConstructor>
   Sub New(nId As Integer, nCircIndex As Integer, nState As Integer, sDoorCode As String, sDoorDescription As String, sDDFName As String, sCSVName As String,
           sM1CNPath As String, sM2CNPath As String, nLoadTime As Long, nMachining1Start As Long, nMachining1End As Long, nMachining2Start As Long,
           nMachining2End As Long, nUnloadTime As Long)
      m_nId = nId
      m_nCircIndex = nCircIndex
      m_nState = nState
      m_sDoorCode = sDoorCode
      m_sDoorDescription = sDoorDescription
      m_sDDFName = sDDFName
      m_sCSVName = sCSVName
      m_sM1CNPath = sM1CNPath
      m_sM2CNPath = sM2CNPath
      m_dtLoadTime = New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(nLoadTime).ToLocalTime()
      m_dtMachining1Start = New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(nMachining1Start).ToLocalTime()
      m_dtMachining1End = New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(nMachining1End).ToLocalTime()
      m_dtMachining2Start = New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(nMachining2Start).ToLocalTime()
      m_dtMachining2End = New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(nMachining2End).ToLocalTime()
      m_dtUnloadTime = New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(nUnloadTime).ToLocalTime()
   End Sub

   Sub New(Door As Door)
      m_nId = Door.nId
      m_nCircIndex = 0
      m_nState = 0
      m_sDDFName = Door.sFileName
      'm_sCSVName = Door.sCSVName
      m_sM1CNPath = ""
      m_sM2CNPath = ""
      m_dtLoadTime = DateTime.MinValue
      m_dtMachining1Start = DateTime.MinValue
      m_dtMachining1End = DateTime.MinValue
      m_dtMachining2Start = DateTime.MinValue
      m_dtMachining2End = DateTime.MinValue
      m_dtUnloadTime = DateTime.MinValue
   End Sub

   Friend Sub Update(JsonLuaDoor As JsonLuaDoor)
      m_nCircIndex = JsonLuaDoor.m_nCircIndex
      m_nState = JsonLuaDoor.m_nState
      m_sM1CNPath = JsonLuaDoor.m_sM1CNPath
      m_sM2CNPath = JsonLuaDoor.m_sM2CNPath
      m_dtLoadTime = JsonLuaDoor.m_dtLoadTime
      m_dtMachining1Start = JsonLuaDoor.m_dtMachining1Start
      m_dtMachining1End = JsonLuaDoor.m_dtMachining1End
      m_dtMachining2Start = JsonLuaDoor.m_dtMachining2Start
      m_dtMachining2End = JsonLuaDoor.m_dtMachining2End
      m_dtUnloadTime = JsonLuaDoor.m_dtUnloadTime
   End Sub

End Class
