Imports System.IO
Imports KeraLua
Imports Effector.Plugin.Lib

Public Module Lua_General

   Friend func_PlgGetNumberFromIni As LuaFunction = AddressOf Lua_PlgGetNumberFromIni
   Friend func_PlgGetStringFromIni As LuaFunction = AddressOf Lua_PlgGetStringFromIni
   Friend func_PlgWriteStringToIni As LuaFunction = AddressOf Lua_PlgWriteStringToIni
   'Friend func_PlgExecProcess As LuaFunction = AddressOf Lua_PlgExecProcess
   'Friend func_PlgExecProcessAsync As LuaFunction = AddressOf Lua_PlgExecProcessAsync
   'Friend func_PlgCheckExecProcessAsync As LuaFunction = AddressOf Lua_PlgCheckExecProcessAsync
   Friend func_PlgSetChangeState As LuaFunction = AddressOf Lua_PlgSetChangeState
   Friend func_PlgGetNextDoor As LuaFunction = AddressOf Lua_PlgGetNextDoor
   'Friend func_PlgOpenRestartWnd As LuaFunction = AddressOf Lua_PlgOpenRestartWnd
   Friend func_PlgMessageWnd As LuaFunction = AddressOf Lua_PlgMessageWnd
   Friend func_PlgListReset As LuaFunction = AddressOf Lua_PlgListReset
   Friend func_PlgMachineReset As LuaFunction = AddressOf Lua_PlgMachineReset

   Private Function Lua_PlgGetNumberFromIni(ByVal p As IntPtr) As Integer
      Dim state = Lua.FromIntPtr(p)
      ' 4 parametri: sSec, sKey, dDef
      Dim sSec As String = ""
      Dim sKey As String = ""
      Dim dDef As Integer = 0
      LuaCheckParam(state, 1, sSec)
      LuaCheckParam(state, 2, sKey)
      LuaCheckParam(state, 3, dDef)
      LuaClearStack(state)
      Dim dResult As Double = GetPluginPrivateProfileDouble(sSec, sKey, dDef)
      ' restituisco il risultato
      LuaSetParam(state, dResult)
      Return 1
   End Function

   Private Function Lua_PlgGetStringFromIni(ByVal p As IntPtr) As Integer
      Dim state = Lua.FromIntPtr(p)
      ' 4 parametri: sSec, sKey, sDef
      Dim sSec As String = ""
      Dim sKey As String = ""
      Dim sDef As String = 0
      LuaCheckParam(state, 1, sSec)
      LuaCheckParam(state, 2, sKey)
      LuaCheckParam(state, 3, sDef)
      LuaClearStack(state)
      Dim sResult As String = ""
      GetPluginPrivateProfileString(sSec, sKey, sDef, sResult)
      ' restituisco il risultato
      LuaSetParam(state, sResult)
      Return 1
   End Function

   Private Function Lua_PlgWriteStringToIni(ByVal p As IntPtr) As Integer
      Dim state = Lua.FromIntPtr(p)
      ' 4 parametri: sSec, sKey, sVal
      Dim sSec As String = ""
      Dim sKey As String = ""
      Dim sVal As Integer = 0
      LuaCheckParam(state, 1, sSec)
      LuaCheckParam(state, 2, sKey)
      LuaCheckParam(state, 3, sVal)
      LuaClearStack(state)
      Dim bResult As Boolean = WritePluginPrivateProfileString(sSec, sKey, sVal)
      ' restituisco il risultato
      LuaSetParam(state, bResult)
      Return 1
   End Function

   'Private Function Lua_PlgExecProcess(ByVal p As IntPtr) As Integer
   '   Dim state = Lua.FromIntPtr(p)
   '   Dim sDDFName As String = ""
   '   LuaCheckParam(state, 1, sDDFName)
   '   LuaClearStack(state)
   '   If Map.refDoorListPageVM.ExecCAMProcess(sDDFName) Then
   '      state.PushBoolean(True)
   '      Return 1
   '   Else
   '      Return 0
   '   End If
   'End Function

   'Private Function Lua_PlgExecProcessAsync(ByVal p As IntPtr) As Integer
   '   Dim state = Lua.FromIntPtr(p)
   '   Dim sDDFName As String = ""
   '   LuaCheckParam(state, 1, sDDFName)
   '   LuaClearStack(state)
   '   If Map.refDoorListPageVM.ExecCAMProcessAsync(sDDFName) Then
   '      state.PushBoolean(True)
   '      Return 1
   '   Else
   '      Return 0
   '   End If
   'End Function

   'Private Function Lua_PlgCheckExecProcessAsync(ByVal p As IntPtr) As Integer
   '   Dim state = Lua.FromIntPtr(p)
   '   LuaClearStack(state)
   '   Dim bHasExited As Boolean = False
   '   Dim nExitCode As Integer = -1
   '   If Map.refSupervisorFunction.PlgCheckExecProcessAsync(bHasExited, nExitCode) Then
   '      state.PushBoolean(bHasExited)
   '      state.PushInteger(nExitCode)
   '      Return 2
   '   Else
   '      Return 0
   '   End If
   'End Function

   Private Function Lua_PlgGetNextDoor(ByVal p As IntPtr) As Integer
      Dim state = Lua.FromIntPtr(p)
      LuaClearStack(state)
      Dim NextDoor As Door = Map.refDoorListPageVM.GetNextDoor()
      If Not IsNothing(NextDoor) Then
         Dim sPartProgramDirPath As String = ""
         GetPluginPrivateProfileString(S_GENERAL, "PartProgramDir", "", sPartProgramDirPath)
         ' restituisco il risultato
         LuaSetParam(state, NextDoor.nId)
         LuaSetParam(state, sPartProgramDirPath & "\" & NextDoor.sFileName)
         Return 2
      End If
      Return 0
   End Function

   'Private Function Lua_PlgOpenRestartWnd(ByVal p As IntPtr) As Integer
   '   Dim state = Lua.FromIntPtr(p)
   '   LuaClearStack(state)
   '   Dim RestartWnd As New RestartWndV(Application.Current.MainWindow, New RestartWndVM())
   '   RestartWnd.ShowDialog()
   '   Return 0
   'End Function

   Private Function Lua_PlgMessageWnd(ByVal p As IntPtr) As Integer
      Dim state = Lua.FromIntPtr(p)
      Dim sMsg As String = ""
      LuaCheckParam(state, 1, sMsg)
      LuaClearStack(state)
      MessageBox.Show(sMsg, "", MessageBoxButton.OK)
      Return 0
   End Function

   Private Function Lua_PlgMachineReset(ByVal p As IntPtr) As Integer
      Dim state = Lua.FromIntPtr(p)
      Dim nMachineIndex As Integer = 1
      LuaGetParam(state, 1, nMachineIndex)
      LuaClearStack(state)
      Map.refDoorListPageVM.ResetProductionQueue()
      'Map.refDoorListPageVM.ResetStateAfterReset()
      Map.refDoorListPageVM.WriteBackup()
      Return 0
   End Function

   Private Function Lua_PlgListReset(ByVal p As IntPtr) As Integer
      Dim state = Lua.FromIntPtr(p)
      LuaClearStack(state)
      If Map.refDoorListPageVM.bResetList Then
         Map.refDoorListPageVM.ResetResetList()
         state.PushBoolean(True)
         Return 1
      Else
         Return 0
      End If
   End Function


   Private Function Lua_PlgSetChangeState(ByVal p As IntPtr) As Integer
      Dim state = Lua.FromIntPtr(p)
      Dim nState As Integer = 0
      LuaGetParam(state, 1, nState)
      LuaClearStack(state)
      If nState = 1 Then
         Dim CurrDoor As Door = Map.refDoorListPageVM.DoorList.FirstOrDefault(Function(x) x.nState = Door.PartProgramStates.SENT_TO_NC)
         If Not IsNothing(CurrDoor) Then
            Dim Now As DateTime = DateTime.Now
            ' verifico se fine precedente impostata
            Dim nIndex As Integer = Map.refDoorListPageVM.DoorList.IndexOf(CurrDoor)
            If nIndex > 0 Then
               Dim PrevDoor As Door = Map.refDoorListPageVM.DoorList(nIndex - 1)
               If Not IsNothing(PrevDoor) AndAlso PrevDoor.nState = Door.PartProgramStates.MACHINE_START Then
                  PrevDoor.SetState(Door.PartProgramStates.PRODUCED)
                  PrevDoor.SetMachiningEnd(Now - TimeSpan.FromSeconds(1))
               End If
            End If
            CurrDoor.SetState(Door.PartProgramStates.MACHINE_START)
            CurrDoor.SetMachiningStart(Now)
         End If
      ElseIf nState = 2 Then
         Dim CurrDoor As Door = Map.refDoorListPageVM.DoorList.FirstOrDefault(Function(x) x.nState = Door.PartProgramStates.MACHINE_START)
         If Not IsNothing(CurrDoor) Then
            CurrDoor.SetState(Door.PartProgramStates.PRODUCED)
            CurrDoor.SetMachiningEnd(DateTime.Now())
         End If
      End If
      Map.refDoorListPageVM.WriteBackup()
      Return 0
   End Function

   Friend Function LuaInstallGeneral(state As Lua) As Boolean
      If IsNothing(state) Then Return False
      state.Register("PlgGetNumberFromIni", func_PlgGetNumberFromIni)
      state.Register("PlgGetStringFromIni", func_PlgGetStringFromIni)
      state.Register("PlgWriteStringToIni", func_PlgWriteStringToIni)
      'state.Register("PlgExecProcess", func_PlgExecProcess)
      'state.Register("PlgExecProcessAsync", func_PlgExecProcessAsync)
      'state.Register("PlgCheckExecProcessAsync", func_PlgCheckExecProcessAsync)
      state.Register("PlgGetNextDoor", func_PlgGetNextDoor)
      'state.Register("PlgOpenRestartWnd", func_PlgOpenRestartWnd)
      state.Register("PlgMessageWnd", func_PlgMessageWnd)
      state.Register("PlgSetChangeState", func_PlgSetChangeState)
      state.Register("PlgListReset", func_PlgListReset)
      state.Register("PlgMachineReset", func_PlgMachineReset)
      Return True
   End Function

End Module
