Imports System.Collections.ObjectModel
Imports System.Collections.Specialized
Imports System.ComponentModel
Imports System.IO
Imports System.Windows
Imports System.Windows.Input
Imports EgtBEAMWALL.Core
Imports EgtUILib
Imports EgtWPFLib5

Public Class NewMachGroupPanelVM
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

   Protected m_MachGroupPanelM As MachGroupPanelM

   Public ReadOnly Property DefaultMachine As String
      Get
         Return m_MachGroupPanelM.DefaultMachine
      End Get
   End Property

   Protected m_MachGroupVMList As ObservableCollection(Of MachGroupVM)
   Public Property MachGroupVMList As ObservableCollection(Of MachGroupVM)
      Get
         Return m_MachGroupVMList
      End Get
      Set(value As ObservableCollection(Of MachGroupVM))
         m_MachGroupVMList = value
      End Set
   End Property

   Protected m_SelectedMachGroup As MachGroupVM
   Public Property SelectedMachGroup As MachGroupVM
      Get
         Return m_SelectedMachGroup
      End Get
      Set(value As MachGroupVM)
         m_SelectedMachGroup = value
         If Not IsNothing(value) Then
            OnPreSetCurrMachGroup()
            EgtSetCurrMachGroup(value.Id)
            OnPostSetCurrMachGroup()
         End If
         NotifyPropertyChanged(NameOf(SelectedMachGroup))
      End Set
   End Property

   Public ReadOnly Property MachGroupPanel_Visibility As Visibility
      Get
         Return m_MachGroupPanelM.MachGroupPanel_Visibility
      End Get
   End Property

   Public Sub SetMachGroupPanelVisibility(IsVisible As Boolean)
      If IsVisible Then
         m_MachGroupPanelM.MachGroupPanel_Visibility = Visibility.Visible
      Else
         m_MachGroupPanelM.MachGroupPanel_Visibility = Visibility.Collapsed
      End If
      NotifyPropertyChanged(NameOf(MachGroupPanel_Visibility))
   End Sub


   ' Variabile che permette di abilitare/disabilitare i bottoni aggiungi e togli MachGroup
   Public Property IsEnabledAddRemove As Boolean
      Get
         Return m_MachGroupPanelM.IsEnabledAddRemove
      End Get
      Set(value As Boolean)
         m_MachGroupPanelM.IsEnabledAddRemove = value
         NotifyPropertyChanged(NameOf(IsEnabledAddRemove))
      End Set
   End Property
   ' Variabile che definisce lo stato (attivi/disattivi) di tutti i gruppi tranne quello selezionato
   Public ReadOnly Property IsEnabledMachGroups As Boolean
      Get
         Return m_MachGroupPanelM.IsEnabledMachGroups
      End Get
   End Property

   ' Definizione comandi
   Private m_cmdSetCurrMachGroup As ICommand
   Private m_cmdAddMachGroup As ICommand
   Private m_cmdRemoveMachGroup As ICommand

#End Region    ' FIELDS & PROPERTIES

#Region "CONSTRUCTOR"

   Sub New(MachGroupPanelM As MachGroupPanelM)
      m_MachGroupPanelM = MachGroupPanelM
      AddHandler m_MachGroupPanelM.MachGroupAdded, AddressOf OnMachGroupAdded
      If Not MachGroupPanelM.IsMultiMachGroup Then SetMachGroupPanelVisibility(False)
      CreateMachGroupVMList()
   End Sub

#End Region    ' CONSTRUCTOR

#Region "METHODS"

   Protected Overridable Sub CreateMachGroupVMList()
      Dim all As List(Of MachGroupVM) = (From MachGroupM In m_MachGroupPanelM.GetMachGroups()
                                         Select New MachGroupVM(MachGroupM)).ToList()

      For Each MachGroupvM As MachGroupVM In all
         AddHandler MachGroupvM.PropertyChanged, AddressOf OnMachGroupVMPropertyChanged
      Next

      m_MachGroupVMList = New ObservableCollection(Of MachGroupVM)(all)
      AddHandler m_MachGroupVMList.CollectionChanged, AddressOf OnMachGroupVMListChanged
   End Sub

   Public Overridable Function InitMachGroupList(Optional bUseDefaults As Boolean = False) As Boolean
      ' Svuoto precedente lista di MachGroup
      m_MachGroupPanelM.MachGroupMList.Clear()
      ' verifico se esistono già gruppi di lavorazione o se devo creare il primo
      Dim bOk As Boolean
      Dim nId = EgtGetFirstMachGroup()
      If nId <> GDB_ID.NULL Then
         bOk = EgtSetCurrMachGroup(nId)
      Else
         If bUseDefaults Then
            bOk = Not IsNothing(m_MachGroupPanelM.NewMachGroupWithDefaults())
         Else
            bOk = Not IsNothing(m_MachGroupPanelM.NewMachGroup())
         End If
      End If
      If Not bOk Then Return False
      ' Sistemazioni finali
      m_MachGroupPanelM.MachGroupMList = MachGroupPanelM.LoadMachGroups()
      SelectedMachGroup = MachGroupVMList(0)
      Return True
   End Function

   ' Funzione che permette di abilitare o disabilitare tutti i gruppi tranne quello selezionato più i bottni Add/Remove
   Public Function SetMachGroupState(bState As Boolean) As Boolean
      If bState Then
         For Each Group In MachGroupVMList
            Group.IsEnabled = True
         Next
         m_MachGroupPanelM.IsEnabledAddRemove = True
         NotifyPropertyChanged(NameOf(IsEnabledAddRemove))
         m_MachGroupPanelM.IsEnabledMachGroups = True
         Return True
      Else
         For Each Group In MachGroupVMList
            If Group Is SelectedMachGroup Then
               Group.IsEnabled = True
            Else
               Group.IsEnabled = False
            End If
         Next
         m_MachGroupPanelM.IsEnabledAddRemove = False
         NotifyPropertyChanged(NameOf(IsEnabledAddRemove))
         m_MachGroupPanelM.IsEnabledMachGroups = False
         Return True
      End If
      Return False
   End Function

   Public Overridable Function OnPreSetCurrMachGroup() As Boolean
      Return True
   End Function

   Public Overridable Function OnPostSetCurrMachGroup() As Boolean
      Return True
   End Function

#End Region    ' METHODS

#Region "COMMANDS"

#Region "AddMachGroupCommand"

   ''' <summary>
   ''' Returns a command that set the selected MachGroup as the Current one.
   ''' </summary>
   Public ReadOnly Property AddMachGroupCommand As ICommand
      Get
         If m_cmdAddMachGroup Is Nothing Then
            m_cmdAddMachGroup = New Command(AddressOf AddMachGroup)
         End If
         Return m_cmdAddMachGroup
      End Get
   End Property

   Public Overridable Sub AddMachGroup()
      Dim MachGroupM As MachGroupM = m_MachGroupPanelM.NewMachGroup()
      If IsNothing(MachGroupM) Then Return
      '  lo seleziono
      SelectedMachGroup = MachGroupVMList.FirstOrDefault(Function(x) x.MachGroupM Is MachGroupM)
      EgtZoom(ZM.ALL)
   End Sub

#End Region ' AddMachGroupCommand

#Region "RemoveMachGroupCommand"

   ''' <summary>
   ''' Returns a command that set the selected MachGroup as the Current one.
   ''' </summary>
   Public ReadOnly Property RemoveMachGroupCommand As ICommand
      Get
         If m_cmdRemoveMachGroup Is Nothing Then
            m_cmdRemoveMachGroup = New Command(AddressOf RemoveMachGroup)
         End If
         Return m_cmdRemoveMachGroup
      End Get
   End Property

   Public Overridable Sub RemoveMachGroup()
      ' Calcolo indice del gruppo da cancellare
      Dim nSelectedMachGroupIndex As Integer = MachGroupVMList.IndexOf(SelectedMachGroup)
      If MachGroupVMList.Count = 1 Then
         'chiedo conferma prima di resettare il gruppo di lavorazione
         Select Case MessageBox.Show("Eliminare?", "", MessageBoxButton.YesNo, MessageBoxImage.Question)
            Case MessageBoxResult.Yes
               ' cancello il gruppo corrente e ne creo uno nuovo con lo stesso nome
               m_MachGroupPanelM.OnPreRemoveCurrMachGroup()
               EgtRemoveMachGroup(MachGroupVMList(0).Id)
               MachGroupVMList.Clear()
               AddMachGroup()
               EgtZoom(ZM.ALL)
            Case MessageBoxResult.No
               Return
         End Select
      Else
         'chiedo conferma prima di cancellare il gruppo di lavorazione
         Select Case MessageBox.Show("Eliminare?", "", MessageBoxButton.YesNo, MessageBoxImage.Question)
            Case MessageBoxResult.Yes
               ' cancello quello selezionato (ovvero il corrente)
               m_MachGroupPanelM.OnPreRemoveCurrMachGroup()
               EgtRemoveMachGroup(MachGroupVMList(nSelectedMachGroupIndex).Id)
               ' rendo corrente il gruppo di lavorazione successivo a quello da cancellare
               If nSelectedMachGroupIndex = 0 And MachGroupVMList.Count > 1 Then
                  EgtSetCurrMachGroup(MachGroupVMList(nSelectedMachGroupIndex + 1).Id)
                  SelectedMachGroup = MachGroupVMList(nSelectedMachGroupIndex + 1)
                  ' rendo corrente il gruppo di lavorazione precedente a quello da cancellare
               ElseIf nSelectedMachGroupIndex > 0 Then
                  EgtSetCurrMachGroup(MachGroupVMList(nSelectedMachGroupIndex - 1).Id)
                  SelectedMachGroup = MachGroupVMList(nSelectedMachGroupIndex - 1)
               End If
               EgtZoom(ZM.ALL)
               ' aggiorno la lista dei gruppi
               MachGroupVMList.RemoveAt(nSelectedMachGroupIndex)
            Case MessageBoxResult.No
               Return
         End Select
      End If

   End Sub

#End Region ' RemoveMachGroupCommand

#End Region    ' COMMANDS

#Region "EVENTS"

   Protected Overridable Sub OnMachGroupAdded(sender As Object, e As MachGroupAddedEventArgs)
      Dim MachGroupVM As MachGroupVM = New MachGroupVM(e.NewMachGroupM)
      m_MachGroupVMList.Add(MachGroupVM)
      NotifyPropertyChanged(NameOf(MachGroupVMList))
   End Sub

   Protected Sub OnMachGroupVMListChanged(sender As Object, e As NotifyCollectionChangedEventArgs)
      If Not IsNothing(e.NewItems) AndAlso e.NewItems.Count > 0 Then
         For Each MachGroupVM As MachGroupVM In e.NewItems
            AddHandler MachGroupVM.PropertyChanged, AddressOf OnMachGroupVMPropertyChanged
         Next
      End If
      If Not IsNothing(e.OldItems) AndAlso e.OldItems.Count > 0 Then
         For Each MachGroupVM As MachGroupVM In e.OldItems
            RemoveHandler MachGroupVM.PropertyChanged, AddressOf OnMachGroupVMPropertyChanged
            m_MachGroupPanelM.MachGroupMList.Remove(MachGroupVM.MachGroupM)
         Next
      End If
   End Sub

   Protected Overridable Sub OnMachGroupVMPropertyChanged(sender As Object, e As PropertyChangedEventArgs)
      Select Case e.PropertyName
         'Case nameof(sender.sMATERIAL)
      End Select
   End Sub

#End Region    ' EVENTS

End Class