Imports System.Collections.ObjectModel
Imports EgtUILib
Imports EgtWPFLib5

Friend Module MachParamIniFile

   Public Function GetMachPrivateProfileParam(IpParentTable As Integer, IpMachParamIndex As String, ByRef IpMachParam As MachParam, TableName As String, bTs3dataLoaded As Boolean) As Boolean
      Dim sMachParam As String = String.Empty
      If EgtUILib.GetPrivateProfileString(IpParentTable, IpMachParamIndex, String.Empty, sMachParam, sMachParamIniFile) > 0 Then
         Dim sMachParamParams() As String = sMachParam.Split(","c)
         ' verifico numero minimo di parametri
         If sMachParamParams.Count >= 4 Then
            ' cancello spazi
            For Index = 0 To sMachParamParams.Count - 1
               sMachParamParams(Index) = sMachParamParams(Index).Trim()
            Next
            ' creo parametro
            If sMachParamParams(0) = "d" OrElse sMachParamParams(0) = "l" Then
               Dim dDefault As Double
               StringToDoubleAdv(sMachParamParams(2), dDefault)
               Dim ParamType As MachParamType
               If sMachParamParams(0) = "d" Then
                  ParamType = MachParamType.DOUBLE_
               Else
                  ParamType = MachParamType.LENGTH
               End If
               If bTs3dataLoaded Then
                  Dim dTs3DataValue As Double
                  If EgtLuaGetGlobNumVar(TableName & "." & sMachParamParams(1), dTs3DataValue) Then
                     dDefault = dTs3DataValue
                  End If
               End If
               IpMachParam = New MachParam(IpParentTable, IpMachParamIndex, ParamType, sMachParamParams(1), dDefault, sMachParamParams(3))
               Return Not IsNothing(IpMachParam)
            ElseIf sMachParamParams(0) = "s" Then
               IpMachParam = New MachParam(IpParentTable, IpMachParamIndex, MachParamType.STRING_, sMachParamParams(1), sMachParamParams(3))
               Return Not IsNothing(IpMachParam)
            End If
         End If
      End If
      Return False
   End Function

   Public Function GetMachPrivateProfileString(IpAppName As String, IpKeyName As String, IpDefault As String, ByRef IpString As String) As Integer
      Return EgtUILib.GetPrivateProfileString(IpAppName, IpKeyName, IpDefault, IpString, sMachParamIniFile)
   End Function

   Public Function GetMachPrivateProfileParamValue(IpParentTable As Integer, IpMachParamIndex As String, ByRef IpMachParamValue As String) As Boolean
      Dim sMachParam As String = String.Empty
      If EgtUILib.GetPrivateProfileString(IpParentTable, IpMachParamIndex, String.Empty, sMachParam, sMachParamIniFile) > 0 Then
         Dim sMachParamParams() As String = sMachParam.Split(","c)
         ' verifico numero minimo di parametri
         If sMachParamParams.Count >= 4 Then
            ' cancello spazi
            For Index = 0 To sMachParamParams.Count - 1
               sMachParamParams(Index) = sMachParamParams(Index).Trim()
            Next
            'restituisco il valore del parametro
            IpMachParamValue = sMachParamParams(2)
            Return Not IsNothing(IpMachParamValue)
         End If
      End If
      Return False
   End Function

   Public Function WriteMachPrivateProfileString(IpAppName As String, IpKeyName As String, ByRef IpString As String) As Boolean
      Return EgtUILib.WritePrivateProfileString(IpAppName, IpKeyName, IpString, sMachParamIniFile)
   End Function

End Module