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

Public Module DataGridColumnsIniFile

   Public m_sDataGridColumnsIniFile As String

   ' funzione per ottenere dal file INI le colonne da caricare nelle EgtDataGrid del programma
   Public Function GetPrivateProfileColumns(ParentDGName As String, ByRef ocColumns As ObservableCollection(Of EgtDataGridColumn)) As Boolean
      Dim colIndex As Integer = 0
      Dim str = String.Empty
      While EgtUILib.GetPrivateProfileString(ParentDGName, colIndex, String.Empty, str, m_sDataGridColumnsIniFile) > 0
         Dim sColumnParams() As String = str.Split(","c)
         ' verifico numero minimo di parametri
         If sColumnParams.Count >= 9 Then
            ' cancello spazi
            For index = 0 To sColumnParams.Count - 1
               sColumnParams(index) = sColumnParams(index).Trim()
            Next
            ' creo colonna
            Dim sName = sColumnParams(0)
            Dim bReorder As Boolean = sColumnParams(1).Equals("1")
            Dim bResize As Boolean = sColumnParams(2).Equals("1")
            Dim bSort As Boolean = sColumnParams(3).Equals("1")
            Dim bIsReadOnly As Boolean = sColumnParams(4).Equals("1")
            Dim Width As Double
            Dim WidthType As DataGridLengthUnitType
            StringToDoubleAdv(sColumnParams(5), Width)
            Integer.TryParse(sColumnParams(6), WidthType)
            Dim bVisible As Boolean = sColumnParams(7).Equals("1")
            Dim bCanUserEditVisible As Boolean = sColumnParams(8).Equals("1")
            Dim nSortDirection As SortOrder = If(sColumnParams.Count >= 10, CInt(sColumnParams(9)), 0)
            ocColumns.Add(New EgtDataGridColumn(ParentDGName, sName, bReorder, bResize, bSort, bIsReadOnly, New DataGridLength(Width, WidthType), bVisible, bCanUserEditVisible, nSortDirection))
         End If
         colIndex += 1
      End While
      Return ocColumns.Count > 0
   End Function

   Public Function WriteColumnPrivateProfileParam(ParentDataGridName As String, DisplayIndex As String, sColumnParams As String) As Boolean
      Return WritePrivateProfileString(ParentDataGridName, DisplayIndex, sColumnParams, m_sDataGridColumnsIniFile)
   End Function

   ' funzione per ottenere dal file INI le colonne da caricare nelle EgtDataGrid del programma
   Public Function GetPrivateProfileColumn(ParentDGName As String, ByRef Column As EgtDataGridColumn) As Boolean
      Dim colIndex As Integer = 0
      Dim str = String.Empty
      While EgtUILib.GetPrivateProfileString(ParentDGName, colIndex, String.Empty, str, m_sDataGridColumnsIniFile) > 0
         Dim sColumnParams() As String = str.Split(","c)
         ' verifico numero minimo di parametri
         If sColumnParams.Count >= 9 Then
            If Column.Name = sColumnParams(0) Then
               ' cancello spazi
               For index = 0 To sColumnParams.Count - 1
                  sColumnParams(index) = sColumnParams(index).Trim()
               Next
               ' creo colonna
               Dim sName = sColumnParams(0)
               Dim bReorder As Boolean = sColumnParams(1).Equals("1")
               Dim bResize As Boolean = sColumnParams(2).Equals("1")
               Dim bSort As Boolean = sColumnParams(3).Equals("1")
               Dim bIsReadOnly As Boolean = sColumnParams(4).Equals("1")
               Dim Width As Double
               Dim WidthType As DataGridLengthUnitType
               StringToDoubleAdv(sColumnParams(5), Width)
               Integer.TryParse(sColumnParams(6), WidthType)
               Dim bVisible As Boolean = sColumnParams(7).Equals("1")
               Dim bCanUserEditVisible As Boolean = sColumnParams(8).Equals("1")
               Dim nSortDirection As SortOrder = If(sColumnParams.Count >= 10, CInt(sColumnParams(9)), 0)
               Column = New EgtDataGridColumn(ParentDGName, sName, bReorder, bResize, bSort, bIsReadOnly, New DataGridLength(Width, WidthType), bVisible, bCanUserEditVisible, nSortDirection)
               Exit While
            End If
         End If
         colIndex += 1
      End While
      Return Not IsNothing(Column)
   End Function

End Module