Imports System.Windows.Threading
Imports EgtBEAMWALL.Core
Imports EgtUILib
Imports EgtWPFLib5

Public Class BlockedWndVM
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

   Friend Event m_CloseWindow(bDialogResult As Boolean)

   Private m_Waiting_Timer As New DispatcherTimer

   Private WaitingTimer_TickHandler As New EventHandler(AddressOf WaitingTimer_Tick)

   Private m_Buttons_Visibility As Visibility = Visibility.Visible
   Public ReadOnly Property Buttons_Visibility As Visibility
      Get
         Return m_Buttons_Visibility
      End Get
   End Property

   Private m_nIndex As Integer = 0

#Region "Messages"

   Private m_Msg_Text As String
   Public ReadOnly Property Msg_Text As String
      Get
         Return m_Msg_Text
      End Get
   End Property

   Public ReadOnly Property Save_Msg As String
      Get
         Return EgtMsg(61966)
      End Get
   End Property

   Public ReadOnly Property DoNotSave_Msg As String
      Get
         Return EgtMsg(61967)
      End Get
   End Property

#End Region    ' Messages

   ' Definizione comandi
   Private m_cmdSave As ICommand
   Private m_cmdDoNotSave As ICommand

#End Region    ' FIELDS & PROPERTIES

   Sub New()
      If DbControllers.bNetwork Then
         m_Buttons_Visibility = Visibility.Visible
         m_Msg_Text = "Ottimizzatore bloccato per ricalcolo trave. Salvare le modifiche apportate al progetto?"
      Else
         m_Msg_Text = "In attesa del ricalcolo da parte del supervisore."
         m_Buttons_Visibility = Visibility.Collapsed
         Save()
      End If
      NotifyPropertyChanged(NameOf(Msg_Text))
      NotifyPropertyChanged(NameOf(Buttons_Visibility))
      ' avvio contatore
      m_Waiting_Timer.Interval = TimeSpan.FromMilliseconds(500)
      AddHandler m_Waiting_Timer.Tick, WaitingTimer_TickHandler
      m_Waiting_Timer.Start()
   End Sub

   Private Sub WaitingTimer_Tick()
      ' se notifica di chiusura da supervisor, esco
      If Not SupervisorCommThread.bViewerOptimizerBlocked And Map.refMainWindowVM.m_ManagingSupervisorStop Then
         If m_Waiting_Timer.IsEnabled Then
            m_Waiting_Timer.Stop()
            RemoveHandler m_Waiting_Timer.Tick, WaitingTimer_TickHandler
         End If
         ' se mi viene passato un Id di MachGroup aggiunto
         If SupervisorCommThread.m_AddedMachGroupId > 0 Then
            ' ricarico progetto
            Dim sCurrFilePath As String = ""
            EgtGetCurrFilePath(sCurrFilePath)
            EgtOpenFile(sCurrFilePath)
            ' aggiungo a lista nuovo MachGroup
            Map.refProjectVM.MachGroupPanelVM.AddMachGroupToList(SupervisorCommThread.m_AddedMachGroupId)
            '' seleziono ultimo gruppo
            'Map.refProjectVM.MachGroupPanelVM.SelLastMachGroup()
         End If
         Map.refMainWindowVM.m_ManagingSupervisorStop = False
         RaiseEvent m_CloseWindow(True)
      End If
      ' incremento contatore
      m_nIndex += 1
      ' se passata attesa per risposta utente
      If m_nIndex = 17 Then
         ' aggiorno finestra con attesa fine calcolo
         m_Msg_Text = "In attesa del ricalcolo da parte del supervisore."
         m_Buttons_Visibility = Visibility.Collapsed
         NotifyPropertyChanged(NameOf(Buttons_Visibility))
         NotifyPropertyChanged(NameOf(Msg_Text))
         ' mando messaggio di blocco avvenuto
         DbControllers.m_StatusMapController.UpdateAction("", Map.refProdManagerVM.CurrProd.nProdId, Map.refProdManagerVM.CurrProd.nProdId, StatusMapItemType.Comm, StatusMapOpType.ViewOptimStoped, "")
         'ElseIf m_nIndex > 17 Then
         '   'If m_Waiting_Timer.IsEnabled Then m_Waiting_Timer.Stop()
         '   If Not SupervisorCommThread.bViewerOptimizerBlocked Then
         '      ' ricarico progetto
         '      Dim sCurrFilePath As String = ""
         '      EgtGetCurrFilePath(sCurrFilePath)
         '      EgtOpenFile(sCurrFilePath)
         '      RaiseEvent m_CloseWindow(True)
         '   End If
      End If
   End Sub

#Region "COMMANDS"

#Region "Save"

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

   ''' <summary>
   ''' Execute the Exec.  This method is invoked by the ExecCommand.
   ''' </summary>
   Public Sub Save()
      ' salvo progetto
      Map.refProdManagerVM.Save()
      ' mando messaggio di blocco avvenuto
      DbControllers.m_StatusMapController.UpdateAction("", Map.refProdManagerVM.CurrProd.nProdId, Map.refProdManagerVM.CurrProd.nProdId, StatusMapItemType.Comm, StatusMapOpType.ViewOptimStoped, "")
   End Sub

#End Region    ' Save

#Region "DoNotSave"

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

   ''' <summary>
   ''' Execute the Exec.  This method is invoked by the ExecCommand.
   ''' </summary>
   Public Sub DoNotSave()
      ' mando messaggio di blocco avvenuto
      DbControllers.m_StatusMapController.UpdateAction("", Map.refProdManagerVM.CurrProd.nProdId, Map.refProdManagerVM.CurrProd.nProdId, StatusMapItemType.Comm, StatusMapOpType.ViewOptimStoped, "")
   End Sub

#End Region    ' DoNotSave

#End Region    ' COMMANDS

End Class
