Imports EgtBEAMWALL.Core
Imports EgtUILib.EgtInterface
Imports EgtWPFLib5

Public Class FeatureListManagerVM
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

   Public ReadOnly Property ViewPage_Visibility As Visibility
      Get
         Return If(Map.refMainMenuVM.SelPage = Pages.VIEW OrElse Map.refMainMenuVM.SelPage = Pages.ONLYPRODPAGE, Visibility.Visible, Visibility.Collapsed)
      End Get
   End Property

   Public ReadOnly Property PartIsSelected As Boolean
      Get
         Return Not IsNothing(Map.refProjectVM) AndAlso Not IsNothing(Map.refProjectVM.BTLStructureVM) AndAlso Not IsNothing(Map.refProjectVM.BTLStructureVM.SelBTLPart)
      End Get
   End Property

   Public ReadOnly Property FeatureIsSelected As Boolean
      Get
         Return PartIsSelected AndAlso Not IsNothing(Map.refProjectVM.BTLStructureVM.SelBTLPart.SelBTLFeatureVM)
      End Get
   End Property


   Public ReadOnly Property CopyFeature_IsEnabled As Boolean
      Get
         Return Map.refBTLPartManagerVM.BTLPartManager_IsEnabled AndAlso PartIsSelected AndAlso FeatureIsSelected
      End Get
   End Property

   Public ReadOnly Property MacroFeature_IsEnabled As Boolean
      Get
         Return Map.refBTLPartManagerVM.BTLPartManager_IsEnabled AndAlso PartIsSelected AndAlso FeatureIsSelected
      End Get
   End Property

   Public ReadOnly Property AddFeature_IsEnabled As Boolean
      Get
         Return Map.refBTLPartManagerVM.BTLPartManager_IsEnabled AndAlso PartIsSelected AndAlso Not IsNothing(Map.refProjectVM.BTLStructureVM) AndAlso Not Map.refProjectVM.BTLStructureVM.SelBTLPartsCnt > 1
      End Get
   End Property

   Public ReadOnly Property RemoveFeature_IsEnabled As Boolean
      Get
         Return Map.refBTLPartManagerVM.BTLPartManager_IsEnabled AndAlso PartIsSelected AndAlso FeatureIsSelected
      End Get
   End Property


   Private m_cmdCopyFeature As ICommand
   Private m_cmdMacroFeature As ICommand
   Private m_cmdAddFeature As ICommand
   Private m_cmdRemoveFeature As ICommand

#End Region    ' Fields & Properties

#Region "Messages"

   Public ReadOnly Property CopyFeature_ToolTip As String
      Get
         Return EgtMsg(61908)
      End Get
   End Property

   Public ReadOnly Property MacroFeature_ToolTip As String
      Get
         Return EgtMsg(61909)
      End Get
   End Property

   Public ReadOnly Property AddFeature_ToolTip As String
      Get
         Return EgtMsg(61910)
      End Get
   End Property

   Public ReadOnly Property RemoveFeature_ToolTip As String
      Get
         Return EgtMsg(61911)
      End Get
   End Property


#End Region    ' Messages

#Region "CONSTRUCTOR"

   Sub New()
      Map.SetRefFeatureListManagerVM(Me)
   End Sub

#End Region    ' Constructor

#Region "METHODS"

   Friend Sub UpdateView()
      NotifyPropertyChanged(NameOf(ViewPage_Visibility))
   End Sub

   Friend Sub SetLeftPanelIsEnabled(bIsEnabled As Boolean)
      NotifyPropertyChanged(NameOf(CopyFeature_IsEnabled))
      NotifyPropertyChanged(NameOf(AddFeature_IsEnabled))
      NotifyPropertyChanged(NameOf(RemoveFeature_IsEnabled))
      NotifyPropertyChanged(NameOf(MacroFeature_IsEnabled))
   End Sub

   Friend Sub PartShowAllChanged()
      NotifyPropertyChanged(NameOf(AddFeature_IsEnabled))
   End Sub

   Friend Sub FeatureSelectionChanged()
      NotifyPropertyChanged(NameOf(CopyFeature_IsEnabled))
      NotifyPropertyChanged(NameOf(MacroFeature_IsEnabled))
      NotifyPropertyChanged(NameOf(RemoveFeature_IsEnabled))
   End Sub

   Private Sub CreateNewFeature(MacroListCount As Integer, nSelMacro As Integer, AddFeatureWndVM As AddFeatureWndVM, nNewFeatureId As Integer, bMacroFlag As Boolean, NewFeat As BTLFeatureM)
      ' verifico se una Macro è selezionata
      If MacroListCount > 0 AndAlso nSelMacro >= 0 Then
         ' Creo nuova feature sulla base della Macro selezionata
         ' Creo table e setto variabili
         CreateMacroTable()
         Dim bOk As Boolean
         If AddFeatureWndVM.nSelMacroDefault >= 0 Then
            bOk = EgtLuaExecFile(AddFeatureWndVM.m_MacroCustomFilePathList(nSelMacro))
         Else
            bOk = EgtLuaExecFile(AddFeatureWndVM.m_MacroFilePathList(nSelMacro))
         End If
         ' Leggo variabili
         EgtLuaGetGlobIntVar("MACRO.FEATUREID", nNewFeatureId)
         ' Reset lua
         EgtLuaResetGlobVar("MACRO")
         If Not bOk Then
            'MessageBox.Show(EgtMsg(61869), EgtMsg(30007))
            If bOnlyProd Then
               EgtBEAMWALL.Core.OnlyProdEgtMessageBoxV.Show(Application.Current.MainWindow, EgtMsg(61869), EgtMsg(30007))
            Else
               EgtBEAMWALL.Core.EgtMessageBoxV.Show(Application.Current.MainWindow, EgtMsg(61869), EgtMsg(30007))
            End If
            Return
         End If
         bMacroFlag = True
      Else
         ' Creo nuova feature (Macro non selezionata)
         NewFeat = BTLFeatureM.CreateBTLFeature(AddFeatureWndVM.nSelPRC.nPRC, AddFeatureWndVM.nSelPRC.nGRP, AddFeatureWndVM.nSelPRC.nSIDE)
         NewFeat.SetDefaultValues()
         Dim vPar() As Double = Nothing
         Dim sPar As String = String.Empty
         Dim vParQ() As String = Nothing
         NewFeat.CalcParamArray(vPar, sPar, vParQ)
         If NewFeat.nPRC = 900 Then NewFeat.sDES = "2"
         ' aggiorno la feature con nuovo valore
         EgtBeamSetPart(Map.refProjectVM.BTLStructureVM.SelBTLPart.nPartId)
         nNewFeatureId = EgtBeamAddProcess(NewFeat.nSelGRP, NewFeat.nPRC, NewFeat.nSelSIDE, NewFeat.sDES,
                                           Map.refProjectVM.BTLStructureVM.SelBTLPart.BTLPartM.NewProcId(),
                                           New Frame3d(), vPar, sPar, vParQ, GDB_ID.NULL, GDB_ID.NULL)
         bMacroFlag = False
      End If
      ' se è stata creata
      If nNewFeatureId <> GDB_ID.NULL Then
         MyMachGroupPanelM.SetDuploModified(Map.refProjectVM.BTLStructureVM.SelBTLPart.nPartId)
         ' la aggiungo a struttura BTL corrente
         NewFeat = BTLFeatureM.CreateBTLFeature(Map.refProjectVM.BTLStructureVM.SelBTLPart.BTLPartM, nNewFeatureId)
         If Not bMacroFlag Then NewFeat.SetDefaultValues()
         Map.refProjectVM.BTLStructureVM.SelBTLPart.BTLPartM.AddBTLFeature(NewFeat)
         Map.refProjectVM.BTLStructureVM.SelBTLPart.SelBTLFeatureVM = Map.refProjectVM.BTLStructureVM.SelBTLPart.BTLFeatureVMList.FirstOrDefault(Function(x) x.BTLFeatureM Is NewFeat)
         Map.refProjectVM.BTLStructureVM.SelBTLPart.ResetCalcPart()
      End If
   End Sub

   Private Sub CreateMacroTable()
      Dim nMacroW, nMacroH As Integer
      If Not IsNothing(Map.refProjectVM.BTLStructureVM) AndAlso Not IsNothing(Map.refProjectVM.BTLStructureVM.SelBTLPart) AndAlso Not IsNothing(Map.refProjectVM.BTLStructureVM.SelBTLPart.m_SelBTLFeatureVM) Then
         If Map.refProjectVM.BTLStructureVM.SelBTLPart.nROTATED = 0 OrElse Map.refProjectVM.BTLStructureVM.SelBTLPart.nROTATED = 180 Then
            If Map.refProjectVM.BTLStructureVM.SelBTLPart.m_SelBTLFeatureVM.nSelSIDE = 1 OrElse Map.refProjectVM.BTLStructureVM.SelBTLPart.m_SelBTLFeatureVM.nSelSIDE = 3 Then
               nMacroW = Map.refProjectVM.BTLStructureVM.SelBTLPart.dBtlW
               nMacroH = Map.refProjectVM.BTLStructureVM.SelBTLPart.dBtlH
            Else
               nMacroW = Map.refProjectVM.BTLStructureVM.SelBTLPart.dBtlH
               nMacroH = Map.refProjectVM.BTLStructureVM.SelBTLPart.dBtlW
            End If
         ElseIf Map.refProjectVM.BTLStructureVM.SelBTLPart.nROTATED = 90 OrElse Map.refProjectVM.BTLStructureVM.SelBTLPart.nROTATED = 270 Then
            If Map.refProjectVM.BTLStructureVM.SelBTLPart.m_SelBTLFeatureVM.nSelSIDE = 1 OrElse Map.refProjectVM.BTLStructureVM.SelBTLPart.m_SelBTLFeatureVM.nSelSIDE = 3 Then
               nMacroW = Map.refProjectVM.BTLStructureVM.SelBTLPart.dBtlH
               nMacroH = Map.refProjectVM.BTLStructureVM.SelBTLPart.dBtlW
            Else
               nMacroW = Map.refProjectVM.BTLStructureVM.SelBTLPart.dBtlW
               nMacroH = Map.refProjectVM.BTLStructureVM.SelBTLPart.dBtlH
            End If
         Else
            nMacroW = Map.refProjectVM.BTLStructureVM.SelBTLPart.dBtlW
            nMacroH = Map.refProjectVM.BTLStructureVM.SelBTLPart.dBtlH
         End If
      End If

      EgtLuaCreateGlobTable("MACRO")
      EgtLuaSetGlobNumVar("MACRO.L", Map.refProjectVM.BTLStructureVM.SelBTLPart.dBtlL)
      EgtLuaSetGlobNumVar("MACRO.W", nMacroW)
      EgtLuaSetGlobNumVar("MACRO.H", nMacroH)
      EgtLuaSetGlobIntVar("MACRO.PROCID", Map.refProjectVM.BTLStructureVM.SelBTLPart.BTLPartM.NewProcId())
   End Sub

#End Region    ' Methods

#Region "COMMANDS"

#Region "CopyFeature"

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

   ''' <summary>
   ''' Execute the Exec.  This method is invoked by the ExecCommand.
   ''' </summary>
   Public Sub CopyFeature()
      If IsNothing(ProjectManagerVM.CurrProj) Then Return
      Dim SelPart As BTLPartVM = Map.refProjectVM.BTLStructureVM.SelBTLPart
      If IsNothing(SelPart) Then Return
      Dim SelFeature As BTLFeatureVM = SelPart.SelBTLFeatureVM
      If IsNothing(SelFeature) Then Return
      ' creo copia
      Dim NewFeature = SelFeature.Copy()
      If Not IsNothing(NewFeature) Then
         SelPart.SelBTLFeatureVM = Map.refProjectVM.BTLStructureVM.SelBTLPart.BTLFeatureVMList.FirstOrDefault(Function(x) x.BTLFeatureM Is NewFeature)
         MyMachGroupPanelM.SetDuploModified(SelPart.nPartId)
      End If
      SelPart.ResetCalcPart()
   End Sub

#End Region    ' CopyFeature

#Region "MacroFeature"

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

   ''' <summary>
   ''' Execute the Exec.  This method is invoked by the ExecCommand.
   ''' </summary>
   Public Sub MacroFeature()
      If IsNothing(ProjectManagerVM.CurrProj) Then Return
      If IsNothing(Map.refProjectVM.BTLStructureVM.SelBTLPart) Then Return
      ' apro finestra di creazione macro
      Dim MacroFeatureWndVM As New MacroFeatureWndVM()
      If bOnlyProd Then
         Dim NewMacroFeatureWnd As New NewMacroFeatureWnd(Application.Current.MainWindow, MacroFeatureWndVM)
         If NewMacroFeatureWnd.ShowDialog() Then
         End If
      Else
         Dim MacroFeatureWnd As New MacroFeatureWndV(Application.Current.MainWindow, MacroFeatureWndVM)
         If MacroFeatureWnd.ShowDialog() Then
         End If
      End If
   End Sub

#End Region    ' MacroFeature

#Region "AddFeature"

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

   ''' <summary>
   ''' Execute the Exec.  This method is invoked by the ExecCommand.
   ''' </summary>
   Public Sub AddFeature(ByVal param As Object)
      If IsNothing(ProjectManagerVM.CurrProj) Then Return
      If IsNothing(Map.refProjectVM.BTLStructureVM.SelBTLPart) Then Return
      ' apro finestra di definizione nuova feature
      Dim AddFeatureWndVM As New AddFeatureWndVM()
      Dim NewFeat As BTLFeatureM = Nothing
      Dim nNewFeatureId As Integer = 0
      Dim bMacroFlag As Boolean = False
      If bOnlyProd Then
         Dim AddNewFeatureWnd As New NewAddFeatureWndV(Application.Current.MainWindow, AddFeatureWndVM)
         If AddNewFeatureWnd.ShowDialog() Then
            ' verifico se una Macro è selezionata
            If AddFeatureWndVM.nSelMacroDefault >= 0 Then
               CreateNewFeature(AddFeatureWndVM.MacroDefaultList.Count, AddFeatureWndVM.nSelMacroDefault, AddFeatureWndVM, nNewFeatureId, bMacroFlag, NewFeat)
            Else
               CreateNewFeature(AddFeatureWndVM.MacroCustomList.Count, AddFeatureWndVM.nSelMacroCustom, AddFeatureWndVM, nNewFeatureId, bMacroFlag, NewFeat)
            End If
         End If
      Else
         Dim AddFeatureWnd As New AddFeatureWndV(Application.Current.MainWindow, AddFeatureWndVM)
         If AddFeatureWnd.ShowDialog() Then
            ' verifico se una Macro è selezionata
            CreateNewFeature(AddFeatureWndVM.MacroCustomList.Count, AddFeatureWndVM.nSelMacroCustom, AddFeatureWndVM, nNewFeatureId, bMacroFlag, NewFeat)
         End If
      End If
   End Sub

#End Region    ' AddFeature

#Region "RemoveFeature"

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

   ''' <summary>
   ''' Execute the Exec.  This method is invoked by the ExecCommand.
   ''' </summary>
   Public Sub RemoveFeature()
      If IsNothing(ProjectManagerVM.CurrProj) Then Return
      If IsNothing(Map.refProjectVM.BTLStructureVM.SelBTLPart) Then Return
      If IsNothing(Map.refProjectVM.BTLStructureVM.SelBTLPart.SelBTLFeatureVM) Then Return
      If EgtBeamEraseProcess(Map.refProjectVM.BTLStructureVM.SelBTLPart.SelBTLFeatureVM.nFeatureId) Then
         MyMachGroupPanelM.SetDuploModified(Map.refProjectVM.BTLStructureVM.SelBTLPart.nPartId)
         ' rimuovo dalla lista feature
         Dim Index As Integer = Map.refProjectVM.BTLStructureVM.SelBTLPart.BTLFeatureVMList.IndexOf(Map.refProjectVM.BTLStructureVM.SelBTLPart.SelBTLFeatureVM)
         If Index = 0 Then
            If Map.refProjectVM.BTLStructureVM.SelBTLPart.BTLFeatureVMList.Count > 0 Then
               Map.refProjectVM.BTLStructureVM.SelBTLPart.SelBTLFeatureVM = Map.refProjectVM.BTLStructureVM.SelBTLPart.BTLFeatureVMList(0)
            Else
               Map.refProjectVM.BTLStructureVM.SelBTLPart.SelBTLFeatureVM = Nothing
            End If
         ElseIf Index = Map.refProjectVM.BTLStructureVM.SelBTLPart.BTLFeatureVMList.Count - 1 Then
            If Map.refProjectVM.BTLStructureVM.SelBTLPart.BTLFeatureVMList.Count > 1 Then
               Map.refProjectVM.BTLStructureVM.SelBTLPart.SelBTLFeatureVM = Map.refProjectVM.BTLStructureVM.SelBTLPart.BTLFeatureVMList(Map.refProjectVM.BTLStructureVM.SelBTLPart.BTLFeatureVMList.Count - 2)
            Else
               Map.refProjectVM.BTLStructureVM.SelBTLPart.SelBTLFeatureVM = Nothing
            End If
         Else
            Map.refProjectVM.BTLStructureVM.SelBTLPart.SelBTLFeatureVM = Map.refProjectVM.BTLStructureVM.SelBTLPart.BTLFeatureVMList(Index - 1)
         End If
         Map.refProjectVM.BTLStructureVM.SelBTLPart.BTLFeatureVMList.RemoveAt(Index)
         EgtDraw()
         Map.refProjectVM.BTLStructureVM.SelBTLPart.ResetCalcPart()
      End If
   End Sub

#End Region    ' RemoveFeature

#End Region    ' Commands

End Class
