Imports System.Windows.Threading
Imports EgtBEAMWALL.Core
Imports EgtWPFLib5
Imports EgtUILib
Imports System.Collections.ObjectModel
Imports EgtBEAMWALL.DataLayer.Controllers

Public Class ProjectVM
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

   Private m_Calc_Timer As New DispatcherTimer

   Friend Event OnPreControllerExec(sFilePath As String)
   Friend Event OnPostControllerExec()

   ' Flag per non salvare Script appena eseguito in elenco MruScript
   Private m_bScriptInMru As Boolean = True

   Private Property m_GridDims As New ObservableCollection(Of GridDimension)
   Public Property GridDims As ObservableCollection(Of GridDimension)
      Get
         Return m_GridDims
      End Get
      Set
         m_GridDims = Value
         NotifyPropertyChanged(NameOf(GridDims))
      End Set
   End Property

   Private m_BTLStructureVM As BTLStructureVM
   Public Property BTLStructureVM As BTLStructureVM
      Get
         Return m_BTLStructureVM
      End Get
      Set(value As BTLStructureVM)
         m_BTLStructureVM = value
         NotifyPropertyChanged(NameOf(BTLStructureVM))
      End Set
   End Property

   Private m_MachGroupPanelVM As MyMachGroupPanelVM
   Public Property MachGroupPanelVM As MyMachGroupPanelVM
      Get
         Return m_MachGroupPanelVM
      End Get
      Set(value As MyMachGroupPanelVM)
         m_MachGroupPanelVM = value
         NotifyPropertyChanged(NameOf(MachGroupPanelVM))
      End Set
   End Property

   Private m_bCalcRunning As Boolean = False
   Public ReadOnly Property bCalcRunning As Boolean
      Get
         Return m_bCalcRunning
      End Get
   End Property
   Private m_bLockUX As Boolean = False

   Private m_LeftPanel_Visibility As Boolean = True
   Public Property LeftPanel_Visibility As Visibility
      Get
         Return If(m_LeftPanel_Visibility, Visibility.Visible, Visibility.Collapsed)
      End Get
      Set(value As Visibility)
         m_LeftPanel_Visibility = (value = Visibility.Visible)
      End Set
   End Property
   Friend Sub SetLeftPanel_Visibility(IsVisible As Boolean)
      m_LeftPanel_Visibility = IsVisible
   End Sub

   Private m_TopPanel_Visibility As Boolean = False
   Public Property TopPanel_Visibility As Visibility
      Get
         Return If(m_TopPanel_Visibility, Visibility.Visible, Visibility.Collapsed)
      End Get
      Set(value As Visibility)
         m_TopPanel_Visibility = (value = Visibility.Visible)
      End Set
   End Property
   Friend Sub SetTopPanel_Visibility(IsVisible As Boolean)
      m_TopPanel_Visibility = IsVisible
   End Sub

   Private m_BottomPanel_Visibility As Boolean = True
   Public Property BottomPanel_Visibility As Visibility
      Get
         Return If(m_BottomPanel_Visibility, Visibility.Visible, Visibility.Collapsed)
      End Get
      Set(value As Visibility)
         m_BottomPanel_Visibility = (value = Visibility.Visible)
      End Set
   End Property
   Friend Sub SetBottomPanel_Visibility(IsVisible As Boolean)
      m_BottomPanel_Visibility = IsVisible
   End Sub

   Private m_PartManager_Visibility As Boolean = True
   Public ReadOnly Property PartManager_Visibility As Visibility
      Get
         Return If(Map.refMainMenuVM.SelPage = Pages.VIEW AndAlso Not IsNothing(Map.refProjectVM.BTLStructureVM) AndAlso Map.refProjectVM.BTLStructureVM.SelectionType = BTLStructureVM.SelectionTypes.SELECT_ AndAlso Not Map.refFreeContourManagerVM.bIsActive, Visibility.Visible, Visibility.Collapsed)
      End Get
   End Property

   Private m_FeatureManager_Visibility As Boolean = True
   Public Property FeatureManager_Visibility As Visibility
      Get
         Return If(m_FeatureManager_Visibility, Visibility.Visible, Visibility.Collapsed)
      End Get
      Set(value As Visibility)
         m_FeatureManager_Visibility = (value = Visibility.Visible)
      End Set
   End Property
   Friend Sub SetFeatureManager_Visibility(IsVisible As Boolean)
      m_FeatureManager_Visibility = IsVisible
   End Sub

   Private m_ShowBeamPanel_Visibility As Boolean = True
   Public Property ShowBeamPanel_Visibility As Visibility
      Get
         Return If(m_ShowBeamPanel_Visibility, Visibility.Visible, Visibility.Collapsed)
      End Get
      Set(value As Visibility)
         m_ShowBeamPanel_Visibility = (value = Visibility.Visible)
      End Set
   End Property
   Friend Sub SetShowBeamPanel_Visibility(IsVisible As Boolean)
      m_ShowBeamPanel_Visibility = IsVisible
   End Sub

   Private m_ProjManager_Visibility As Boolean = True
   Public Property ProjManager_Visibility As Visibility
      Get
         Return If(m_ProjManager_Visibility, Visibility.Visible, Visibility.Collapsed)
      End Get
      Set(value As Visibility)
         m_ProjManager_Visibility = (value = Visibility.Visible)
      End Set
   End Property
   Friend Sub SetProjManager_Visibility(IsVisible As Boolean)
      m_ProjManager_Visibility = IsVisible
   End Sub

   Private m_ProdManager_Visibility As Boolean = True
   Public Property ProdManager_Visibility As Visibility
      Get
         Return If(m_ProdManager_Visibility, Visibility.Visible, Visibility.Collapsed)
      End Get
      Set(value As Visibility)
         m_ProdManager_Visibility = (value = Visibility.Visible)
      End Set
   End Property
   Friend Sub SetProdManager_Visibility(IsVisible As Boolean)
      m_ProdManager_Visibility = IsVisible
   End Sub

   Private m_OptimizePanel_Visibility As Boolean = True
   Public Property OptimizePanel_Visibility As Visibility
      Get
         Return If(m_OptimizePanel_Visibility, Visibility.Visible, Visibility.Collapsed)
      End Get
      Set(value As Visibility)
         m_OptimizePanel_Visibility = (value = Visibility.Visible)
      End Set
   End Property
   Friend Sub SetOptimizePanel_Visibility(IsVisible As Boolean)
      m_OptimizePanel_Visibility = IsVisible
      NotifyPropertyChanged(NameOf(OptimizePanel_Visibility))
      Map.refOptimizePanelVM.NotifyPropertyChanged(NameOf(Map.refOptimizePanelVM.CalcRotFlip_Visibility))
      Map.refOptimizePanelVM.NotifyPropertyChanged(NameOf(Map.refOptimizePanelVM.Optimize_Visibility))
      Map.refOptimizePanelVM.NotifyPropertyChanged(NameOf(Map.refOptimizePanelVM.OriginType_Visibility))
      Map.refOptimizePanelVM.NotifyPropertyChanged(NameOf(Map.refOptimizePanelVM.ViewPage_Visibility))
      Map.refOptimizePanelVM.NotifyPropertyChanged(NameOf(Map.refOptimizePanelVM.MachiningPage_Visibility))
      Map.refOptimizePanelVM.NotifyPropertyChanged(NameOf(Map.refOptimizePanelVM.NestingOption_Visibility))
   End Sub

   Friend Sub NotifyCalcPanel_Visibility()
      Map.refCALCPanelVM.NotifyPropertyChanged(NameOf(Map.refCALCPanelVM.ViewPage_Visibility))
      Map.refCALCPanelVM.NotifyPropertyChanged(NameOf(Map.refCALCPanelVM.Edit_Visibility))
   End Sub

   Private m_FreeContourManager_Visibility As Boolean = False
   Public Property FreeContourManager_Visibility As Visibility
      Get
         Return If(m_FreeContourManager_Visibility, Visibility.Visible, Visibility.Collapsed)
      End Get
      Set(value As Visibility)
         m_FreeContourManager_Visibility = (value = Visibility.Visible)
      End Set
   End Property
   Friend Sub SetFreeContourManager_Visibility(IsVisible As Boolean)
      m_FreeContourManager_Visibility = IsVisible
      NotifyPropertyChanged("FreeContourManager_Visibility")
   End Sub

   Public ReadOnly Property SpecialPanel_Visibility As Visibility
      Get
         Return If(Map.refMainWindowVM.MainWindowM.bSpecialPanel AndAlso Map.refMainWindowVM.MainWindowM.nUserLevel > 5, Visibility.Visible, Visibility.Collapsed)
      End Get
   End Property


   Friend Sub NotifyAllPanelVisibility()
      NotifyPropertyChanged("LeftPanel_Visibility")
      NotifyPropertyChanged("TopPanel_Visibility")
      NotifyPropertyChanged("BottomPanel_Visibility")
      NotifyPropertyChanged("PartManager_Visibility")
      NotifyPropertyChanged("FeatureManager_Visibility")
      NotifyPropertyChanged("ShowBeamPanel_Visibility")
      NotifyPropertyChanged("ProjManager_Visibility")
      NotifyPropertyChanged("ProdManager_Visibility")
      NotifyCalcPanel_Visibility()
   End Sub

#End Region    ' FIELDS & PROPERTIES

#Region "CONSTRUCTOR"

   Sub New()
      ' Creo riferimento a questa classe in Map
      Map.SetRefProjectVM(Me)
      ' imposto timer per aggiornamenti Calc
      m_Calc_Timer.Interval = TimeSpan.FromMilliseconds(500)
      AddHandler m_Calc_Timer.Tick, AddressOf Calc_Timer_Tick
      ' imposto dimensioni colonne/righe della Grid
      DimensionsIniFile.ReadGridDimensions(ConstDims.PROJECT_VIEW, GridDims)
   End Sub

#End Region    ' CONSTRUCTOR

#Region "METHODS"

   Friend Sub SetCalcRunning(value As Boolean)
      If value Then m_Calc_Timer.Start()
      m_bCalcRunning = value
   End Sub

   Friend Sub ManageIsEnabled(bIsEnabled As Boolean)
      Map.refInstrumentPanelVM.SetInstrumentPanelIsEnabled(bIsEnabled)
      Map.refCALCPanelVM.SetCalcPanelIsEnabled(bIsEnabled)
      Map.refMainMenuVM.SetMainMenuIsEnabled(bIsEnabled)
      Map.refPartManagerVM.SetPartManagerIsEnabled(bIsEnabled)
      Map.refLeftPanelVM.SetLeftPanelIsEnabled(bIsEnabled)
      Map.refProjManagerVM.SetProjManagerIsEnabled(bIsEnabled)
      Map.refProdManagerVM.SetProdManagerIsEnabled(bIsEnabled)
      Map.refTopPanelVM.SetTopPanelIsEnabled(bIsEnabled)
      Map.refOptimizePanelVM.SetOptimizePanelIsEnabled(bIsEnabled)
      Map.refFeatureManagerVM.SetFeatureManagerIsEnabled(bIsEnabled)
   End Sub

   Private Sub Calc_Timer_Tick()
      If m_bCalcRunning <> m_bLockUX Then
         ' se calcolo iniziato
         If m_bCalcRunning Then
            ManageIsEnabled(False)
            ' aggiungere gestione colonne editabili delle tabelle
            If Not IsNothing(Map.refPartListVM.colPart_Do) Then
               Map.refPartListVM.colPart_Do.IsReadOnly = True
            End If
            If Not IsNothing(Map.refFeatureListVM.colFeature_Do) Then
               Map.refFeatureListVM.colFeature_Do.IsReadOnly = True
            End If
            If Not IsNothing(Map.refPParameterListVM.colPParam_Value) Then
               Map.refPParameterListVM.colPParam_Value.IsReadOnly = True
            End If
            If Not IsNothing(Map.refQParameterListVM.colQParam_Value) Then
               Map.refQParameterListVM.colQParam_Value.IsReadOnly = True
            End If
            If Not IsNothing(Map.refQParameterListVM.colQParam_Custom) Then
               Map.refQParameterListVM.colQParam_Custom.IsReadOnly = True
            End If
            If Not IsNothing(Map.refRawPartListVM.colRawPart_StartCut) Then
               Map.refRawPartListVM.colRawPart_StartCut.IsReadOnly = True
            End If
            If Not IsNothing(Map.refRawPartListVM.colRawPart_W) Then
               Map.refRawPartListVM.colRawPart_W.IsReadOnly = True
            End If
            If Not IsNothing(Map.refRawPartListVM.colRawPart_L) Then
               Map.refRawPartListVM.colRawPart_L.IsReadOnly = True
            End If
            If Not IsNothing(Map.refRawPartListVM.colRawPart_PosZ) Then
               Map.refRawPartListVM.colRawPart_PosZ.IsReadOnly = True
            End If
            If Not IsNothing(Map.refPartInRawPartListVM.colPartInRawPart_Offset) Then
               Map.refPartInRawPartListVM.colPartInRawPart_Offset.IsReadOnly = True
            End If
            If Not IsNothing(Map.refPartInRawPartListVM.colPartInRawPart_Rot) Then
               Map.refPartInRawPartListVM.colPartInRawPart_Rot.IsReadOnly = True
            End If
            If Not IsNothing(Map.refPartInRawPartListVM.colPartInRawPart_Flip) Then
               Map.refPartInRawPartListVM.colPartInRawPart_Flip.IsReadOnly = True
            End If
            If Not IsNothing(Map.refPartInRawPartListVM.colPartInRawPart_PosX) Then
               Map.refPartInRawPartListVM.colPartInRawPart_PosX.IsReadOnly = True
            End If
            If Not IsNothing(Map.refPartInRawPartListVM.colPartInRawPart_PosY) Then
               Map.refPartInRawPartListVM.colPartInRawPart_PosY.IsReadOnly = True
            End If
            If Not IsNothing(Map.refFeatureInPartInRawPartListVM.colFeatureInPartInRawPart_Do) Then
               Map.refFeatureInPartInRawPartListVM.colFeatureInPartInRawPart_Do.IsReadOnly = True
            End If
            m_bLockUX = True
            ' se calcolo finito
         Else
            ManageIsEnabled(True)
            ' aggiungere gestione colonne editabili delle tabelle
            If Not IsNothing(Map.refPartListVM.colPart_Do) Then
               Map.refPartListVM.colPart_Do.ResetToOrigIsReadOnly()
            End If
            If Not IsNothing(Map.refFeatureListVM.colFeature_Do) Then
               Map.refFeatureListVM.colFeature_Do.ResetToOrigIsReadOnly()
            End If
            If Not IsNothing(Map.refPParameterListVM.colPParam_Value) Then
               Map.refPParameterListVM.colPParam_Value.ResetToOrigIsReadOnly()
            End If
            If Not IsNothing(Map.refQParameterListVM.colQParam_Value) Then
               Map.refQParameterListVM.colQParam_Value.ResetToOrigIsReadOnly()
            End If
            If Not IsNothing(Map.refQParameterListVM.colQParam_Custom) Then
               Map.refQParameterListVM.colQParam_Custom.ResetToOrigIsReadOnly()
            End If
            If Not IsNothing(Map.refRawPartListVM.colRawPart_StartCut) Then
               Map.refRawPartListVM.colRawPart_StartCut.ResetToOrigIsReadOnly()
            End If
            If m_BTLStructureVM.nPROJTYPE <> BWType.BEAM AndAlso Not IsNothing(Map.refRawPartListVM.colRawPart_W) Then
               Map.refRawPartListVM.colRawPart_W.ResetToOrigIsReadOnly()
            End If
            If Not IsNothing(Map.refRawPartListVM.colRawPart_L) Then
               Map.refRawPartListVM.colRawPart_L.ResetToOrigIsReadOnly()
            End If
            If Not IsNothing(Map.refRawPartListVM.colRawPart_PosZ) Then
               Map.refRawPartListVM.colRawPart_PosZ.ResetToOrigIsReadOnly()
            End If
            If Not IsNothing(Map.refPartInRawPartListVM.colPartInRawPart_Offset) Then
               Map.refPartInRawPartListVM.colPartInRawPart_Offset.ResetToOrigIsReadOnly()
            End If
            If Not IsNothing(Map.refPartInRawPartListVM.colPartInRawPart_Rot) Then
               Map.refPartInRawPartListVM.colPartInRawPart_Rot.ResetToOrigIsReadOnly()
            End If
            If Not IsNothing(Map.refPartInRawPartListVM.colPartInRawPart_Flip) Then
               Map.refPartInRawPartListVM.colPartInRawPart_Flip.ResetToOrigIsReadOnly()
            End If
            If Not IsNothing(Map.refPartInRawPartListVM.colPartInRawPart_PosX) Then
               Map.refPartInRawPartListVM.colPartInRawPart_PosX.ResetToOrigIsReadOnly()
            End If
            If Not IsNothing(Map.refPartInRawPartListVM.colPartInRawPart_PosY) Then
               Map.refPartInRawPartListVM.colPartInRawPart_PosY.ResetToOrigIsReadOnly()
            End If
            If Not IsNothing(Map.refFeatureInPartInRawPartListVM.colFeatureInPartInRawPart_Do) Then
               Map.refFeatureInPartInRawPartListVM.colFeatureInPartInRawPart_Do.ResetToOrigIsReadOnly()
            End If
            ' fermo il timer
            If Not m_bCalcRunning Then
               m_Calc_Timer.Stop()
               m_bLockUX = False
            End If
         End If
      End If

   End Sub

#End Region    ' METHODS

End Class

