Imports System.Collections.ObjectModel
Imports System.IO
Imports EgtBEAMWALL.Core
Imports EgtBEAMWALL.Core.ConstBeam
Imports EgtUILib
Imports EgtWPFLib5

Public Class MainMenuVM
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

   Private m_MainMenu_IsEnabled As Boolean = True
   Public ReadOnly Property MainMenu_IsEnabled As Boolean
      Get
         Return m_MainMenu_IsEnabled
      End Get
   End Property

   Public Property Supervisor_IsChecked As Boolean
      Get
         Return m_SelPage = Pages.SUPERVISOR
      End Get
      Set(value As Boolean)
         If value Then SelPage = Pages.SUPERVISOR
      End Set
   End Property

   Public Property Config_IsChecked As Boolean
      Get
         Return m_SelPage = Pages.CONFIG
      End Get
      Set(value As Boolean)
         If value Then SelPage = Pages.CONFIG
      End Set
   End Property

   Public Property Inputs_IsChecked As Boolean
      Get
         Return m_SelPage = Pages.INPUTS
      End Get
      Set(value As Boolean)
         If value Then SelPage = Pages.INPUTS
      End Set
   End Property

   Public Property Outputs_IsChecked As Boolean
      Get
         Return m_SelPage = Pages.OUTPUTS
      End Get
      Set(value As Boolean)
         If value Then SelPage = Pages.OUTPUTS
      End Set
   End Property

   Private m_Inputs_Visibility As Visibility
   Public ReadOnly Property Inputs_Visibility As Visibility
      Get
         Return m_Inputs_Visibility
      End Get
   End Property

   Private m_Outputs_Visibility As Visibility
   Public ReadOnly Property Outputs_Visibility As Visibility
      Get
         Return m_Outputs_Visibility
      End Get
   End Property

   Private m_SelPage As Integer = -1
   Public Property SelPage As Integer
      Get
         Return m_SelPage
      End Get
      Set(value As Integer)
         ' lancio selezione pagina con verifica file modificato
         SetSelPage(value)
      End Set
   End Property
   ' funzione che permette di cambiare pagina
   ' bVerifyModification: se vero verifica modifiche su file e chiede di salvare
   Friend Sub SetSelPage(Page As Pages, Optional bVerifyModification As Boolean = True)
      Dim bOk As Boolean = True
      ' Esco dallo stato corrente
      Select Case m_SelPage
         Case Pages.SUPERVISOR
            bOk = ExitSUPERVISOR(bVerifyModification)
         Case Pages.CONFIG
            bOk = ExitCONFIG()
         Case Pages.INPUTS
            bOk = ExitINPUTS()
         Case Pages.OUTPUTS
            bOk = ExitOUTPUTS()
      End Select
      If bOk Then
         ' Entro nel nuovo stato
         m_SelPage = Page
         Select Case m_SelPage
            Case Pages.SUPERVISOR
               ' Map.refMachGroupPanelVM.SetMachGroupState(False)
               InitSUPERVISOR()
               'Map.refTopCommandBarVM.IsEnabled = False
            Case Pages.CONFIG
               InitCONFIG()
               'Map.refMachGroupPanelVM.SetMachGroupState(False)
               'Map.refCONFIGTabVM.InitCONFIGation()
               'Map.refTopCommandBarVM.IsEnabled = False
            Case Pages.INPUTS
               InitINPUTS()
            Case Pages.OUTPUTS
               InitOUTPUTS()
         End Select
      End If
      ' aggiorno visualizzazione RadioButton
      NotifyPropertyChanged(NameOf(Supervisor_IsChecked))
      NotifyPropertyChanged(NameOf(Config_IsChecked))
      NotifyPropertyChanged(NameOf(Inputs_IsChecked))
      NotifyPropertyChanged(NameOf(Outputs_IsChecked))
   End Sub

#Region "Messages"

   Public ReadOnly Property Viewer_Msg As String
      Get
         Return EgtMsg(61830)
      End Get
   End Property

   Public ReadOnly Property Optimizer_Msg As String
      Get
         Return EgtMsg(61831)
      End Get
   End Property

   Public ReadOnly Property Supervisor_Msg As String
      Get
         Return EgtMsg(62500)
      End Get
   End Property

   Public ReadOnly Property Configuration_Msg As String
      Get
         Return EgtMsg(61832)
      End Get
   End Property

   Public ReadOnly Property Inputs_Msg As String
      Get
         Return EgtMsg(61930)
      End Get
   End Property

   Public ReadOnly Property Outputs_Msg As String
      Get
         Return EgtMsg(61931)
      End Get
   End Property

#End Region    ' Messages

   ' Definizione comandi
   Private m_cmdViewer As ICommand
   Private m_cmdOptimizer As ICommand

#End Region    ' Fields & Properties

#Region "CONSTRUCTOR"

   Sub New()
      ' Creo riferimento a questa classe in EgtCAM5Map
      Map.SetRefMainMenuVM(Me)
   End Sub

#End Region    ' CONSTRUCTOR

#Region "METHODS"

   Friend Sub SetInputOutputVisibility()
      If CurrentMachine.NCType = NCTypes.TPA Then
         m_Inputs_Visibility = Visibility.Collapsed
         m_Outputs_Visibility = Visibility.Collapsed
      End If
   End Sub

   Friend Sub SetMainMenuIsEnabled(bIsEnabled As Boolean)
      m_MainMenu_IsEnabled = bIsEnabled
      NotifyPropertyChanged(NameOf(MainMenu_IsEnabled))
   End Sub

   Private Function InitSUPERVISOR() As Boolean
      Map.refProjectVM.SetBottomPanel_Visibility(True)
      Map.refProjectVM.SetLeftPanel_Visibility(True)
      Map.refLeftPanelVM.UpdateView()
      Map.refProjectVM.SetPartManager_Visibility(False)
      Map.refProjectVM.SetFeatureManager_Visibility(True)
      Map.refProjectVM.SetTopPanel_Visibility(False)
      Map.refProjectVM.SetShowBeamPanel_Visibility(True)
      Map.refProjectVM.SetProjManager_Visibility(True)
      Map.refProjectVM.SetProdManager_Visibility(False)
      Map.refProjectVM.NotifyAllPanelVisibility()
      ' aggiorno le colonne in base al tipo progetto
      If Not IsNothing(Map.refSupervisorManagerVM.CurrProd) Then
         Map.refRawPartListVM.UpdateColumns(Map.refSupervisorManagerVM.CurrProd.nType)
         Map.refPartInRawPartListVM.UpdateColumns(Map.refSupervisorManagerVM.CurrProd.nType)
      Else
         Map.refRawPartListVM.UpdateColumns(BWType.BEAM)
         Map.refPartInRawPartListVM.UpdateColumns(BWType.BEAM)
      End If
      ' aggiorno la visibilità delle colonne
      For Each col In Map.refFeatureInPartInRawPartListVM.FeatureInPartInRawPartColumns
         col.ColumnVisibility = If(col.Visible, Visibility.Visible, Visibility.Collapsed)
      Next
      '' apro progetto proj
      'If Not IsNothing(Map.refProjManagerVM.CurrProj) Then
      '   If Map.refProjManagerVM.CurrProj.bReloadProject Then
      '      Map.refProjManagerVM.OpenProject(Map.refProjManagerVM.CurrProj)
      '      ' aggiorno le colonne in base al tipo progetto
      '      If Not IsNothing(Map.refProjectVM.BTLStructureVM) Then
      '         Map.refRawPartListVM.UpdateColumns(Map.refProjectVM.BTLStructureVM.nPROJTYPE)
      '         Map.refPartInRawPartListVM.UpdateColumns(Map.refProjectVM.BTLStructureVM.nPROJTYPE)
      '      Else
      '         Map.refRawPartListVM.UpdateColumns(BWType.BEAM)
      '         Map.refPartInRawPartListVM.UpdateColumns(BWType.BEAM)
      '      End If
      '      DbControllers.m_ProjController.LockByProjId(Map.refProjManagerVM.CurrProj.nProjId, True)
      '      ' aggiorno titolo
      '      Map.refMainWindowVM.UpdateTitle()
      '      Map.refProjManagerVM.NotifyPropertyChanged(NameOf(Map.refProjManagerVM.MruFileNames))
      '   Else
      '      ' verifico se il prod di provenienza ha piu' proj
      '      If Not IsNothing(Map.refProdManagerVM.CurrProd) AndAlso Map.refProdManagerVM.CurrProd.nProjIdList.Count > 1 Then
      '         ' se si rigenero BTLStructure
      '         Map.refProjectVM.BTLStructureVM = New BTLStructureVM(BTLStructureM.CreateBTLStructure(Map.refProjManagerVM.CurrProj.nProjId))
      '      End If
      '      ' mostro tutti i pezzi
      '      Map.refShowBeamPanelVM.ShowAll()
      '      Map.refProjManagerVM.CurrProj.SetReloadProject(True)
      '   End If
      '   DbControllers.m_ProjController.LockByProjId(Map.refProjManagerVM.CurrProj.nProjId, True)
      'Else
      '   Map.refSceneHostVM.MainController.NewProject()
      '   Map.refProjectVM.BTLStructureVM = Nothing
      '   Map.refProjectVM.MachGroupPanelVM = Nothing
      'End If
      Map.refMainWindowVM.NotifyPropertyChanged(NameOf(Map.refMainWindowVM.nSelTabPage))
      Return True
   End Function

   Private Function ExitSUPERVISOR(bVerifyModification As Boolean) As Boolean
      ' verifico se progetto modificato, e chiedo se salvare
      'If bVerifyModification Then
      '   If Not ProjFileVM.VerifyProjectModification(Map.refProjManagerVM.CurrProj, ProjectType.PROJ) Then
      '      Return False
      '   End If
      'End If
      Map.refProjectVM.SetBottomPanel_Visibility(False)
      Map.refProjectVM.SetLeftPanel_Visibility(True)
      Map.refProjectVM.SetPartManager_Visibility(False)
      Map.refProjectVM.SetFeatureManager_Visibility(False)
      Map.refProjectVM.SetTopPanel_Visibility(True)
      Map.refProjectVM.SetShowBeamPanel_Visibility(False)
      Map.refProjectVM.SetProjManager_Visibility(False)
      Map.refProjectVM.SetProdManager_Visibility(True)
      Return True
   End Function


   Private Function InitCONFIG() As Boolean
      Map.refMainWindowVM.NotifyPropertyChanged(NameOf(Map.refMainWindowVM.nSelTabPage))
      Return True
   End Function

   Private Function ExitCONFIG() As Boolean
      Map.refConfigurationPageVM.VerifyConfigPageModification()
      Return True
   End Function

   Private Function InitINPUTS() As Boolean
      ' fermo e riavvio lettura variabili per includere quelle della pagina input
      MachManaging.AddToCommandList(ThreadCommand.CreateCommand(CommandTypes.STOPREAD_NUMFLEXIUM))
      MachManaging.AddToCommandList(ThreadCommand.CreateCommand(CommandTypes.READ_NUMFLEXIUM))
      Map.refMainWindowVM.NotifyPropertyChanged(NameOf(Map.refMainWindowVM.nSelTabPage))
      Return True
   End Function

   Private Function ExitINPUTS() As Boolean
      ' fermo e riavvio lettura variabili per includere quelle della pagina input
      MachManaging.AddToCommandList(ThreadCommand.CreateCommand(CommandTypes.STOPREAD_NUMFLEXIUM))
      MachManaging.AddToCommandList(ThreadCommand.CreateCommand(CommandTypes.READ_NUMFLEXIUM))
      Return True
   End Function

   Private Function InitOUTPUTS() As Boolean
      ' fermo e riavvio lettura variabili per includere quelle della pagina input
      MachManaging.AddToCommandList(ThreadCommand.CreateCommand(CommandTypes.STOPREAD_NUMFLEXIUM))
      MachManaging.AddToCommandList(ThreadCommand.CreateCommand(CommandTypes.READ_NUMFLEXIUM))
      Map.refMainWindowVM.NotifyPropertyChanged(NameOf(Map.refMainWindowVM.nSelTabPage))
      Return True
   End Function

   Private Function ExitOUTPUTS() As Boolean
      ' fermo e riavvio lettura variabili per includere quelle della pagina input
      MachManaging.AddToCommandList(ThreadCommand.CreateCommand(CommandTypes.STOPREAD_NUMFLEXIUM))
      MachManaging.AddToCommandList(ThreadCommand.CreateCommand(CommandTypes.READ_NUMFLEXIUM))
      Return True
   End Function

#End Region    ' METHODS

#Region "COMMANDS"

#Region "Viewer"

   Public ReadOnly Property Viewer_Command As ICommand
      Get
         If m_cmdViewer Is Nothing Then
            m_cmdViewer = New Command(AddressOf Viewer)
         End If
         Return m_cmdViewer
      End Get
   End Property

   Public Sub Viewer()
      Dim sViewerOptimizerName As String = "EgtBEAMWALL.ViewerOptimizerR32"
      ' recupero processo del supervisore
      Dim localProc As Process() = Process.GetProcessesByName(sViewerOptimizerName)
      If localProc.Length > 0 Then
         For Each p As Process In localProc
            ' porto in primo piano il ViewerOptimizer
            BringWindowToFront(p.MainWindowHandle)
            DbControllers.m_StatusMapController.UpdateAction(DbControllers.m_SupervisorId, Map.refSupervisorManagerVM.CurrProd.nProdId, -Pages.VIEW, StatusMapItemType.Comm, StatusMapOpType.OpenPageInViewOptimRequest, "")
            Exit For
         Next
      Else
         Dim sViewerOptimizerPath As String = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory) & "\" & sViewerOptimizerName & ".exe"
         Try
            Process.Start(sViewerOptimizerPath, "2 " & Pages.VIEW)
         Catch ex As Exception
            EgtOutLog("Error: impossible starting vieweroptimizer from path " & sViewerOptimizerPath)
         End Try
      End If
   End Sub

#End Region ' Viewer

#Region "Optimizer"

   Public ReadOnly Property Optimizer_Command As ICommand
      Get
         If m_cmdOptimizer Is Nothing Then
            m_cmdOptimizer = New Command(AddressOf Optimizer)
         End If
         Return m_cmdOptimizer
      End Get
   End Property

   Public Sub Optimizer()
      Dim sViewerOptimizerName As String = "EgtBEAMWALL.ViewerOptimizerR32"
      ' recupero processo del supervisore
      Dim localProc As Process() = Process.GetProcessesByName(sViewerOptimizerName)
      If localProc.Length > 0 Then
         For Each p As Process In localProc
            ' porto in primo piano il ViewerOptimizer
            BringWindowToFront(p.MainWindowHandle)
            DbControllers.m_StatusMapController.UpdateAction(DbControllers.m_SupervisorId, Map.refSupervisorManagerVM.CurrProd.nProdId, -Pages.MACHINING, StatusMapItemType.Comm, StatusMapOpType.OpenPageInViewOptimRequest, "")
            Exit For
         Next
      Else
         Dim sViewerOptimizerPath As String = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory) & "\" & sViewerOptimizerName & ".exe"
         Try
            Process.Start(sViewerOptimizerPath, "2 " & Pages.MACHINING)
         Catch ex As Exception
            EgtOutLog("Error: impossible starting vieweroptimizer from path " & sViewerOptimizerPath)
         End Try
      End If
   End Sub

#End Region ' Optimizer

#End Region    ' COMMANDS

End Class