Imports System.Collections.ObjectModel
Imports System.IO
Imports EgtUILib
Imports EgtWPFLib5
Imports EgtBEAMWALL.Core

Public Class AddFeatureWndVM
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

   Friend Event m_CloseWindow(bDialogResult As Boolean)
   Friend m_MacroFilePathList As List(Of String)
   Friend m_MacroCustomFilePathList As List(Of String)

   Private m_PrevSelPRCIndex_L As Integer = 0
   Private m_PrevSelPRCIndex_T As Integer = 0

   Private m_SelGRPType As GRPType
   Public Property SelGRPType As Integer
      Get
         Return m_SelGRPType
      End Get
      Set(value As Integer)
         m_SelGRPType = value
         ' carico lista process
         Dim TempPRCList As New ObservableCollection(Of Integer)
         GetBeamPrivateProfileProcessList(m_SelGRPType, TempPRCList)
         m_PRCList.Clear()
         For Each PRC In TempPRCList
            ' leggo gruppi 
            Dim GRPList As New ObservableCollection(Of Integer)
            GetBeamPrivateProfileGRPList(m_SelGRPType, PRC, GRPList)
            m_PRCList.Add(New PRC(If(GRPList.Count > 0, GRPList(0), m_SelGRPType), PRC, GetBeamPrivateProfileName(m_SelGRPType, PRC)))
         Next
         If m_SelGRPType = GRPType.L Then
            nSelPRC = PRCList(m_PrevSelPRCIndex_L)
         ElseIf m_SelGRPType = GRPType.T Then
            nSelPRC = PRCList(m_PrevSelPRCIndex_T)
         End If
         NotifyPropertyChanged(NameOf(PRCList))
         NotifyPropertyChanged(NameOf(nSelPRC))
      End Set
   End Property

   Private m_PRCList As New ObservableCollection(Of PRC)
   Public ReadOnly Property PRCList As ObservableCollection(Of PRC)
      Get
         Return m_PRCList
      End Get
   End Property

   Private m_PRCListView As New List(Of PRC)
   Public ReadOnly Property PRCListView As List(Of PRC)
      Get
         Return m_PRCListView
      End Get
   End Property

#Region "New Macro OnlyProdPage"

   Private m_MacroDefaultList As New ObservableCollection(Of Object)
   Public ReadOnly Property MacroDefaultList As ObservableCollection(Of Object)
      Get
         Return m_MacroDefaultList
      End Get
   End Property

   Private m_nSelMacroDefault As Integer = -1
   Public Property nSelMacroDefault As Integer
      Get
         Return m_nSelMacroDefault
      End Get
      Set(value As Integer)
         If value <> m_nSelMacroDefault Then
            m_nSelMacroDefault = value
         End If
      End Set
   End Property

#End Region ' New Macro OnlyProdPage

   Private m_nSelPRC As PRC = Nothing
   Public Property nSelPRC As PRC
      Get
         Return m_nSelPRC
      End Get
      Set(value As PRC)
         If value IsNot m_nSelPRC Then
            m_nSelPRC = value
            If Not IsNothing(m_nSelPRC) Then
               If SelGRPType = GRPType.L Then
                  m_PrevSelPRCIndex_L = PRCList.IndexOf(m_nSelPRC)
               ElseIf SelGRPType = GRPType.T Then
                  m_PrevSelPRCIndex_T = PRCList.IndexOf(m_nSelPRC)
               End If
            End If
            NotifyPropertyChanged(NameOf(sDrawPath))
            ' carico eventuali file Macro
            MacroCustomList.Clear()
            If Core.Configuration.bOnlyProd Then
               MacroDefaultList.Clear()
            End If
            m_MacroFilePathList = New List(Of String)
            m_MacroCustomFilePathList = New List(Of String)
            If Not IsNothing(nSelPRC) Then
               Dim nGRPFromFileName As Integer = 0
               Dim nPRCFromFileName As Integer = 0
               Dim sMacroNameFromFileName As String = String.Empty
               Dim nIdMacroName As Integer = 0
               ' ricavo lista file Macro Default
               GetMacroListDefault(nGRPFromFileName, nPRCFromFileName, sMacroNameFromFileName, nIdMacroName)
               ' ricavo lista file Macro Custom
               If Core.Configuration.bOnlyProd Then GetMacroListCustom(nGRPFromFileName, nPRCFromFileName, sMacroNameFromFileName, nIdMacroName)
               If MacroCustomList.Count > 0 Then
                  MacroList_IsEnabled = True
               Else
                  MacroList_IsEnabled = False
               End If
            End If
         End If
         NotifyPropertyChanged(NameOf(nSelPRC))
      End Set
   End Property

   Private m_MacroList_IsEnabled As Boolean
   Public Property MacroList_IsEnabled As Boolean
      Get
         Return m_MacroList_IsEnabled
      End Get
      Set(value As Boolean)
         If value <> m_MacroList_IsEnabled Then
            m_MacroList_IsEnabled = value
            NotifyPropertyChanged(NameOf(MacroList_IsEnabled))
         End If
      End Set
   End Property

   Private m_MacroCustomList As New ObservableCollection(Of Object)
   Public ReadOnly Property MacroCustomList As ObservableCollection(Of Object)
      Get
         Return m_MacroCustomList
      End Get
   End Property

   Private m_nSelMacroCustom As Integer = -1
   Public Property nSelMacroCustom As Integer
      Get
         Return m_nSelMacroCustom
      End Get
      Set(value As Integer)
         If value <> m_nSelMacroCustom Then
            m_nSelMacroCustom = value
         End If
      End Set
   End Property

   Public ReadOnly Property sDrawPath As String
      Get
         If IsNothing(m_nSelPRC) Then Return ""
         Dim sDescConstruction As String = String.Empty
         If m_SelGRPType = GRPType.L Then
            sDescConstruction = "L"
         Else
            sDescConstruction = "T"
         End If
         sDescConstruction &= m_nSelPRC.nPRC.ToString("000")
         Return Map.refMainWindowVM.MainWindowM.sResourcesRoot & "\Features\" & sDescConstruction & ".png"
      End Get
   End Property

   ' Definizione comandi
   Private m_cmdOk As ICommand

#Region "Messages"

   Public ReadOnly Property Process_Msg As String
      Get
         Return EgtMsg(61805).ToUpper()
      End Get
   End Property

   Public ReadOnly Property Default_Msg As String
      Get
         Return EgtMsg(61899).ToUpper()
      End Get
   End Property

   Public ReadOnly Property Custom_Msg As String
      Get
         Return EgtMsg(61801).ToUpper()
      End Get
   End Property

   Public ReadOnly Property OK_Msg As String
      Get
         Return EgtMsg(91651)
      End Get
   End Property

   Public ReadOnly Property Cancel_Msg As String
      Get
         Return EgtMsg(91652)
      End Get
   End Property

#End Region

#End Region    ' FIELDS & PROPERTIES

#Region "CONSTRUCTOR"

   Sub New()
      If Core.Configuration.bOnlyProd Then
         ' carico lista process
         Dim TempPRCList As New ObservableCollection(Of Integer)
         Dim TempPRCLList As New ObservableCollection(Of Integer)
         Dim TempPRCLTist As New ObservableCollection(Of Integer)
         GetBeamPrivateProfileProcessList(0, TempPRCLList)
         GetBeamPrivateProfileProcessList(1, TempPRCLTist)
         ' riporto le liste process in un unica lista
         For Each PRCL In TempPRCLList
            TempPRCList.Add(PRCL)
         Next
         For Each PRCT In TempPRCLTist
            TempPRCList.Add(PRCT)
         Next
         m_PRCList.Clear()
         ' ciclo sulla lista process eliminado i doppi
         For Each PRC In TempPRCList.Distinct()
            ' leggo gruppi 
            Dim GRPLList As New ObservableCollection(Of Integer)
            Dim GRPTList As New ObservableCollection(Of Integer)
            GetBeamPrivateProfileGRPList(0, PRC, GRPLList)
            GetBeamPrivateProfileGRPList(1, PRC, GRPTList)
            If GRPLList.Count > 0 Then m_PRCList.Add(New PRC(GRPLList(0), PRC, GetBeamPrivateProfileName(0, PRC)))
            If GRPTList.Count > 0 Then m_PRCList.Add(New PRC(GRPTList(0), PRC, GetBeamPrivateProfileName(1, PRC)))
         Next
         m_PRCListView = m_PRCList.OrderBy(Function(x) x.nPRC).ToList()
         nSelPRC = m_PRCList(0)
         NotifyPropertyChanged(NameOf(PRCListView))
         NotifyPropertyChanged(NameOf(PRCList))
      Else
         SelGRPType = 0
         nSelPRC = PRCList(0)
      End If
   End Sub

#End Region    ' CONSTRUCTOR

#Region "METHODS"

   Private Sub GetMacroListDefault(ByRef nGRPFromFileName As Integer, ByRef nPRCFromFileName As Integer, ByRef sMacroNameFromFileName As String, ByRef nIdMacroName As Integer)
      Dim AllFilesInDir As IEnumerable(Of String) = IO.Directory.EnumerateFiles(Map.refMainWindowVM.MainWindowM.sMacroCustomDir)
      For Each File In AllFilesInDir
         If Path.GetExtension(File).ToLower() = ".lua" Then
            If File.Contains(FILENAMESEPARATOR) Then
               Dim FileName As String = Path.GetFileNameWithoutExtension(File)
               Dim DataFromFileName As String() = FileName.Split(FILENAMESEPARATOR)
               If DataFromFileName.Count = 2 Then
                  If Not String.IsNullOrEmpty(DataFromFileName(0)) Then
                     Dim NumbersFromFileName As String() = DataFromFileName(0).Split(".")
                     Integer.TryParse(NumbersFromFileName(0), nGRPFromFileName)
                     Integer.TryParse(NumbersFromFileName(1), nPRCFromFileName)
                  End If
                  If Not String.IsNullOrEmpty(DataFromFileName(1)) Then
                     sMacroNameFromFileName = DataFromFileName(1)
                  End If
                  If nGRPFromFileName = CalcBeamPrivateProfileGRP(nSelPRC.nGRP) AndAlso nPRCFromFileName = nSelPRC.nPRC Then
                     If Core.Configuration.bOnlyProd Then
                        MacroCustomList.Add(New NewMacro(nIdMacroName, sMacroNameFromFileName, Map.refMainWindowVM.MainWindowM.sMacroCustomDir & "\Template\" & sMacroNameFromFileName & ".png"))
                     Else
                        MacroCustomList.Add(New IdNameStruct(nIdMacroName, sMacroNameFromFileName))
                     End If
                     m_MacroFilePathList.Add(File)
                     nIdMacroName += 1
                  End If
               End If
            End If
         End If
      Next
   End Sub

   Private Sub GetMacroListCustom(ByRef nGRPFromFileName As Integer, ByRef nPRCFromFileName As Integer, ByRef sMacroNameFromFileName As String, ByRef nIdMacroName As Integer)
      nIdMacroName = 0
      Dim AllFilesInDirCustom As IEnumerable(Of String) = IO.Directory.EnumerateFiles(Map.refMainWindowVM.MainWindowM.sMacroDefaultDir)
      For Each File In AllFilesInDirCustom
         If Path.GetExtension(File).ToLower() = ".lua" Then
            If File.Contains(FILENAMESEPARATOR) Then
               Dim FileName As String = Path.GetFileNameWithoutExtension(File)
               Dim DataFromFileName As String() = FileName.Split(FILENAMESEPARATOR)
               If DataFromFileName.Count = 2 Then
                  If Not String.IsNullOrEmpty(DataFromFileName(0)) Then
                     Dim NumbersFromFileName As String() = DataFromFileName(0).Split(".")
                     Integer.TryParse(NumbersFromFileName(0), nGRPFromFileName)
                     Integer.TryParse(NumbersFromFileName(1), nPRCFromFileName)
                  End If
                  If Not String.IsNullOrEmpty(DataFromFileName(1)) Then
                     sMacroNameFromFileName = DataFromFileName(1)
                  End If
                  If nGRPFromFileName = CalcBeamPrivateProfileGRP(nSelPRC.nGRP) AndAlso nPRCFromFileName = nSelPRC.nPRC Then
                     MacroDefaultList.Add(New NewMacro(nIdMacroName, sMacroNameFromFileName))
                     m_MacroCustomFilePathList.Add(File)
                     nIdMacroName += 1
                  End If
               End If
            End If
         End If
      Next
   End Sub

#End Region    ' METHODS

#Region "COMMANDS"

#Region "Ok"

   Public ReadOnly Property Ok_Command As ICommand
      Get
         If m_cmdOk Is Nothing Then
            m_cmdOk = New Command(AddressOf Ok)
         End If
         Return m_cmdOk
      End Get
   End Property

   Public Sub Ok()
      'verifico che tutti i campi contengano un valore valido
      If Not IsNothing(m_nSelPRC) AndAlso m_nSelPRC.nPRC > 0 Then
         RaiseEvent m_CloseWindow(True)
      Else
         'MessageBox.Show(EgtMsg(61852), EgtMsg(30007))
         If bOnlyProd Then
            EgtBEAMWALL.Core.OnlyProdEgtMessageBoxV.Show(Application.Current.MainWindow, EgtMsg(61852), EgtMsg(30007))
         Else
            EgtBEAMWALL.Core.EgtMessageBoxV.Show(Application.Current.MainWindow, EgtMsg(61852), EgtMsg(30007))
         End If
      End If
   End Sub

#End Region ' Ok

#End Region    ' COMMANDS

End Class

Public Class PRC

   ' Processo
   Private m_nPRC As Integer
   Friend ReadOnly Property nPRC As Integer
      Get
         Return m_nPRC
      End Get
   End Property

   ' Gruppo
   Private m_nGRP As Integer
   Friend ReadOnly Property nGRP As Integer
      Get
         Return m_nGRP
      End Get
   End Property

   Private m_nSIDE As Integer = 2
   Public ReadOnly Property nSIDE As Integer
      Get
         Return m_nSIDE
      End Get
   End Property

   ' Nome
   Private m_sName As String
   Public ReadOnly Property sName As String
      Get
         Return m_sName
      End Get
   End Property

   Public ReadOnly Property ghDesc As String
      Get
         Return If(m_nGRP = 1 OrElse m_nGRP = 2, "T", "L") & m_nPRC.ToString("000") & " " & m_sName
      End Get
   End Property

   ' lista dei parametri Q della feature
   Protected m_QBTLParamVMList As List(Of QBTLParamVM)
   Public Property QBTLParamVMList As List(Of QBTLParamVM)
      Get
         Return m_QBTLParamVMList
      End Get
      Set(value As List(Of QBTLParamVM))
         m_QBTLParamVMList = value
      End Set
   End Property

   Sub New(GRP As Integer, PRC As Integer, Name As String)
      m_nPRC = PRC
      m_sName = Name
      m_nGRP = GRP
   End Sub

   Sub New(GRP As Integer, PRC As Integer, Name As String, QBTLParamList As List(Of QBTLParamVM))
      m_nPRC = PRC
      m_sName = Name
      m_nGRP = GRP
      m_QBTLParamVMList = QBTLParamList
   End Sub

End Class

Public Class NewMacro

   ' Indice Macro
   Private m_nIndexMacro As Integer
   Public ReadOnly Property nIndexMacro As Integer
      Get
         Return m_nIndexMacro
      End Get
   End Property

   ' Nome Macro
   Private m_sMacroName As String
   Public ReadOnly Property sMacroName As String
      Get
         Return m_sMacroName.ToUpper()
      End Get
   End Property

   ' Cartella dove sono contenuti i templete delle macro
   Private m_sMacroDrawPath As String
   Public ReadOnly Property sMacroDrawPath As String
      Get
         Return m_sMacroDrawPath
      End Get
   End Property

   Sub New(IndexMacro As Integer, MacroName As String)
      m_nIndexMacro = IndexMacro
      m_sMacroName = MacroName
   End Sub

   Sub New(IndexMacro As Integer, MacroName As String, MacroDrawPath As String)
      m_nIndexMacro = IndexMacro
      m_sMacroName = MacroName
      m_sMacroDrawPath = MacroDrawPath
   End Sub

End Class