Imports System.Collections.ObjectModel
Imports System.IO
Imports System.Windows
Imports EgtUILib
Imports EgtWPFLib5

Public Class MachGroupPanelM

#Region "FIELDS & PROPERTIES"

   ' Lista delle macchine disponibili
   Protected m_MachineList As List(Of Machine)

   ' Macchina di default
   Protected m_DefaultMachine As String = String.Empty
   Public ReadOnly Property DefaultMachine As String
      Get
         Return m_DefaultMachine
      End Get
   End Property
   ' Variabile che definisce se il nome è automatico
   Protected m_IsGroupNameAutomatic As Boolean = True
   ' Variabile che definisce se ci sono più gruppi di lavorazione
   Protected m_IsMultiMachGroup As Boolean
   Public ReadOnly Property IsMultiMachGroup As Boolean
      Get
         Return m_IsMultiMachGroup
      End Get
   End Property
   ' Nome base dei gruppi
   Protected m_BaseName As String

   Protected m_MachGroupMList As New List(Of MachGroupM)
   Public Property MachGroupMList As List(Of MachGroupM)
      Get
         Return m_MachGroupMList
      End Get
      Set(value As List(Of MachGroupM))
         m_MachGroupMList = value
      End Set
   End Property

   Protected m_MachGroupPanel_Visibility As Visibility
   Public Property MachGroupPanel_Visibility As Visibility
      Get
         Return m_MachGroupPanel_Visibility
      End Get
      Set(value As Visibility)
         m_MachGroupPanel_Visibility = value
      End Set
   End Property

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

#End Region    ' FIELDS & PROPERTIES

#Region "CONSTRUCTOR"

   Protected Sub New()
   End Sub

   Public Shared Function CreateNewMachGroupPanel() As MachGroupPanelM
      Return New MachGroupPanelM
   End Function

   Public Shared Function CreateMachGroupPanel(IsMultiMachGroup As Boolean,
                                               MachineList As List(Of Machine), DefaultMachine As String,
                                               Optional sBaseName As String = "MachGroup_1") As MachGroupPanelM
      Dim NewMachGroupPanelM As New MachGroupPanelM
      NewMachGroupPanelM.m_IsMultiMachGroup = IsMultiMachGroup
      ' Assegno nome base
      NewMachGroupPanelM.m_BaseName = sBaseName
      ' Recupero lista macchine
      NewMachGroupPanelM.m_MachineList = MachineList
      ' Assegno macchina di default
      NewMachGroupPanelM.m_DefaultMachine = DefaultMachine
      ' recupero i MachGroup
      NewMachGroupPanelM.m_MachGroupMList = LoadMachGroups()

      Return NewMachGroupPanelM
   End Function

   Public Shared Function LoadMachGroups() As List(Of MachGroupM)
      Dim TempList As New List(Of MachGroupM)
      Dim nId = EgtGetFirstMachGroup()
      While nId <> GDB_ID.NULL
         EgtSetCurrMachGroup(nId)
         Dim sName As String = String.Empty
         Dim sMachine As String = String.Empty
         EgtGetMachGroupName(nId, sName)
         EgtGetMachGroupMachineName(nId, sMachine)
         TempList.Add(MachGroupM.CreateMachGroup(nId, sName, sMachine))
         nId = EgtGetNextMachGroup(nId)
      End While
      Return TempList
   End Function

#End Region    ' CONSTRUCTOR

#Region "METHODS"

   Public Event MachGroupAdded As EventHandler(Of MachGroupAddedEventArgs)
   Public Event MachGroupRemoved As EventHandler(Of MachGroupAddedEventArgs)

   Public Overridable Sub AddMachGroup(MachGroupM As MachGroupM)
      If IsNothing(MachGroupM) Then Return
      If Not m_MachGroupMList.Contains(MachGroupM) Then
         m_MachGroupMList.Add(MachGroupM)
         RaiseEvent MachGroupAdded(Me, New MachGroupAddedEventArgs(MachGroupM))
      End If
   End Sub

   Public Overridable Sub RemoveMachGroup(MachGroupM As MachGroupM)
      If IsNothing(MachGroupM) Then Return
      If m_MachGroupMList.Contains(MachGroupM) AndAlso m_MachGroupMList.Remove(MachGroupM) Then
         RaiseEvent MachGroupRemoved(Me, New MachGroupAddedEventArgs(MachGroupM))
      End If
   End Sub

   Public Function GetMachGroups() As List(Of MachGroupM)
      Return New List(Of MachGroupM)(m_MachGroupMList)
   End Function

   'Public Function InitMachGroupPanel(IsMultiMachGroup As Boolean,
   '                               MachineList As List(Of Machine), DefaultMachine As String,
   '                               Optional sBaseName As String = "MachGroup_1") As Boolean
   '   m_IsMultiMachGroup = IsMultiMachGroup
   '   ' se è a gruppo singolo, nascondo il panel
   '   If Not m_IsMultiMachGroup Then SetMachGroupPanelVisibility(False)
   '   ' Assegno nome base
   '   m_BaseName = sBaseName
   '   ' Recupero lista macchine
   '   m_MachineList = MachineList
   '   ' Assegno macchina di default
   '   m_DefaultMachine = DefaultMachine
   '   Return MachineList.Count > 0
   'End Function

   'Public Overridable Sub LoadMachGroupList()
   '   ' Carico i gruppi di lavorazione nella lista
   '   Dim nId = EgtGetFirstMachGroup()
   '   While nId <> GDB_ID.NULL
   '      Dim sName As String = String.Empty
   '      Dim sMachine As String = String.Empty
   '      EgtGetMachGroupName(nId, sName)
   '      EgtGetMachGroupMachineName(nId, sMachine)
   '      MachGroupMList.Add(New MachGroup(nId, sName, sMachine))
   '      nId = EgtGetNextMachGroup(nId)
   '   End While
   'End Sub

   Public Overridable Function NewMachGroup() As MachGroupM
      ' Sistemazioni preliminari
      OnPreNewMachGroup()
      ' Se non ci sono macchine disponibili esco con errore
      If m_MachineList.Count <= 0 Then Return Nothing
      ' Creo il VM per chiedere nome e macchina a seconda del caso in cui mi trovo
      Dim NewMachGroupWndVM As NewMachGroupWndVM = Nothing
      Dim sNewMGrpName As String = m_BaseName
      Dim sNewMachName As String = m_MachineList(0).Name
      If m_IsMultiMachGroup Then
         EgtGetMachGroupNewName(sNewMGrpName)
         If m_MachineList.Count = 1 Then
            If Not m_IsGroupNameAutomatic Then
               NewMachGroupWndVM = New NewMachGroupWndVM(sNewMGrpName)
            End If
         Else
            If m_IsGroupNameAutomatic Then
               NewMachGroupWndVM = New NewMachGroupWndVM(m_DefaultMachine, m_MachineList)
            Else
               NewMachGroupWndVM = New NewMachGroupWndVM(sNewMGrpName, m_DefaultMachine, m_MachineList)
            End If
         End If
      ElseIf EgtGetMachGroupCount() = 0 Then
         If m_MachineList.Count > 1 Then
            NewMachGroupWndVM = New NewMachGroupWndVM(m_DefaultMachine, m_MachineList)
         End If
      End If
      ' Se necessario, lancio finestra di acquisizione dati
      If Not IsNothing(NewMachGroupWndVM) Then
         ' Se non esistono gruppi nella lista, rendo la finestra non chiudibile senza scelta
         If m_MachGroupMList.Count = 0 Then NewMachGroupWndVM.IsClosable = False
         ' Lancio finestra
         Dim NewMachGroupWndV As New NewMachGroupWndV(Application.Current.MainWindow, NewMachGroupWndVM)
         If NewMachGroupWndV.ShowDialog() = False Then Return Nothing
         ' Recupero dati
         If Not IsNothing(NewMachGroupWndVM.Name) Then sNewMGrpName = NewMachGroupWndVM.Name
         If Not IsNothing(NewMachGroupWndVM.SelectedMachine) Then sNewMachName = NewMachGroupWndVM.SelectedMachine.Name
      End If
      ' Creo il nuovo gruppo di lavorazione con i dati ottenuti a seconda del caso in cui mi trovo
      Dim MachGroupM As MachGroupM = MachGroupM.CreateMachGroup(sNewMGrpName, sNewMachName)
      If IsNothing(MachGroupM) Then Return Nothing
      AddMachGroup(MachGroupM)
      ' Sistemazioni finali
      m_DefaultMachine = sNewMachName
      OnPostNewMachGroup()
      Return MachGroupM
   End Function

   Public Function NewMachGroupWithDefaults() As MachGroupM
      ' Sistemazioni preliminari
      OnPreNewMachGroup()
      ' Se non ci sono macchine disponibili esco con errore
      If m_MachineList.Count <= 0 Then Return Nothing
      ' Assegno valori di default
      Dim sNewMGrpName As String = m_BaseName
      Dim sNewMachName As String = If(Machine.ExistsMachine(m_DefaultMachine, m_MachineList), m_DefaultMachine, m_MachineList(0).Name)
      ' Creo il nuovo gruppo di lavorazione con i dati ottenuti a seconda del caso in cui mi trovo
      Dim MachGroupM As MachGroupM = MachGroupM.CreateMachGroup(sNewMGrpName, sNewMachName)
      If IsNothing(MachGroupM) Then Return Nothing
      AddMachGroup(MachGroupM)
      ' Sistemazioni finali
      m_DefaultMachine = sNewMachName
      OnPostNewMachGroup()
      Return MachGroupM
   End Function

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

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

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

#End Region     ' METHODS

End Class
