Public Class ParametriSetup


    Private _ElencoParametri As DataSet
    Private _NomeFileSetup As String

    Public Sub New(ByVal FileSetup As String)
        _ElencoParametri = New DataSet
        _NomeFileSetup = FileSetup

        If FileSetup <> "" Then
            CaricaParametriSetup()
        End If
    End Sub

    Public Sub New()
        _ElencoParametri = New DataSet
        _NomeFileSetup = ""
    End Sub

    Public Sub CaricaParametriSetup()
        _ElencoParametri = LeggiXmlDataSet("Setup", _NomeFileSetup, "Nome")
    End Sub

    Public Sub SalvaParametriSetup()
        If System.IO.File.Exists(_NomeFileSetup) = True Then
            Kill(_NomeFileSetup)
        End If
        _ElencoParametri.WriteXml(_NomeFileSetup)
    End Sub

    Public Function LeggiParametroString(ByVal NomeParametro As String) As String
        Dim Risposta As String = ""

        Try
            Dim LElenco As DataRow() = _ElencoParametri.Tables("Setup").Select("Nome='" & NomeParametro & "'")

            Dim LaRiga As DataRow
            For Each LaRiga In LElenco
                Risposta = LaRiga("Valore").ToString
            Next
        Catch
            Risposta = ""
        End Try

        Return Risposta
    End Function

    Public Function LeggiParametroBoolean(ByVal NomeParametro As String) As Boolean
        Dim Risposta As String = ""

        Try
            Dim LElenco As DataRow() = _ElencoParametri.Tables("Setup").Select("Nome='" & NomeParametro & "'")

            Dim LaRiga As DataRow
            For Each LaRiga In LElenco
                Risposta = LaRiga("Valore").ToString
            Next
        Catch
            Risposta = ""
        End Try

        Select Case Risposta.ToUpper
            Case "TRUE", "OK", "SI", "1", "YES", "VERO"
                Return True
            Case Else
                Return False
        End Select
    End Function

    Public Sub AggiornaParametro(ByVal NomeParametro As String, ByVal ValoreParametro As Object)
        Try
            If _ElencoParametri.Tables("Setup") Is Nothing Then
                Dim TabellaTmp As New DataTable("Setup")
                Dim RigaTmp As DataRow

                Dim LaColonna As DataColumn
                LaColonna = TabellaTmp.Columns.Add("Nome", System.Type.GetType("System.String"))
                LaColonna = TabellaTmp.Columns.Add("Valore", System.Type.GetType("System.String"))

                '* Aggiunge alla tabella tutte le righe
                RigaTmp = TabellaTmp.NewRow
                RigaTmp("Nome") = NomeParametro
                RigaTmp("Valore") = ValoreParametro
                TabellaTmp.Rows.Add(RigaTmp)

                _ElencoParametri.Tables.Add(TabellaTmp)
            Else
                Dim LElenco As DataRow() = _ElencoParametri.Tables("Setup").Select("Nome='" & NomeParametro & "'")

                If LElenco.Length = 0 Then
                    Dim LaRiga As DataRow
                    LaRiga = _ElencoParametri.Tables("Setup").NewRow
                    LaRiga("Nome") = NomeParametro
                    LaRiga("Valore") = ValoreParametro
                    _ElencoParametri.Tables("Setup").Rows.Add(LaRiga)
                Else
                    LElenco(0).Item("Valore") = ValoreParametro
                End If
            End If
        Catch

        End Try
    End Sub


    Private Function LeggiXmlDataTable(ByVal NomeTabella As String, ByVal NomeFileXml As String, Optional ByVal NomeColonnaChiave As String = "") As DataTable
        '* Crea e Legge il dataset dal file xml
        Dim DataSetXml As New System.Data.DataSet
        DataSetXml.ReadXml(NomeFileXml)

        '* Aggiunge il campo chiave
        If NomeColonnaChiave <> "" Then
            DataSetXml.Tables(NomeTabella).Constraints.Add(NomeColonnaChiave, DataSetXml.Tables(NomeTabella).Columns(NomeColonnaChiave), True)
        End If

        '* Restituisce la risposta
        Return DataSetXml.Tables(NomeTabella)
    End Function

    Private Shared Function LeggiXmlDataSet(ByVal NomeTabella As String, ByVal NomeFileXml As String, Optional ByVal NomeColonnaChiave As String = "") As DataSet
        '* Crea e Legge il dataset dal file xml
        Dim DataSetXml As New System.Data.DataSet
        DataSetXml.ReadXml(NomeFileXml)

        '* Aggiunge il campo chiave
        If NomeColonnaChiave <> "" Then
            DataSetXml.Tables(NomeTabella).Constraints.Add(NomeColonnaChiave, DataSetXml.Tables(NomeTabella).Columns(NomeColonnaChiave), True)
        End If

        '* Restituisce la risposta
        Return DataSetXml
    End Function




    Public Property NomeFileSetup() As String
        Get
            Return _NomeFileSetup
        End Get
        Set(ByVal Value As String)
            _NomeFileSetup = Value
        End Set
    End Property


End Class
