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

Public Module DimensionsIniFile

   Public m_sDimensionsIniFile As String

   Public Sub ReadGridDimensions(GridName As String, ByRef GridDimsList As ObservableCollection(Of GridDimension))
      GridDimsList = New ObservableCollection(Of GridDimension)
      Dim Index As Integer = 0
      Dim sValue = String.Empty
      While GetPrivateProfileString(GridName, Index, String.Empty, sValue, m_sDimensionsIniFile) > 0
         Dim sValueParams() As String = sValue.Split(","c)
         ' verifico numero minimo di parametri
         If sValueParams.Count >= 4 Then
            ' cancello spazi
            For I = 0 To sValueParams.Count - 1
               sValueParams(I) = sValueParams(I).Trim()
            Next
            ' creo valore
            Dim nDimIndex As Integer
            Dim nDimType As DimensionType
            Dim GridLenValue As Double
            Dim GridLenType As GridUnitType
            Integer.TryParse(sValueParams(0), nDimIndex)
            Integer.TryParse(sValueParams(1), nDimType)
            StringToDoubleAdv(sValueParams(2), GridLenValue)
            Integer.TryParse(sValueParams(3), GridLenType)
            GridDimsList.Add(New GridDimension(GridName, nDimIndex, nDimType, New GridLength(GridLenValue, GridLenType)))
         End If
         Index += 1
      End While
      For Each GridDimsItem In GridDimsList
         GridDimsItem.GridDimensions = GridDimsList
      Next
   End Sub

   ' funzione per calcolare e scrivere la stringa dei valori delle righe/colonne della Grid nell'INI
   Public Function SaveGridDimensions(GridName As String, GridDims As ObservableCollection(Of GridDimension)) As Boolean
      Dim bOk As Boolean
      Dim Index = 0
      Dim sValue = String.Empty
      For Each GridDimsItem In GridDims
         sValue = GridDimsItem.Index & "," & GridDimsItem.DimType & "," & DoubleToString(GridDimsItem.GridLen.Value, 6) & "," & GridDimsItem.GridLen.GridUnitType
         bOk = WritePrivateProfileDimension(GridName, Index, sValue)
         Index += 1
      Next
      Return bOk
   End Function

   Public Function WritePrivateProfileDimension(GridName As String, Index As String, sValue As String) As Boolean
      Return WritePrivateProfileString(GridName, Index, sValue, m_sDimensionsIniFile)
   End Function

   Private Function GetGridLengthFromIndexAndType(GridDimsList As List(Of GridDimension), Index As Integer, DimType As DimensionType) As GridDimension
      Return GridDimsList.FirstOrDefault(Function(x) x.Index = Index And x.DimType = DimType)
   End Function

End Module
