Imports Effector.Plugin.Lib
Imports Newtonsoft.Json
Imports System.Collections.ObjectModel
Imports System.Globalization
Imports System.IO

Public Class RestartWndVM
   Inherits VMBase

   Friend Event m_CloseWindow(bDialogResult As Boolean)

   ' Definizione comandi
   Private m_cmdOk As ICommand

   Private m_RestartDoorList As ObservableCollection(Of RestartDoor)
   Public ReadOnly Property RestartDoorList As ObservableCollection(Of RestartDoor)
      Get
         Return m_RestartDoorList
      End Get
   End Property

   Private m_sMachineImagePath As String
   Public ReadOnly Property sMachineImagePath As String
      Get
         Return m_sMachineImagePath
      End Get
   End Property

   'Private m_DoorOnMachineArray(4) As RestartDoor
   'Public ReadOnly Property DoorOnMachineArray As RestartDoor()
   '   Get
   '      Return m_DoorOnMachineArray
   '   End Get
   'End Property

   Private m_DoorOnMachineArray As New ObservableCollection(Of RestartDoor)
   Public ReadOnly Property DoorOnMachineArray As ObservableCollection(Of RestartDoor)
      Get
         Return m_DoorOnMachineArray
      End Get
   End Property

   Public ReadOnly Property VariableList As ObservableCollection(Of Variable)
      Get
         Return Map.refMachinePageVM.VariableList
      End Get
   End Property

#Region "CONSTRUCTOR"

   Sub New()
      Dim sBackupFilePath As String = ""
      GetPluginPrivateProfileString("General", "BackupDir", "", sBackupFilePath)
      sBackupFilePath &= "\LuaBackup.json"

      Dim JsonLuaDoorList As List(Of JsonLuaDoor)
      If File.Exists(sBackupFilePath) Then
         Dim sReadedFile As String = File.ReadAllText(sBackupFilePath)
         JsonLuaDoorList = JsonConvert.DeserializeObject(Of List(Of JsonLuaDoor))(sReadedFile)
         JsonLuaDoorList = JsonLuaDoorList.OrderBy(Of Integer)(Function(x) x.nCircIndex).ToList()
      End If

      ' creo posti in lista descrizioni
      For nIndex As Integer = 0 To 4
         m_DoorOnMachineArray.Add(Nothing)
      Next

      ' leggo presenze porta
      For nPositionIndex As Integer = 4 To 0 Step -1
         If GetVariableFromName(1, "@WP_PR_0" & nPositionIndex.ToString()).dValue <> 0 Then
            ' verifico indice
            Dim nCircindex As Integer = GetVariableFromName(1, "@DOORN_S" & nPositionIndex.ToString()).nValue
            If nCircindex <> 0 Then
               m_DoorOnMachineArray(nPositionIndex) = New RestartDoor(JsonLuaDoorList(nCircindex - 1))
            End If
         End If
      Next
      NotifyPropertyChanged(NameOf(DoorOnMachineArray))


      'If File.Exists(sBackupFilePath) Then
      '   Dim sReadedFile As String = File.ReadAllText(sBackupFilePath)

      '   Dim JsonLuaDoorList As List(Of JsonLuaDoor) = JsonConvert.DeserializeObject(Of List(Of JsonLuaDoor))(sReadedFile)
      '   JsonLuaDoorList = JsonLuaDoorList.OrderBy(Of Integer)(Function(x) x.nCircIndex).ToList()

      '   m_RestartDoorList = New ObservableCollection(Of RestartDoor)((From JsonLuaDoor In Map.refDoorListPageVM.DoorList
      '                                                                 Select New RestartDoor(JsonLuaDoor)).ToList())
      '   For JsonLuaDoorIndex = 0 To JsonLuaDoorList.Count - 1
      '      Dim JsonLuaDoor As JsonLuaDoor = JsonLuaDoorList(JsonLuaDoorIndex)
      '      Dim RestartDoor As RestartDoor = m_RestartDoorList.FirstOrDefault(Function(x) x.nId = JsonLuaDoor.nId)
      '      If Not IsNothing(RestartDoor) Then
      '         RestartDoor.Update(JsonLuaDoor)
      '      End If


      '   Next

      'End If



      ' se macchina 1
      Dim sResourcesDirPath As String = ""
      GetPluginPrivateProfileString(S_GENERAL, K_RESOURCESDIR, "", sResourcesDirPath)
      m_sMachineImagePath = sResourcesDirPath & "\Multiax-C1223Ripartenza.png"
   End Sub

#End Region    ' CONSTRUCTOR

#Region "METHODS"

   Private Function GetVariableFromName(nMachIndex As Integer, sName As String) As Variable
      Return Map.refMachinePageVM.VariableList.FirstOrDefault(Function(x) x.nMachine = nMachIndex AndAlso x.sName = sName)
   End Function
#End Region    ' METHODS

#Region "COMMANDS"

#Region "Ok"

   Public ReadOnly Property Ok_Command As ICommand
      Get
         If m_cmdOk Is Nothing Then
            m_cmdOk = New Command(AddressOf Ok)
         End If
         Return m_cmdOk
      End Get
   End Property

   Public Sub Ok()
      RaiseEvent m_CloseWindow(True)
   End Sub

#End Region ' Ok

#End Region    ' COMMANDS

End Class

Public Class StateToVisibilityConverter
   Implements IValueConverter

   Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
      If IsNothing(value) OrElse Not TypeOf value Is Integer Then Return Visibility.Collapsed
      Return If(value > 0, Visibility.Visible, Visibility.Collapsed)
   End Function

   Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
      Throw New NotImplementedException()
   End Function

End Class

Public Class RestartDoor
   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

   Public Property SelDoorState As IdNameStruct
      Get
         Return m_DoorStateList.FirstOrDefault(Function(x) x.Id = m_nState)
      End Get
      Set(value As IdNameStruct)
         m_nState = value.Id
      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

   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

   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

   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

   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

   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

   Sub New(Id As Integer, IndiceCirc As Integer, Stato As Integer, CodicePorta As String, DescrizionePorta As String, NomeDDF As String, NomeCSV As String,
           CN_macchina_1 As String, CN_macchina_2 As String, tempo_carico As Long, tempo_inizio_lav_1 As Long, tempo_fine_lav_1 As Long, tempo_inizio_lav_2 As Long,
           tempo_fine_lav_2 As Long, tempo_scarico As Long)
      m_nId = Id
      m_nCircIndex = IndiceCirc
      m_nState = Stato
      m_sDoorCode = CodicePorta
      m_sDoorDescription = DescrizionePorta
      m_sDDFName = NomeDDF
      m_sCSVName = NomeCSV
      m_sM1CNPath = CN_macchina_1
      m_sM2CNPath = CN_macchina_2
      m_dtLoadTime = New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(tempo_carico).ToLocalTime()
      m_dtMachining1Start = New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(tempo_inizio_lav_1).ToLocalTime()
      m_dtMachining1End = New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(tempo_fine_lav_1).ToLocalTime()
      m_dtMachining2Start = New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(tempo_inizio_lav_2).ToLocalTime()
      m_dtMachining2End = New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(tempo_fine_lav_2).ToLocalTime()
      m_dtUnloadTime = New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(tempo_scarico).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 New(JsonLuaDoor As JsonLuaDoor)
      m_nId = JsonLuaDoor.nId
      m_nCircIndex = JsonLuaDoor.nCircIndex
      m_nState = JsonLuaDoor.nState
      m_sDDFName = JsonLuaDoor.sDDFName
      m_sCSVName = JsonLuaDoor.sCSVName
      m_sM1CNPath = JsonLuaDoor.sM1CNPath
      m_sM2CNPath = JsonLuaDoor.sM2CNPath
      m_dtLoadTime = JsonLuaDoor.dtLoadTime
      m_dtMachining1Start = JsonLuaDoor.dtMachining1Start
      m_dtMachining1End = JsonLuaDoor.dtMachining1End
      m_dtMachining2Start = JsonLuaDoor.dtMachining2Start
      m_dtMachining2End = JsonLuaDoor.dtMachining2End
      m_dtUnloadTime = JsonLuaDoor.dtUnloadTime
   End Sub

End Class