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)

   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("PRCList")
         NotifyPropertyChanged("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_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("sDrawPath")
            ' carico eventuali file Macro
            MacroList.Clear()
            m_MacroFilePathList = 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
               Dim AllFilesInDir As IEnumerable(Of String) = IO.Directory.EnumerateFiles(Map.refMainWindowVM.MainWindowM.sMacroDir)
               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
                              MacroList.Add(New IdNameStruct(nIdMacroName, sMacroNameFromFileName))
                              m_MacroFilePathList.Add(File)
                              nIdMacroName = nIdMacroName + 1
                           End If
                        End If
                     End If
                  End If
               Next
               If MacroList.Count > 0 Then
                  MacroList_IsEnabled = True
               Else
                  MacroList_IsEnabled = False
               End If
            End If
         End If
      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("MacroList_IsEnabled")
         End If
      End Set
   End Property

   Private m_MacroList As New ObservableCollection(Of Object)
   Public ReadOnly Property MacroList As ObservableCollection(Of Object)
      Get
         Return m_MacroList
      End Get
   End Property

   Private m_nSelMacro As Integer = -1
   Public Property nSelMacro As Integer
      Get
         Return m_nSelMacro
      End Get
      Set(value As Integer)
         If value <> m_nSelMacro Then
            m_nSelMacro = 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

#End Region    ' FIELDS & PROPERTIES

#Region "CONSTRUCTOR"

   Sub New()
      SelGRPType = 0
      nSelPRC = PRCList(0)
   End Sub

#End Region    ' CONSTRUCTOR

#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))
      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