Imports System.Data.Entity.ModelConfiguration.Configuration.Properties
Imports EgtBEAMWALL.Core
Imports EgtUILib
Imports EgtWPFLib5

Public Class RawPartManagerVM
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

   Private m_RawPartManager_IsEnabled As Boolean = True
   Public ReadOnly Property RawPartManagerIsEnabled As Boolean
      Get
         Return m_RawPartManager_IsEnabled
      End Get
   End Property
   Friend Sub SetRawPartManagerIsEnabled(bIsEnabled As Boolean)
      m_RawPartManager_IsEnabled = bIsEnabled
      NotifyPropertyChanged(NameOf(RawPartManagerIsEnabled))
   End Sub

   Private m_ProduceRawPart_Visibility As Visibility
   Public ReadOnly Property ProduceRawPart_Visibility As Visibility
      Get
         Return m_ProduceRawPart_Visibility
      End Get
   End Property

   Private m_MovePartInRawPart_Visibility As Visibility
   Public ReadOnly Property MovePartInRawPart_Visibility As Visibility
      Get
         Return m_MovePartInRawPart_Visibility
      End Get
   End Property

   ' Definizione comandi
   Private m_cmdProduceAllRawPart As ICommand
   Private m_cmdProduceRawPart As ICommand
   Private m_cmdCopyRawPart As ICommand
   Private m_cmdRemoveRawPart As ICommand
   Private m_cmdRemoveAllRawPart As ICommand
   Private m_cmdMovePartInRawPart As ICommand

#End Region    ' Fields & Properties

#Region "Messages"

   Public ReadOnly Property ProduceRawPart_ToolTip As String
      Get
         Return EgtMsg(61912)
      End Get
   End Property

   Public ReadOnly Property ProduceAllRawPart_ToolTip As String
      Get
         Return EgtMsg(61913)
      End Get
   End Property

   Public ReadOnly Property CopyRawPart_ToolTip As String
      Get
         Return EgtMsg(61914)
      End Get
   End Property

   Public ReadOnly Property RemoveRawPart_ToolTip As String
      Get
         Return EgtMsg(61915)
      End Get
   End Property

   Public ReadOnly Property RemoveAllRawPart_ToolTip As String
      Get
         Return EgtMsg(61976)
      End Get
   End Property

   Public ReadOnly Property ReOrderPart_ToolTip As String
      Get
         Return EgtMsg(61916)
      End Get
   End Property

   Public ReadOnly Property RemovePart_ToolTip As String
      Get
         Return EgtMsg(61919)
      End Get
   End Property

   Public ReadOnly Property MovePartInRawPart_ToolTip As String
      Get
         Return EgtMsg(61944)
      End Get
   End Property

#End Region    ' Messages

#Region "CONSTRUCTORS"

   Sub New()
      Map.SetRefRawPartManagerVM(Me)
      ' mostro bottoni produci solo se supervisore attivo
      m_ProduceRawPart_Visibility = If(Map.refMainWindowVM.MainWindowM.GetKeyOption(KEY_OPT.SUPERVISOR), Visibility.Visible, Visibility.Collapsed)
   End Sub

#End Region    ' Constructors

#Region "METHODS"

   Friend Sub UpdateMovePartInRawPartVisibility()
      m_MovePartInRawPart_Visibility = If(Map.refMainWindowVM.MainWindowM.nUserLevel > 5 AndAlso Not IsNothing(ProjectManagerVM.CurrProd) AndAlso
         ProjectManagerVM.CurrProd.nType = BWType.WALL, Visibility.Visible, Visibility.Collapsed)
      NotifyPropertyChanged(NameOf(MovePartInRawPart_Visibility))
   End Sub

#End Region    ' Methods

#Region "COMMANDS"

#Region "ProduceRawPart"

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

   ''' <summary>
   ''' Execute the Exec.  This method is invoked by the ExecCommand.
   ''' </summary>
   Public Sub ProduceRawPart()
      If IsNothing(ProjectManagerVM.CurrProd) Then Return
      Dim ErrorMsg As String = ""
      ' se attiva opzione e non ancora calcolato
      If GetMainPrivateProfileInt(S_GENERAL, K_FASTPRODUCE, 0) = 1 And Map.refMachGroupPanelVM.SelectedMachGroup.nGlobalState = CalcStates.NOTCALCULATED Then
         Map.refCALCPanelVM.SetFromProduce(CALCPanelVM.ProduceType.PRODUCE)
         Map.refCALCPanelVM.Verify()
         Return
      End If
      If Not Produce(Map.refMachGroupPanelVM.SelectedMachGroup, ErrorMsg) AndAlso Not String.IsNullOrWhiteSpace(ErrorMsg) Then
         'MessageBox.Show(ErrorMsg)
         If bOnlyProd Then
            EgtBEAMWALL.Core.OnlyProdEgtMessageBoxV.Show(Application.Current.MainWindow, ErrorMsg)
         Else
            EgtBEAMWALL.Core.EgtMessageBoxV.Show(Application.Current.MainWindow, ErrorMsg)
         End If
      End If
   End Sub

   Private Function Produce(MachGroup As MyMachGroupVM, ByRef ErrorMsg As String) As Boolean
      If IsNothing(MachGroup) Then Return False
      ' se barra gia' assegnata a supervisore, esco
      If MachGroup.nProduction_State >= ItemState.Assigned Then Return False
      If MachGroup.nGlobalState = CalcStates.OK Or MachGroup.nGlobalState = CalcStates.INFO Then
         ' se ci sono modifiche, salvo il pogetto
         If EgtGetModified() AndAlso Map.refMainMenuVM.SelPage = Pages.MACHINING Then
            Map.refProdManagerVM.Save()
         ElseIf EgtGetModified() AndAlso Map.refMainMenuVM.SelPage = Pages.ONLYPRODPAGE Then
            Map.refOnlyProdManagerVM.Save()
         End If
         ' mando al supervisore
         DbControllers.m_MachGroupController.UpdateOrder(ProjectManagerVM.CurrProd.nProdId, MachGroup.Id, DbControllers.m_MachGroupController.GetMinIndex(ProjectManagerVM.CurrProd.nProdId) + 1)
         DbControllers.m_MachGroupController.UpdateSupervisor(ProjectManagerVM.CurrProd.nProdId, MachGroup.Id, DbControllers.m_SupervisorId)
         MachGroup.SentToSupervisor()
         For Each Part As PartVM In MachGroup.PartVMList
            DbControllers.m_PartController.UpdateStatus(ProjectManagerVM.CurrProd.nProdId, MachGroup.Id, Part.nPartId, ItemState.Assigned)
            Part.nProduction_State = ItemState.Assigned
            Part.NotifyPropertyChanged(NameOf(Part.Background))
            ' disabilito impostazione modificato
            Dim DisableMgr As New DisableModifiedMgr
            ' blocco modifica del pezzo
            EgtDuploSetLocked(Part.nPartId)
            ' ripristino precedente impostazione modificato
            DisableMgr.ReEnable()
         Next
         Return True
      Else
         ErrorMsg = String.Format(EgtMsg(62568), MachGroup.Name)
         Return False
      End If
   End Function

#End Region    ' ProduceRawPart

#Region "ProduceAllRawPart"

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

   ''' <summary>
   ''' Execute the Exec.  This method is invoked by the ExecCommand.
   ''' </summary>
   Public Sub ProduceAllRawPart()
      If IsNothing(ProjectManagerVM.CurrProd) Then Return
      ' se attiva opzione e non ancora calcolato
      If GetMainPrivateProfileInt(S_GENERAL, K_FASTPRODUCE, 0) = 1 And Map.refMachGroupPanelVM.MachGroupVMList.Any(Function(x As MyMachGroupVM) x.nProduction_State = ItemState.ND AndAlso x.nGlobalState = CalcStates.NOTCALCULATED) Then
         Map.refCALCPanelVM.SetFromProduce(CALCPanelVM.ProduceType.PRODUCEALL)
         Map.refCALCPanelVM.VerifyAll()
         Return
      End If
      If Map.refMachGroupPanelVM.MachGroupVMList.Count > 0 Then
         Dim MachgroupErrorList As New List(Of MachGroupVM)
         For Each Machgroup In Map.refMachGroupPanelVM.MachGroupVMList
            Dim ErrorMsg As String = ""
            If Not Produce(Machgroup, ErrorMsg) AndAlso Not String.IsNullOrWhiteSpace(ErrorMsg) Then
               MachgroupErrorList.Add(Machgroup)
            End If
         Next
         If MachgroupErrorList.Count > 0 Then
            Dim sMachGroups As String = ""
            For Each MachGroup In MachgroupErrorList
               sMachGroups &= " " & MachGroup.Name & ","
            Next
            sMachGroups = sMachGroups.Trim({" "c, ","c})
            'MessageBox.Show(String.Format("Impossible sending {0} to supervisor because not machinable!", sMachGroups))
            If bOnlyProd Then
               EgtBEAMWALL.Core.OnlyProdEgtMessageBoxV.Show(Application.Current.MainWindow, String.Format(EgtMsg(62568), sMachGroups))
            Else
               EgtBEAMWALL.Core.EgtMessageBoxV.Show(Application.Current.MainWindow, String.Format(EgtMsg(62568), sMachGroups))
            End If
         End If
      End If
   End Sub

#End Region    ' ProduceAllRawPart

#Region "CopyRawPart"

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

   ''' <summary>
   ''' Execute the Exec.  This method is invoked by the ExecCommand.
   ''' </summary>
   Public Sub CopyRawPart()
      If IsNothing(ProjectManagerVM.CurrProd) Then Return
      Dim nQty As Integer = 1
      ' se premuto shift
      If (Keyboard.Modifiers And ModifierKeys.Shift) = ModifierKeys.Shift Then
         Dim MultiCopyRawPartWndVM As New MultiCopyRawPartWndVM()
         Dim MultiCopyRawPartWnd As New MultiCopyRawPartWndV(Application.Current.MainWindow, MultiCopyRawPartWndVM)
         If Not MultiCopyRawPartWnd.ShowDialog() Then Return
         nQty = MultiCopyRawPartWndVM.nQuantity
      End If
      Dim SelMachGroup As MyMachGroupVM = Map.refMachGroupPanelVM.SelectedMachGroup
      If IsNothing(SelMachGroup) Then Return
      ' creo copia
      Dim NewMachGroup As MyMachGroupVM = SelMachGroup.Copy(nQty)
      If Not IsNothing(NewMachGroup) Then
         ' lo seleziono
         Map.refProjectVM.MachGroupPanelVM.SelectedMachGroup = NewMachGroup
      End If
   End Sub

#End Region    ' CopyRawPart

#Region "RemoveRawPart"

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

   ''' <summary>
   ''' Execute the Exec.  This method is invoked by the ExecCommand.
   ''' </summary>
   Public Sub RemoveRawPartCmd()
      RemoveRawPart()
   End Sub

   Public Sub RemoveRawPart(Optional bAll As Boolean = False)
      If IsNothing(ProjectManagerVM.CurrProd) Then Return
      ' se tutti
      If bAll Then
         ' cancello tutti
         For Index = Map.refMachGroupPanelVM.MachGroupVMList.Count - 1 To 0 Step -1
            Dim CurrMachGroup As MyMachGroupVM = Map.refMachGroupPanelVM.MachGroupVMList(Index)
            ' se barra gia' assegnata a supervisore, esco
            If CurrMachGroup.nProduction_State >= ItemState.Assigned Then Continue For
            CurrMachGroup.DeleteMachGroup(True)
         Next
      Else
         Dim SelMachGroup As MyMachGroupVM = Map.refMachGroupPanelVM.SelectedMachGroup
         If IsNothing(SelMachGroup) Then Return
         ' se barra gia' assegnata a supervisore, esco
         If SelMachGroup.nProduction_State >= ItemState.Assigned Then Return
         SelMachGroup.DeleteMachGroup()
         If Not IsNothing(Map.refMachGroupPanelVM.SelectedMachGroup) Then Map.refMachGroupPanelVM.SelectedMachGroup = Map.refMachGroupPanelVM.MachGroupVMList(0)
      End If
      EgtDraw()
      ' se nessun grezzo, rimetto tutti i pezzi invisibili
      If Map.refProjectVM.MachGroupPanelVM.MachGroupVMList.Count = 0 Then
         Map.refProjectVM.BTLStructureVM.HideAll(True)
      End If
   End Sub

#End Region    ' RemoveRawPart

#Region "RemoveAllRawPart"

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

   ''' <summary>
   ''' Execute the Exec.  This method is invoked by the ExecCommand.
   ''' </summary>
   Public Sub RemoveAllRawPart()
      RemoveRawPart(True)
   End Sub

#End Region    ' RemoveAllRawPart

#Region "MovePartInRawPart"

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

   ''' <summary>
   ''' Execute the Exec.  This method is invoked by the ExecCommand.
   ''' </summary>
   Public Sub MovePartInRawPart()
      If IsNothing(ProjectManagerVM.CurrProd) Then Return
      Dim MovePartInRawPartWndVM As New MovePartInRawPartWndVM()
      Dim MovePartInRawPartWnd As Object
      If Map.refMainMenuVM.SelPage = Pages.ONLYPRODPAGE Then
         MovePartInRawPartWnd = New OnlyProdMovePartInRawPartWndV(Application.Current.MainWindow, MovePartInRawPartWndVM)
      Else
         MovePartInRawPartWnd = New MovePartInRawPartWndV(Application.Current.MainWindow, MovePartInRawPartWndVM)
      End If
      If Not MovePartInRawPartWnd.ShowDialog() Then Return
   End Sub

#End Region    ' MovePartInRawPart

#End Region    ' Commands

End Class
