Imports System.Runtime.InteropServices
Imports System.Threading
Imports EgtUILib
Imports EgtWPFLib5

Public Class MyStatusBarVM
   Inherits EgtWPFLib5.StatusBarVM

   ' Funzioni di callback per output in interfaccia da LUA
   Private m_ProcEventsCallback As New ProcessEventsCallback(AddressOf ProcessEvents)
   Private m_OutTextCallback As New OutTextCallback(AddressOf OutText)

   Private m_bStopProgress As Boolean
   Friend ReadOnly Property bStopProgress As Boolean
      Get
         Return m_bStopProgress
      End Get
   End Property

   Public ReadOnly Property CurrMachine As String
      Get
         Return If(Not IsNothing(Map.refMachinePanelVM.SelectedMachine), Map.refMachinePanelVM.SelectedMachine.Name, "")
      End Get
   End Property

   ' Definizione comandi
   Private m_cmdStopProgress As ICommand

   Sub New()
      ' Creo riferimento a questa classe in Map
      Map.SetRefMyStatusBarVM(Me)
      ' Installo funzione gestione eventi per lua
      EgtSetProcessEvents(m_ProcEventsCallback)
      ' Installo funzione output testo su status per lua
      EgtSetOutText(m_OutTextCallback)
   End Sub

   Friend Sub ResetStopProgress()
      m_bStopProgress = False
   End Sub

   Public Function OutText(ByRef psText As IntPtr) As Boolean
      ' Assegno stringa
      OutputMessage = (Marshal.PtrToStringUni(psText))
      ' Costringo ad aggiornare
      UpdateUI()
      Return True
   End Function

   Private Function ProcessEvents(ByVal nProg As Integer, ByVal nPause As Integer) As Integer
      ' se nesting in corso
      If Map.refOptimizePanelVM.m_bNestingRunning Then
         ' gestisco nel nesting
         Return Map.refOptimizePanelVM.ProcessEvents(nProg, nPause)
      End If
      ' Se previsto, imposto progress
      If nProg > 0 Then SetLoadingProgress(Math.Min(nProg, 100))
      ' Costringo ad aggiornare
      UpdateUI()
      ' Eventuale attesa
      Thread.Sleep(nPause)
      ' Ritorno eventuale stop
      If m_bStopProgress Then
         m_bStopProgress = False
         Return 1
      Else
         Return 0
      End If
   End Function

   Friend Sub StartLoading(sMessage As String, bIsStop As Boolean)
      Map.refMyStatusBarVM.SetLoadingProgress_Visibility(True)
      Map.refMyStatusBarVM.SetStopProgress_IsActive(bIsStop)
      Map.refMyStatusBarVM.SetStopProgress_IsEnabled(bIsStop)
      Map.refMyStatusBarVM.SetOutputMessage(sMessage)
   End Sub

   Friend Sub EndLoading(sMessage As String, Optional nMessageTime As Integer = 3)
      Map.refMyStatusBarVM.SetLoadingProgress_Visibility(False)
      Map.refMyStatusBarVM.SetStopProgress_IsActive(False)
      Map.refMyStatusBarVM.SetStopProgress_IsEnabled(False)
      Map.refMyStatusBarVM.SetOutputMessage(sMessage, nMessageTime)
   End Sub

   Friend Sub RefreshMachName()
      NotifyPropertyChanged(NameOf(CurrMachine))
   End Sub

#Region "COMMANDS"

#Region "StopProgress"

   ''' <summary>
   ''' Returns a command that do Exec.
   ''' </summary>
   Public ReadOnly Property StopProgress_Command As ICommand
      Get
         If m_cmdStopProgress Is Nothing Then
            m_cmdStopProgress = New Command(AddressOf StopProgress)
         End If
         Return m_cmdStopProgress
      End Get
   End Property

   ''' <summary>
   ''' Execute the Exec.  This method is invoked by the ExecCommand.
   ''' </summary>
   Public Sub StopProgress()
      m_bStopProgress = True
   End Sub

#End Region    ' StopProgress

#End Region    ' COMMANDS

End Class
