Imports System.Threading
Imports System.Windows.Threading

Public Module LoadingWndHelper

   Public Enum ActiveIds As Integer
      OPENPROJ = 1
      SAVEPROJ = 2
      IMPORTBTL = 3
      GOTOPROD = 4
      OPENPROD = 5
      SAVEPROD = 6
      GOTOPROJ = 7
      EXPORTPROJECT = 8
      IMPORTPROJECT = 9
      CHANGEPARAM = 10
      CREATINGPDF = 11
      CHANGEMATERIAL = 12
      BACKUP = 13
      RESTORE = 14
   End Enum

   Dim m_MainWindow As Windows.Window
   Public Sub SetMainWindow(MainWindow As Windows.Window)
      m_MainWindow = MainWindow
   End Sub

   Private m_bMainWindow_IsActive As Boolean = True
   Public Sub SetMainWindowIsActive(IsActive As Boolean)
      m_bMainWindow_IsActive = IsActive
   End Sub

   Delegate Sub CallbackLoadingDlg(ByRef CurrStep As Integer, ByRef StepText As String, ByRef nProgress As Integer, ByRef nNextProgress As Integer, ByRef MainWindow_IsActive As Boolean, ByRef bClose As Boolean)

   Private m_thread As Thread
   Private m_LoadingWndVM As Core.LoadingWndVM
   Private m_CurrStep As Integer
   Private m_StepText As String
   Private m_nProgress As Integer
   Private m_nNextProgress As Integer
   Private m_bClose As Boolean
   Private m_bActiveId As Integer = -1

   Public Function OpenLoadingWnd(Id As Integer, nSteps As Integer, TotText As String, FirstStepText As String, nNextProgress As Integer) As Boolean
      If m_bActiveId > 0 Then Return False
      m_bActiveId = Id
      m_LoadingWndVM = Nothing
      m_CurrStep = 1
      m_StepText = FirstStepText
      m_nProgress = 1
      m_nNextProgress = nNextProgress
      m_bClose = False
      Dim Top As Double = m_MainWindow.Top + (m_MainWindow.Height / 2) - 100
      Dim Left As Double = m_MainWindow.Left + (m_MainWindow.Width / 2) - 150
      Dim WindowStartupLocation As Windows.WindowStartupLocation = Windows.WindowStartupLocation.Manual
      If m_MainWindow.WindowState = Windows.WindowState.Maximized Then
         WindowStartupLocation = Windows.WindowStartupLocation.CenterOwner
      End If
      m_thread = New Thread(Sub()
                               m_LoadingWndVM = New Core.LoadingWndVM(nSteps, TotText, AddressOf LoadingCallback)
                               m_LoadingWndVM.UpdateProgress(m_CurrStep, m_StepText, m_nProgress, m_nNextProgress)
                               Dim LoadingWnd As New LoadingWndV(Nothing, m_LoadingWndVM)
                               ' posiziono la finestra
                               If WindowStartupLocation = Windows.WindowStartupLocation.CenterScreen Then
                                  LoadingWnd.WindowStartupLocation = Windows.WindowStartupLocation.CenterScreen
                               Else
                                  LoadingWnd.Top = Top
                                  LoadingWnd.Left = Left
                               End If
                               LoadingWnd.Show()
                               Dispatcher.Run()
                            End Sub)
      m_thread.SetApartmentState(ApartmentState.STA)
      m_thread.Start()
      Return True
   End Function

   Private Sub LoadingCallback(ByRef CurrStep As Integer, ByRef StepText As String, ByRef nProgress As Integer, ByRef nNextProgress As Integer, ByRef MainWindow_IsActive As Boolean, ByRef bClose As Boolean)
      CurrStep = m_CurrStep
      StepText = m_StepText
      nProgress = m_nProgress
      nNextProgress = m_nNextProgress
      m_bMainWindow_IsActive = MainWindow_IsActive
      bClose = m_bClose
   End Sub

   Public Sub UpdateLoadingWnd(Id As Integer, CurrStep As Integer, StepText As String, nProgress As Integer, nNextProgress As Integer)
      If Id <> m_bActiveId Then Return
      m_CurrStep = CurrStep
      m_StepText = StepText
      m_nProgress = nProgress
      m_nNextProgress = nNextProgress
   End Sub

   Public Sub CloseLoadingWnd(Id As Integer)
      If Id <> m_bActiveId Then Return
      m_bClose = True
      m_bActiveId = -1
   End Sub

End Module
