Imports System.Collections.ObjectModel
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Windows.Threading
Imports EgtBEAMWALL.Core
Imports EgtBEAMWALL.Core.ConstBeam
Imports EgtBEAMWALL.DataLayer.DatabaseModels
Imports EgtUILib
Imports EgtWPFLib5

Public Class MainMenuVM
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

   Private m_OpenPage_Timer As New DispatcherTimer
   Private m_bOpenPage As Boolean = False
   Private m_nPageToOpen As Pages = Pages.MACHINING

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

   Private m_SendFeedbackIsEnabled As Boolean = True
   Public ReadOnly Property SendFeedbackIsEnabled As Boolean
      Get
         Return m_SelPage = Pages.VIEW Or m_SelPage = Pages.MACHINING
      End Get
   End Property

   Public Property View_IsChecked As Boolean
      Get
         Return m_SelPage = Pages.VIEW
      End Get
      Set(value As Boolean)
         If value Then
            If Not IsNothing(Map.refProjManagerVM.CurrProj) AndAlso Not IsNothing(Map.refProdManagerVM.CurrProd) AndAlso
               Map.refProjManagerVM.CurrProj.nProdId = Map.refProdManagerVM.CurrProd.nProdId Then
               Map.refProjManagerVM.CurrProj.SetReloadProject(False)
            End If
            SelPage = Pages.VIEW
         End If
      End Set
   End Property

   Public Property Machining_IsChecked As Boolean
      Get
         Return m_SelPage = Pages.MACHINING
      End Get
      Set(value As Boolean)
         If value Then
            If Not IsNothing(Map.refProjManagerVM.CurrProj) AndAlso Not IsNothing(Map.refProdManagerVM.CurrProd) AndAlso
               Map.refProjManagerVM.CurrProj.nProdId = Map.refProdManagerVM.CurrProd.nProdId Then
               Map.refProdManagerVM.CurrProd.SetReloadProject(False)
            End If
            SelPage = Pages.MACHINING
         End If
      End Set
   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

   Private m_Supervisor_Visibility As Visibility
   Public ReadOnly Property Supervisor_Visibility As Visibility
      Get
         Return m_Supervisor_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.VIEW
            bOk = ExitVIEW(bVerifyModification)
         Case Pages.MACHINING
            bOk = ExitMACHINING(bVerifyModification)
         Case Pages.CONFIG
            bOk = ExitCONFIG()
      End Select
      If bOk Then
         ' Entro nel nuovo stato
         m_SelPage = Page
         Select Case m_SelPage
            Case Pages.VIEW
               ' Map.refMachGroupPanelVM.SetMachGroupState(False)
               InitVIEW()
               'Map.refTopCommandBarVM.IsEnabled = False
            Case Pages.MACHINING
               ' Map.refMachGroupPanelVM.SetMachGroupState(True)
               InitMACHINING()
               'Map.refTopCommandBarVM.IsEnabled = True
            Case Pages.CONFIG
               InitCONFIG()
               'Map.refMachGroupPanelVM.SetMachGroupState(False)
               'Map.refCONFIGTabVM.InitCONFIGation()
               'Map.refTopCommandBarVM.IsEnabled = False
         End Select
      End If
      ' aggiorno visualizzazione RadioButton
      NotifyPropertyChanged("View_IsChecked")
      NotifyPropertyChanged("Machining_IsChecked")
      NotifyPropertyChanged("Supervisor_IsChecked")
      NotifyPropertyChanged("Config_IsChecked")
      Map.refProjectVM.NotifyPropertyChanged("SelItem")
   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 Configuration_Msg As String
      Get
         Return EgtMsg(61832)
      End Get
   End Property

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

#Region "ToolTip"

   'Proprietà ToolTip
   Public ReadOnly Property SendFeedbackToolTip As String
      Get
         Return EgtMsg(MSG_TOPCOMMANDBAR + 13)
      End Get
   End Property

#End Region ' ToolTip

#End Region    ' Messages

   ' Definizione comandi
   Private m_cmdSupervisor As ICommand
   Private m_cmdSendFeedback As ICommand

#End Region    ' Fields & Properties

#Region "CONSTRUCTOR"

   Sub New()
      ' Creo riferimento a questa classe in EgtCAM5Map
      Map.SetRefMainMenuVM(Me)
      ' abilito supervisore
      If Map.refMainWindowVM.MainWindowM.GetKeyOption(KEY_OPT.SUPERVISOR) Then
         m_Supervisor_Visibility = Visibility.Visible
      Else
         m_Supervisor_Visibility = Visibility.Collapsed
      End If
      ' imposto timer di apertura da ottimizzatore
      m_OpenPage_Timer.Interval = TimeSpan.FromMilliseconds(500)
      AddHandler m_OpenPage_Timer.Tick, AddressOf OpenPage_Tick
      m_OpenPage_Timer.Start()
   End Sub

#End Region    ' CONSTRUCTOR

#Region "METHODS"

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

   Private Function InitVIEW() As Boolean
      Map.refProjectVM.SetBottomPanel_Visibility(True)
      Map.refProjectVM.SetLeftPanel_Visibility(True)
      Map.refLeftPanelVM.UpdateView()
      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.SetOptimizePanel_Visibility(Not IsNothing(Map.refProjectVM.BTLStructureVM) AndAlso Map.refProjectVM.BTLStructureVM.nPROJTYPE = BWType.WALL AndAlso Map.refMainWindowVM.MainWindowM.GetKeyOption(KEY_OPT.NESTING_AUTO))
      Map.refProjectVM.NotifyAllPanelVisibility()
      Map.refCALCPanelVM.SetChooseMachineBtn_Visibility(Map.refMainMenuVM.SelPage = Pages.VIEW AndAlso Map.refMachinePanelVM.MachineList.Count > 1 AndAlso GetMainPrivateProfileInt(S_MACH, K_CHANGEMACH, 0) = 1)
      Map.refStatisticsTimePanelVM.SetStatisticsTimePanel_Visibility()
      If Not IsNothing(Map.refFeatureListVM.colFeature_Do) Then
         Map.refFeatureListVM.colFeature_Do.ColumnVisibility = Visibility.Visible
      End If
      ' aggiorno la visibilità delle colonne
      For Each col In Map.refFeatureListVM.FeatureColumns
         col.ColumnVisibility = If(col.Visible, Visibility.Visible, Visibility.Collapsed)
      Next
      For Each col In Map.refPartListVM.PartColumns
         col.ColumnVisibility = If(col.Visible, Visibility.Visible, Visibility.Collapsed)
      Next
      For Each col In Map.refStatisticsVM.StatisticsColumns
         col.ColumnVisibility = If(col.Visible, Visibility.Visible, Visibility.Collapsed)
      Next
      For Each col In Map.refPParameterListVM.PParameterListColumns
         col.ColumnVisibility = If(col.Visible, Visibility.Visible, Visibility.Collapsed)
      Next
      For Each col In Map.refQParameterListVM.QParameterListColumns
         col.ColumnVisibility = If(col.Visible, Visibility.Visible, Visibility.Collapsed)
      Next
      ' imposto dimensioni colonne/righe della Grid
      DimensionsIniFile.ReadGridDimensions(ConstDims.PROJECT_VIEW, Map.refProjectVM.GridDims)
      DimensionsIniFile.ReadGridDimensions(ConstDims.LEFTPANEL_VIEW, Map.refLeftPanelVM.GridDims)
      ' apro progetto proj
      If Not IsNothing(Map.refProjManagerVM.CurrProj) Then
         If Map.refProjManagerVM.CurrProj.bReloadProject OrElse Map.refConfigurationPageVM.QParametersModified Then
            LoadingWndHelper.UpdateLoadingWnd(ActiveIds.GOTOPROJ, 2, EgtMsg(63005), 10, 100)        ' Loading parts
            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.refFeatureInPartInRawPartListVM.UpdateColumns(CurrentMachine.nType)
               Map.refTopPanelVM.ManageQParamsRowVisibility(Map.refProjectVM.BTLStructureVM.nPROJTYPE <> BWType.BEAM)
               Map.refPartInRawPartListVM.UpdateColumns(Map.refProjectVM.BTLStructureVM.nPROJTYPE)
            Else
               Map.refRawPartListVM.UpdateColumns(BWType.BEAM)
               Map.refFeatureInPartInRawPartListVM.UpdateColumns(CurrentMachine.nType)
               Map.refTopPanelVM.ManageQParamsRowVisibility(BWType.BEAM)
               Map.refPartInRawPartListVM.UpdateColumns(BWType.BEAM)
            End If
            ' resetto modifica parametri Q default
            Map.refConfigurationPageVM.ResetQParametersModified()
            DbControllers.m_ProjController.LockByProjId(Map.refProjManagerVM.CurrProj.nProjId, True, Map.refMainWindowVM.MainWindowM.GetKeyNumber())
            Map.refProjManagerVM.NotifyPropertyChanged(NameOf(Map.refProjManagerVM.MruFileNames))
         Else
            LoadingWndHelper.UpdateLoadingWnd(ActiveIds.GOTOPROJ, 2, EgtMsg(63005), 10, 100)        ' Loading parts
            ' recupero indice di modifica progetto
            Dim CommIndex As Integer = -1
            Dim ActiveSessionList As List(Of StatusMapModel) = DbControllers.m_StatusMapController.GetProd(m_SupervisorId)
            For Each ActiveSession In ActiveSessionList
               If ActiveSession.ItemId = Map.refProjManagerVM.CurrProj.nProdId Then
                  CommIndex = ActiveSession.Index
               End If
            Next
            LoadingWndHelper.UpdateLoadingWnd(ActiveIds.GOTOPROD, 3, EgtMsg(63002), 30, 100)       ' Loading machining groups
            ' 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))
               ' carico filtri di ricerca
               Map.refProjectVM.BTLStructureVM.LoadFilters()
            End If
            ' fisso indice sessione di comunicazione 
            If CommIndex > -1 Then
               Map.refProjManagerVM.CurrProj.SetModificationIndex(CommIndex)
            End If
            ' mostro tutti i pezzi
            Map.refShowBeamPanelVM.ShowAll(True)
            Map.refProjManagerVM.CurrProj.SetReloadProject(True)
         End If
         ' aggiorno titolo
         Map.refMainWindowVM.UpdateTitle()
         DbControllers.m_ProjController.LockByProjId(Map.refProjManagerVM.CurrProj.nProjId, True, Map.refMainWindowVM.MainWindowM.GetKeyNumber())
      Else
         Map.refSceneHostVM.MainController.NewProject()
         Map.refProjectVM.BTLStructureVM = Nothing
         Map.refProjectVM.MachGroupPanelVM = Nothing
      End If
      Map.refMainWindowVM.NotifyPropertyChanged(NameOf(Map.refMainWindowVM.nSelTabPage))
      Map.refMainMenuVM.NotifyPropertyChanged(NameOf(Map.refMainMenuVM.SendFeedbackIsEnabled))
      Map.refInstrumentPanelVM.NotifyPropertyChanged(NameOf(Map.refInstrumentPanelVM.ChangeParam_Visibility))
      Map.refInstrumentPanelVM.NotifyPropertyChanged(NameOf(Map.refInstrumentPanelVM.ChangeMaterial_Visibility))
      Return True
   End Function

   Private Function ExitVIEW(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
      ' resetto eventuale visualizzazione statistiche
      Map.refInstrumentPanelVM.ResetStatisticsIsChecked()
      Map.refProjectVM.SetBottomPanel_Visibility(False)
      Map.refProjectVM.SetLeftPanel_Visibility(True)
      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)
      Map.refProjectVM.SetOptimizePanel_Visibility(True)
      Map.refCALCPanelVM.SetChooseMachine_Visibility(False)
      Return True
   End Function

   Private Function InitMACHINING() As Boolean
      ' imposto dimensioni colonne/righe della Grid
      DimensionsIniFile.ReadGridDimensions(ConstDims.PROJECT_OPTIMIZER, Map.refProjectVM.GridDims)
      DimensionsIniFile.ReadGridDimensions(ConstDims.LEFTPANEL_OPTIMIZER, Map.refLeftPanelVM.GridDims)
      ' inizializzo gruppi di lavorazione
      If Not IsNothing(Map.refProdManagerVM.CurrProd) AndAlso Not IsNothing(Map.refProdManagerVM.CurrProd.nProdId) AndAlso Map.refProdManagerVM.CurrProd.nProdId > 0 Then
         If File.Exists(Map.refProdManagerVM.CurrProd.sProdPath) Then
            ' apro progetto
            If Map.refProdManagerVM.CurrProd.bReloadProject OrElse Map.refConfigurationPageVM.QParametersModified Then
               LoadingWndHelper.UpdateLoadingWnd(ActiveIds.GOTOPROD, 2, EgtMsg(63005), 10, 50)        ' Loading parts
               Map.refProdManagerVM.TempCurrProd = Map.refProdManagerVM.CurrProd
               If Map.refSceneHostVM.MainController.OpenProject(Map.refProdManagerVM.CurrProd.sProdPath, False) Then
                  ' aggiorno le colonne in base al tipo progetto
                  Map.refRawPartListVM.UpdateColumns(Map.refProjectVM.BTLStructureVM.nPROJTYPE)
                  Map.refFeatureInPartInRawPartListVM.UpdateColumns(CurrentMachine.nType)
                  Map.refTopPanelVM.ManageQParamsRowVisibility(Map.refProjectVM.BTLStructureVM.nPROJTYPE <> BWType.BEAM)
                  Map.refPartInRawPartListVM.UpdateColumns(Map.refProjectVM.BTLStructureVM.nPROJTYPE)
                  ' resetto modifica parametri Q default
                  Map.refConfigurationPageVM.ResetQParametersModified()
                  DbControllers.m_ProdController.LockByProdId(Map.refProdManagerVM.CurrProd.nProdId, True, Map.refMainWindowVM.MainWindowM.GetKeyNumber())
                  Map.refProdManagerVM.NotifyPropertyChanged(NameOf(Map.refProdManagerVM.MruFileNames))
               End If
            Else
               LoadingWndHelper.UpdateLoadingWnd(ActiveIds.GOTOPROD, 2, EgtMsg(63005), 10, 30)        ' Loading parts
               ' mostro tutti i pezzi
               Map.refShowBeamPanelVM.ShowAll(False)
               ' verifico se il prod ha piu' proj
               If Map.refProdManagerVM.CurrProd.nProjIdList.Count > 1 Then
                  ' se si rigenero BTLStructure
                  Map.refProjectVM.BTLStructureVM = New BTLStructureVM(BTLStructureM.CreateBTLStructure(0))
                  ' carico filtri di ricerca
                  Map.refProjectVM.BTLStructureVM.LoadFilters()
               End If
               ' recupero indice di modifica progetto
               Dim CommIndex As Integer = -1
               Dim ActiveSessionList As List(Of StatusMapModel) = DbControllers.m_StatusMapController.GetProd(m_SupervisorId)
               For Each ActiveSession In ActiveSessionList
                  If ActiveSession.ItemId = Map.refProdManagerVM.CurrProd.nProdId Then
                     CommIndex = ActiveSession.Index
                  End If
               Next
               LoadingWndHelper.UpdateLoadingWnd(ActiveIds.GOTOPROD, 3, EgtMsg(63002), 30, 100)       ' Loading machining groups
               ' carico lista dei MachGroup
               Map.refProjectVM.MachGroupPanelVM = New MyMachGroupPanelVM(MyMachGroupPanelM.CreateMyMachGroupPanel(Map.refMachinePanelVM.MachineList.ToList()))
               ' fisso indice sessione di comunicazione 
               If CommIndex > -1 Then
                  Map.refProdManagerVM.CurrProd.SetModificationIndex(CommIndex)
               End If
               Map.refProdManagerVM.CurrProd.SetReloadProject(True)
            End If
            'DbControllers.m_ProdController.ResetController()
            ' se Warehouse di tipo Medium e piu' di un progetto collegato
            Dim nDefault As Integer = 2
            If GetMainPrivateProfileInt(S_WAREHOUSE, EgtBEAMWALL.Core.ConstIni.K_TYPE, nDefault) = WarehouseType.MEDIUM AndAlso Map.refProdManagerVM.CurrProd.nProjIdList.Count > 1 Then
               ' confronto le Sezioni del BTL importato con quelle in Warehouse
               WarehouseWndVM.UpdateSectionXMaterial()
            End If
            ' aggiorno titolo
            Map.refMainWindowVM.UpdateTitle()
            DbControllers.m_ProdController.LockByProdId(Map.refProdManagerVM.CurrProd.nProdId, True, Map.refMainWindowVM.MainWindowM.GetKeyNumber())
            ' controllo se devo lanciare verifica perche' importato
            Dim bGetVerifyResult As Boolean = False
            Dim bVerifyMach As Boolean = False
            Dim nBTLInfoLayerId As Integer = EgtGetFirstNameInGroup(GDB_ID.ROOT, BTLINFO)
            While nBTLInfoLayerId <> GDB_ID.NULL
               If EgtGetInfo(nBTLInfoLayerId, IMP_VERIFYMACHGROUP, bGetVerifyResult) AndAlso bGetVerifyResult Then
                  If Not bVerifyMach Then bVerifyMach = True
                  EgtSetInfo(nBTLInfoLayerId, IMP_VERIFYMACHGROUP, False)
               End If
               nBTLInfoLayerId = EgtGetNextName(nBTLInfoLayerId, BTLINFO)
            End While
            If bVerifyMach Then
               Map.refCALCPanelVM.VerifyAll()
            End If
            ' seleziono prima barra
            Map.refProjectVM.MachGroupPanelVM.SelFirstMachGroup()
         Else
            MessageBox.Show(EgtMsg(61871))
         End If
      Else
         Map.refSceneHostVM.MainController.NewProject()
         Map.refProjectVM.BTLStructureVM = Nothing
         Map.refProjectVM.MachGroupPanelVM = Nothing
      End If
      ' aggiorno lista possibili nesting
      Map.refOptimizePanelVM.UpdateOriginTypeList(EgtGetFirstNameInGroup(GDB_ID.ROOT, "RawParts") <> GDB_ID.NULL)
      ' aggiorno visibilita' degli elementi grafici
      Map.refMainWindowVM.NotifyPropertyChanged(NameOf(Map.refMainWindowVM.nSelTabPage))
      Map.refMainMenuVM.NotifyPropertyChanged(NameOf(Map.refMainMenuVM.SendFeedbackIsEnabled))
      Map.refInstrumentPanelVM.NotifyPropertyChanged(NameOf(Map.refInstrumentPanelVM.ChangeParam_Visibility))
      Map.refInstrumentPanelVM.NotifyPropertyChanged(NameOf(Map.refInstrumentPanelVM.ChangeMaterial_Visibility))
      Map.refTopPanelVM.NotifyPropertyChanged(NameOf(Map.refTopPanelVM.MovePart_Visibility))
      Map.refProjectVM.SetBottomPanel_Visibility(False)
      Map.refProjectVM.SetLeftPanel_Visibility(True)
      Map.refLeftPanelVM.UpdateView()
      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)
      Map.refProjectVM.NotifyAllPanelVisibility()
      Map.refCALCPanelVM.SetChooseMachineBtn_Visibility(False)
      Map.refTopPanelVM.UpdateQParameterVisibility()
      Map.refTopPanelVM.UpdateMovePartInRawPartVisibility()
      Map.refStatisticsTimePanelVM.SetStatisticsTimePanel_Visibility()
      ' aggiorno la visibilità delle colonne
      For Each col In Map.refFeatureListVM.FeatureColumns
         col.ColumnVisibility = If(col.Visible, Visibility.Visible, Visibility.Collapsed)
      Next
      For Each col In Map.refRawPartListVM.RawPartColumns
         col.ColumnVisibility = If(col.Visible, Visibility.Visible, Visibility.Collapsed)
      Next
      For Each col In Map.refFeatureInPartInRawPartListVM.FeatureInPartInRawPartColumns
         col.ColumnVisibility = If(col.Visible, Visibility.Visible, Visibility.Collapsed)
      Next
      For Each col In Map.refPartInRawPartListVM.PartInRawPartColumns
         col.ColumnVisibility = If(col.Visible, Visibility.Visible, Visibility.Collapsed)
      Next
      For Each col In Map.refStatisticsVM.OptimizerStatisticsColumns
         col.ColumnVisibility = If(col.Visible, Visibility.Visible, Visibility.Collapsed)
      Next
      If Not IsNothing(Map.refFeatureListVM.colFeature_Do) Then
         Map.refFeatureListVM.colFeature_Do.ColumnVisibility = Visibility.Collapsed
      End If
      Map.refProjectVM.SetOptimizePanel_Visibility(True)
      Return True
   End Function

   Private Function ExitMACHINING(bVerifyModification As Boolean) As Boolean
      ' verifico se progetto modificato, e chiedo se salvare
      If bVerifyModification Then
         If ProdFileVM.VerifyProjectModification(Map.refProdManagerVM.CurrProd) = MessageBoxResult.Cancel Then
            Return False
         End If
      End If
      ' resetto eventuale visualizzazione statistiche
      Map.refInstrumentPanelVM.ResetStatisticsIsChecked()
      Map.refProjectVM.SetBottomPanel_Visibility(True)
      Map.refProjectVM.SetLeftPanel_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.SetOptimizePanel_Visibility(Not IsNothing(Map.refProjectVM.BTLStructureVM) AndAlso Map.refProjectVM.BTLStructureVM.nPROJTYPE = BWType.WALL AndAlso Map.refMainWindowVM.MainWindowM.GetKeyOption(KEY_OPT.NESTING_AUTO))
      Map.refCALCPanelVM.SetChooseMachine_Visibility(False)
      EgtResetCurrMachGroup()
      Return True
   End Function

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

   Private Function ExitCONFIG() As Boolean
      Map.refConfigurationPageVM.VerifyConfigPageModification()
      ' resetto flag inserimento password
      Map.refConfigurationPageVM.bModifyMachParam = False
      If Not IsNothing(Map.refProjectVM.BTLStructureVM) Then
         Map.refRawPartListVM.UpdateColumns(Map.refProjectVM.BTLStructureVM.nPROJTYPE)
         Map.refFeatureInPartInRawPartListVM.UpdateColumns(CurrentMachine.nType)
         Map.refTopPanelVM.ManageQParamsRowVisibility(Map.refProjectVM.BTLStructureVM.nPROJTYPE <> BWType.BEAM)
         Map.refPartInRawPartListVM.UpdateColumns(Map.refProjectVM.BTLStructureVM.nPROJTYPE)
      Else
         Map.refRawPartListVM.UpdateColumns(BWType.BEAM)
         Map.refFeatureInPartInRawPartListVM.UpdateColumns(CurrentMachine.nType)
         Map.refTopPanelVM.ManageQParamsRowVisibility(BWType.BEAM)
         Map.refPartInRawPartListVM.UpdateColumns(BWType.BEAM)
      End If
      Return True
   End Function

   Public Sub OpenPageFromSupervisor(Page As Pages)
      If m_SelPage <> Page Then
         m_nPageToOpen = Page
         m_bOpenPage = True
      End If
   End Sub

   Public Sub OpenPage_Tick()
      If Not m_bOpenPage Then Return
      m_bOpenPage = False
      ' apro la pagina
      SelPage = m_nPageToOpen
      m_nPageToOpen = Pages.MACHINING
   End Sub

#End Region    ' METHODS

#Region "COMMANDS"

#Region "Supervisor"

   Public ReadOnly Property Supervisor_Command As ICommand
      Get
         If m_cmdSupervisor Is Nothing Then
            m_cmdSupervisor = New Command(AddressOf Supervisor)
         End If
         Return m_cmdSupervisor
      End Get
   End Property

   Public Sub Supervisor()
      Dim sSupervisorName As String = "EgtBEAMWALL.SupervisorR32"
      ' recupero processo del supervisore
      Dim localProc As Process() = Process.GetProcessesByName(sSupervisorName)
      If localProc.Length > 0 Then
         For Each p As Process In localProc
            ' porto in primo piano il Supervisor
            BringWindowToFront(p.MainWindowHandle)
            Exit For
         Next
      Else
         Dim sSupervisorPath As String = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory) & "\" & sSupervisorName & ".exe"
         Try
            Process.Start(sSupervisorPath)
         Catch ex As Exception
            EgtOutLog("Error: impossible starting supervisor from path " & sSupervisorPath)
         End Try
      End If
   End Sub

#End Region ' Supervisor

#Region "SendFeedbackCommand"

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

   ''' <summary>
   ''' Execute the SendFeedback.  This method is invoked by the SendFeedbackCommand.
   ''' </summary>
   Public Sub SendFeedback(ByVal param As Object)
      If SelPage = Pages.MACHINING AndAlso IsNothing(Map.refProdManagerVM.CurrProd) Then
         MessageBox.Show(EgtMsg(61891), EgtMsg(MSG_MESSAGEBOX + 1), MessageBoxButton.OK, MessageBoxImage.Error)
         Return
      End If
      ' Recupero indirizzo a cui spedire la mail
      Dim sSupportAddress As String = String.Empty
      GetMainPrivateProfileString(S_GENERAL, K_SUPPORT, "support@egaltech.com", sSupportAddress)
      ' se vuoto do messaggio di errore ed esco
      If String.IsNullOrWhiteSpace(sSupportAddress) Then
         MessageBox.Show(EgtMsg(MSG_TOPCOMMANDBAR + 10), EgtMsg(MSG_MESSAGEBOX + 1), MessageBoxButton.OK, MessageBoxImage.Error)
         Return
      End If
      ' Recupero numero chiave
      Dim sKey As String = String.Empty
      EgtGetKeyInfo(sKey)
      '' Recupero file del progetto corrente
      'Dim sCurrProject As String = String.Empty
      'EgtGetCurrFilePath(sCurrProject)
      '' se nome file vuoto, chiedo se si vuole salvare
      'If String.IsNullOrWhiteSpace(sCurrProject) Then
      '   If MessageBox.Show(EgtMsg(MSG_TOPCOMMANDBAR + 11), "", MessageBoxButton.YesNo, MessageBoxImage.Question) = MessageBoxResult.Yes Then
      '      Map.refSceneHostVM.SaveProject()
      '   End If
      '   EgtGetCurrFilePath(sCurrProject)
      '   ' se modificato, chiedo se si vuole salvare
      'Else
      '   If EgtGetModified() Then
      '      If MessageBox.Show(EgtMsg(MSG_TOPCOMMANDBAR + 11), "", MessageBoxButton.YesNo, MessageBoxImage.Question) = MessageBoxResult.Yes Then
      '         Map.refSceneHostVM.SaveProject()
      '      End If
      '   End If
      'End If
      ' Esporto il progetto
      Dim ProjFileMList As List(Of ProjFileM)
      If SelPage = Pages.MACHINING And Not IsNothing(Map.refProdManagerVM.CurrProd) Then
         ProjFileMList = DbControllers.m_ProjController.GetByProdAsc(Map.refProdManagerVM.CurrProd.nProdId)
         Map.refProjManagerVM.SetCurrProj(ProjFileMList(0).nProjId)
      End If
      Dim sExportFileName = Map.refProjManagerVM.CurrProj.nProjId.ToString("0000") & " - " & Map.refProjManagerVM.CurrProj.sBTLFileName & " - ProjectExport"
      Dim sExpZipToCreate = Map.refProjManagerVM.ExportProject(sExportFileName)
      ' Creo zip file da allegare
      Dim sZipToCreate As String = Map.refMainWindowVM.MainWindowM.sTempDir & "\Feedback.zip"
      If File.Exists(sZipToCreate) Then
         File.Delete(sZipToCreate)
      End If
      Try
         Using zip As New Ionic.Zip.ZipFile(sZipToCreate, Console.Out)
            ' aggiungo progetto esportato
            If File.Exists(sExpZipToCreate) Then
               zip.AddItem(sExpZipToCreate, "")
            End If
            ' aggiungo Prod e Proj associati
            Dim ProdId As Integer
            Select Case Map.refMainMenuVM.SelPage
               Case Pages.VIEW
                  ProdId = Map.refProjManagerVM.CurrProj.nProdId
               Case Pages.MACHINING
                  ProdId = Map.refProdManagerVM.CurrProd.nProdId
            End Select
            If ProdId = 0 Then
               zip.AddItem(Map.refProjManagerVM.CurrProj.sProjDirPath, "Projs\" & Map.refProjManagerVM.CurrProj.nProjId.ToString("0000"))
            Else
               ProjFileMList = DbControllers.m_ProjController.GetByProdAsc(ProdId)
               ' se piu' di uno
               If IsNothing(ProjFileMList) Then
                  Return
               ElseIf ProjFileMList.Count > 0 Then
                  Dim ProjFileVMList As New List(Of ProjectFileVM)
                  For Each Project In ProjFileMList
                     ProjFileVMList.Add(New ProjFileVM(Project))
                  Next
                  For Each ProjFileVMItem In ProjFileVMList
                     zip.AddItem(ProjFileVMItem.sProjDirPath, "Projs\" & ProjFileVMItem.nProjId.ToString("0000"))
                  Next
                  zip.AddItem(ProjFileVMList(0).sProdDirPath, "Prods\" & ProjFileVMList(0).nProdId.ToString("0000"))
               End If
            End If
            ' aggiungo cartella macchina del progetto 
            Dim sMachineDir As String = Map.refMainWindowVM.MainWindowM.sMachinesRoot & "\" & sMachineName
            If Directory.Exists(sMachineDir) Then
               zip.AddItem(sMachineDir, sMachineName)
            End If
            ' aggiungo file log
            If File.Exists(Map.refMainWindowVM.MainWindowM.sLogFile) Then
               zip.AddItem(Map.refMainWindowVM.MainWindowM.sLogFile, "")
            End If
            ' aggiungo file parametri P e Q
            If File.Exists(BTLIniFile.m_sBTLIniFile) Then
               zip.AddItem(BTLIniFile.m_sBTLIniFile, "")
            End If
            ' salvo lo zip
            zip.Save()
         End Using
      Catch ex1 As Exception
         EgtOutLog("Exception in zip: " & ex1.ToString())
      End Try
      ' preparo la mail per il supporto
      Dim bEx As Boolean = False
      Try
         Dim sAddressArray As String() = sSupportAddress.Split(CType(",", Char()))
         Dim SendFeedbackWindow As New EgtWPFLib5.MapiMailMessage("EgtBEAMWALL Feedback - " & sKey)
         SendFeedbackWindow.Recipients.Add(sAddressArray(0))
         For index As Integer = 1 To sAddressArray.Length() - 1
            SendFeedbackWindow.Recipients.Add(sAddressArray(index), EgtWPFLib5.MapiMailMessage.RecipientType.CC)
         Next
         If Not String.IsNullOrWhiteSpace(sZipToCreate) AndAlso File.Exists(sZipToCreate) Then
            SendFeedbackWindow.Files.Add(Map.refMainWindowVM.MainWindowM.sTempDir & "\Feedback.zip")
         End If
         SendFeedbackWindow.ShowDialog()
      Catch ex As Exception
         EgtOutLog("Feedback exception: " & ex.ToString)
         bEx = True
      End Try
      If bEx OrElse EgtWPFLib5.MapiMailMessage.m_ErrorCode <> 0 Then
         MessageBox.Show(String.Format(EgtMsg(MSG_TOPCOMMANDBAR + 12), sSupportAddress, sZipToCreate), EgtMsg(MSG_MESSAGEBOX + 3), MessageBoxButton.OK, MessageBoxImage.Information)
      Else
         Map.refMyStatusBarVM.SetOutputMessage(EgtMsg(MSG_TOPCOMMANDBAR + 14))
      End If
   End Sub

#End Region ' SendFeedbackCommand

#Region "TEST"

#End Region     ' TEST

#End Region    ' COMMANDS

End Class