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

Public Class ConfigurationPageVM
   Inherits VMBase

   ' Definizione comandi
   Private m_cmdSave As ICommand
   Private m_cmdAddVariable As ICommand
   Private m_cmdRemoveVariable As ICommand

   ' Contatore delle variabili rimosse per successiva rimozione dal file INI
   Friend m_RemovedVarsCount As Integer = 0

   Private m_LanguageList As New ObservableCollection(Of Language)
   Public ReadOnly Property LanguageList As ObservableCollection(Of Language)
      Get
         Return m_LanguageList
      End Get
   End Property

   Private m_UnitMeasureList As List(Of String) = New List(Of String)({"inch", "mm"})
   Public ReadOnly Property UnitMeasureList As List(Of String)
      Get
         Return m_UnitMeasureList
      End Get
   End Property

   Private m_SelectedLanguage As Language
   Public Property SelectedLanguage As Language
      Get
         Return m_SelectedLanguage
      End Get
      Set(value As Language)
         If value IsNot m_SelectedLanguage Then
            m_SelectedLanguage = value
         End If
      End Set
   End Property

   Public Property SelMeasureUnit As Integer
      Get
         Return If(EgtUiUnitsAreMM(), MeasureUnitOpt.MM, MeasureUnitOpt.INCH)
      End Get
      Set(value As Integer)
         ' salvo unità di misura precedente
         Dim PrevMeasureUnit As MeasureUnitOpt = If(EgtUiUnitsAreMM(), MeasureUnitOpt.MM, MeasureUnitOpt.INCH)
         If value <> PrevMeasureUnit Then
            ' cambio unità di misura
            EgtSetUiUnits(DirectCast(value, MeasureUnitOpt) = MeasureUnitOpt.MM)
         End If
      End Set
   End Property

   Protected m_VariablesColumns As New ObservableCollection(Of EgtDataGridColumn)
   Public Property VariablesColumns As ObservableCollection(Of EgtDataGridColumn)
      Get
         Return m_VariablesColumns
      End Get
      Set(value As ObservableCollection(Of EgtDataGridColumn))
         m_VariablesColumns = value
      End Set
   End Property

   Private m_VariablesList As New ObservableCollection(Of ConfigVariable)
   Public Property VariablesList As ObservableCollection(Of ConfigVariable)
      Get
         Return m_VariablesList
      End Get
      Set(value As ObservableCollection(Of ConfigVariable))
         m_VariablesList = value
      End Set
   End Property

   Private m_SelVariable As ConfigVariable
   Public Property SelVariable As ConfigVariable
      Get
         Return m_SelVariable
      End Get
      Set(value As ConfigVariable)
         m_SelVariable = value
      End Set
   End Property

#Region "Messages"

   Public ReadOnly Property L_Msg As String
      Get
         Return EgtMsg(61803)
      End Get
   End Property

   Public ReadOnly Property T_Msg As String
      Get
         Return EgtMsg(61804)
      End Get
   End Property

   Public ReadOnly Property PRC_Msg As String
      Get
         Return EgtMsg(61805)
      End Get
   End Property

   Public ReadOnly Property NAM_Msg As String
      Get
         Return EgtMsg(61608)
      End Get
   End Property

   Public ReadOnly Property Description_Msg As String
      Get
         Return EgtMsg(61603)
      End Get
   End Property

   Public ReadOnly Property Default_Msg As String
      Get
         Return EgtMsg(61802)
      End Get
   End Property

   Public ReadOnly Property Min_Msg As String
      Get
         Return EgtMsg(61616)
      End Get
   End Property
   Public ReadOnly Property Max_Msg As String
      Get
         Return EgtMsg(61617)
      End Get
   End Property
   Public ReadOnly Property CurrentLanguage_Msg As String
      Get
         Return EgtMsg(6501)
      End Get
   End Property
   Public ReadOnly Property LanguageAdvert_Msg As String
      Get
         Return EgtMsg(6502)
      End Get
   End Property
   Public ReadOnly Property MMUnits_Msg As String
      Get
         Return EgtMsg(6540)
      End Get
   End Property

   Public ReadOnly Property DisplayIndex_Msg As String
      Get
         Return EgtMsg(61842)
      End Get
   End Property

   Public ReadOnly Property ColumnName_Msg As String
      Get
         Return EgtMsg(61843)
      End Get
   End Property

   Public ReadOnly Property CanUserReorder_Msg As String
      Get
         Return EgtMsg(61844)
      End Get
   End Property

   Public ReadOnly Property CanUserResize_Msg As String
      Get
         Return EgtMsg(61845)
      End Get
   End Property

   Public ReadOnly Property CanUserSort_Msg As String
      Get
         Return EgtMsg(61846)
      End Get
   End Property

   Public ReadOnly Property IsReadOnly_Msg As String
      Get
         Return EgtMsg(61847)
      End Get
   End Property

   Public ReadOnly Property Visible_Msg As String
      Get
         Return EgtMsg(61848)
      End Get
   End Property

   Public ReadOnly Property Name_Msg As String
      Get
         Return EgtMsg(61808)
      End Get
   End Property

   Public ReadOnly Property VarPath_Msg As String
      Get
         Return EgtMsg(61849)
      End Get
   End Property

   Public ReadOnly Property Type_Msg As String
      Get
         Return EgtMsg(61850)
      End Get
   End Property

#End Region    ' Messages

#Region "Constructor"

   Sub New()
      ' imposto riferimento in Map
      Map.SetRefConfigurationPageVM(Me)
      ' Leggo nome lingua corrente
      Dim sLanguage As String = String.Empty
      GetMainPrivateProfileString(S_GENERAL, K_MESSAGES, "", sLanguage)
      ' Leggo elenco lingue disponibili da file ini e imposto lingua corrente
      Dim nIndex As Integer = 1
      While True
         Dim ReadLanguage As Language = GetMainPrivateProfileLanguage(S_LANGUAGES, K_LANGUAGE & nIndex)
         If IsNothing(ReadLanguage) Then Exit While
         m_LanguageList.Add(ReadLanguage)
         If String.Compare(ReadLanguage.Name, sLanguage, True) = 0 Then
            m_SelectedLanguage = ReadLanguage
         End If
         nIndex += 1
      End While
      ' Inizializzo la lingua corrente
      m_SelectedLanguage = m_LanguageList(0)
      Dim sMsgName As String = String.Empty
      IniFile.GetMainPrivateProfileString(S_GENERAL, K_MESSAGES, "", sMsgName)
      For Each Language In m_LanguageList
         If Language.Name = sMsgName Then
            m_SelectedLanguage = Language
            Exit For
         End If
      Next
      ' carico la lista delle colonne delle variabili della macchina in ConfigurationPage
      GetPrivateProfileColumns(S_VARIABLESLIST, VariablesColumns)
      ' carico le Variables della macchina
      LoadVariables()
   End Sub

#End Region ' Constructor

#Region "SaveCommand"

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

   ''' <summary>
   ''' Execute the Save.  This method is invoked by the SaveCommand.
   ''' </summary>
   Public Sub Save()
      WriteVariables()
      WriteMainPrivateProfileString(S_GENERAL, K_MESSAGES, m_SelectedLanguage.Name)
      WriteMainPrivateProfileString(S_SCENE, K_MMUNITS, SelMeasureUnit.ToString())
   End Sub

#End Region ' SaveCommand

#Region "AddVariable"

   Public ReadOnly Property AddVariable_Command As ICommand
      Get
         If m_cmdAddVariable Is Nothing Then
            m_cmdAddVariable = New Command(AddressOf AddVariable)
         End If
         Return m_cmdAddVariable
      End Get
   End Property

   Public Sub AddVariable()
      VariablesList.Add(New ConfigVariable("[Name]", "[VariablePath]", "plc"))
   End Sub

#End Region ' AddVariable

#Region "RemoveVariable"

   Public ReadOnly Property RemoveVariable_Command As ICommand
      Get
         If m_cmdRemoveVariable Is Nothing Then
            m_cmdRemoveVariable = New Command(AddressOf RemoveVariable)
         End If
         Return m_cmdRemoveVariable
      End Get
   End Property

   Public Sub RemoveVariable()
      VariablesList.Remove(SelVariable)
      m_RemovedVarsCount += 1
   End Sub

#End Region ' RemoveVariable

#Region "Methods"

   ' funzione che verifica la modifica dei valori in Configurazione e ne chiede il salvataggio
   Friend Sub VerifyConfigPageModification()
      ' qui veniva verificata solamente la modifica delle proprietà delle colonne delle EgtDataGrid

   End Sub

   ' funzione che legge le variabili della macchina dall'INI
   Public Sub LoadVariables()
      VariablesList = New ObservableCollection(Of ConfigVariable)
      Dim VarIndex As Integer = 1
      Dim sValue As String = Nothing
      While EgtUILib.GetPrivateProfileString(S_VARIABLES, VarIndex, String.Empty, sValue, CurrentMachine.sMachIniFile)
         Dim sVariableValues() As String = sValue.Split(","c)
         ' verifico numero minimo di parametri
         If sVariableValues.Count >= 4 Then
            ' cancello spazi
            For Index = 0 To sVariableValues.Count - 1
               sVariableValues(Index) = sVariableValues(Index).Trim()
            Next
            ' creo parametro
            Dim sName As String = sVariableValues(0)
            Dim sVarPath As String = sVariableValues(1)
            Dim sType As String = sVariableValues(3)
            VariablesList.Add(New ConfigVariable(sName, sVarPath, sType))
         End If
         VarIndex += 1
      End While
   End Sub

   ' funzione che scrive i valori delle variabili della macchina nell'INI
   Public Sub WriteVariables()
      Dim VarIndex = 1
      For Each VarItem In VariablesList
         Dim VarStr As String = VarItem.sName & ", " & VarItem.sVarPath & ",  c, " & VarItem.SelType
         WritePrivateProfileString(S_VARIABLES, VarIndex, VarStr, CurrentMachine.sMachIniFile)
         VarIndex += 1
      Next
      ' Se ci sono variabili da rimuovere le rimuovo
      For Index = VarIndex To VarIndex + m_RemovedVarsCount
         WritePrivateProfileString(S_VARIABLES, Index, Nothing, CurrentMachine.sMachIniFile)
      Next
   End Sub

#End Region ' Methods

End Class

Public Class ConfigVariable

   Private m_sName As String
   Public Property sName As String
      Get
         Return m_sName
      End Get
      Set(value As String)
         m_sName = value
      End Set
   End Property

   Private m_sVarPath As String
   Public Property sVarPath As String
      Get
         Return m_sVarPath
      End Get
      Set(value As String)
         m_sVarPath = value
      End Set
   End Property

   Private m_SelType As String
   Public Property SelType As String
      Get
         Return m_SelType
      End Get
      Set(value As String)
         m_SelType = value
      End Set
   End Property

   Private m_TypeList As New ObservableCollection(Of String)({"plc", "cn"})
   Public Property TypeList As ObservableCollection(Of String)
      Get
         Return m_TypeList
      End Get
      Set(value As ObservableCollection(Of String))
         m_TypeList = value
      End Set
   End Property

   Sub New(Name As String, VarPath As String, Type As String)
      m_sName = Name
      m_sVarPath = VarPath
      m_SelType = TypeList.FirstOrDefault(Function(x) x = Type)
   End Sub

End Class

'' Tipo parametro nel file di configurazione Macchina
'Public Enum MachParamType As Integer
'   DOUBLE_ = 1
'   STRING_ = 2
'   COMBO = 3
'   LENGTH = 4
'   CHECKBOX = 5
'End Enum
