Imports System.IO
Imports System.Xml
Imports DD_LIB_Standards
Module ModuleMySettings
    Public PATH_FileExclusions As String = Path.Combine(Application.UserAppDataPath(), "FileExclusions.xml")
    Dim ConfigPath As String = Path.Combine(Application.UserAppDataPath(), "UserConfig.xml")

    Public DTEXCLUDE_FILES As DataTable
    Public USER_USERNAME As String = ""
    Public USER_GUID As Integer
    Public USER_LANGUAGE As String = "de-DE"

    Public MyConnectionString As String = ""
    Public MyServer_UpdatePath As String = ""
    Public VERSION_SERVER = "1.0.0.0"
    Public VERSION_USER = "1.0.0.0"
    Public FORCE_UPDATE As Boolean = False
    Public VERSIONS_FOR_FORCE_UPDATE As String = ""
    Public FOLDER_TEMP As String
    Public LogErrorsOnly As Boolean = True
    Public MY_INSTALL_PATH As String
    Public DT_UPDATE_ITEMS As DataTable
    Public ERROR_WHILE_UPDATING As Boolean = False

    Public Function LoadFileExclusion()
        Dim rowresult As String = ""
        Try
            'if file doesn't exist, create the file with its default xml table
            If Not File.Exists(path_FileExclusions) Then
                DTEXCLUDE_FILES = CreateExclusionTable()
                DTEXCLUDE_FILES.WriteXml(path_FileExclusions)
            End If
            DTEXCLUDE_FILES = GetTablefromXML(PATH_FileExclusions)

            'For Each Row As DataRow In DT.Rows
            '    rowresult &= Row.Item("FILE_CONTAIN")
            '    Select Case Row.Item("FILE_CONTAIN")

            '    End Select

            'Next
            Return True
        Catch ex As Exception
            MsgBox("Error in LoadFileExclusion" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return False
        End Try
        Return True

    End Function
    Private Function CreateExclusionTable() As DataTable
        Try
            ' Create sample Customers table, in order
            ' to demonstrate the behavior of the DataTableReader.
            Dim table As New DataTable
            table.TableName = "TBEXCLUSION"

            ' Create two columns, ID and Name.
            table.Columns.Add("FILE_CONTAIN", GetType(System.String))
            Dim newRow As DataRow = table.NewRow()
            newRow("FILE_CONTAIN") = "Thumbs"
            table.Rows.Add(newRow)
            Dim newRow1 As DataRow = table.NewRow()
            newRow1("FILE_CONTAIN") = "\~$"
            table.Rows.Add(newRow1)
            Dim newRow2 As DataRow = table.NewRow()
            newRow2("FILE_CONTAIN") = ".db"
            table.Rows.Add(newRow2)
            Dim newRow3 As DataRow = table.NewRow()
            newRow3("FILE_CONTAIN") = "desktop.ini"
            table.Rows.Add(newRow3)
            table.AcceptChanges()
            Return table
        Catch ex As Exception
            MsgBox("Error in CreateExclusionTable" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try
    End Function
    Public Function MySettings_Load()
        Try
            Dim rowresult As String = ""
            Dim DT As DataTable
            ConfigPath = ConfigPath.Replace("VERSION_CHECKER\VersionCheck", "Digital Data\ADDI - Akte der Dinge")
            'if file doesn't exist, create the file with its default xml table
            If Not File.Exists(ConfigPath) Then
                Return False
            End If
            DT = GetTablefromXML(ConfigPath)
            If DT Is Nothing Then
                MsgBox("Configuration could not be loaded!! Check LogFile!", MsgBoxStyle.Critical)
                Return False
            End If
            For Each Row As DataRow In DT.Rows
                rowresult &= Row.Item("ConfigName")
                Select Case Row.Item("ConfigName")
                    Case "MyConnectionString"
                        Dim connstring As String
                        'Den ConnectonString mit verschlüsseltem PW laden
                        Dim csb As New SqlClient.SqlConnectionStringBuilder
                        csb.ConnectionString = Row.Item("Value")
                        If Not csb.ConnectionString = "" Then
                            If csb.ConnectionString.Contains("Password=") Then
                                'sa-
                                'Jetzt das Passwort entschlüsseln
                                Dim PWplainText As String
                                Dim wrapper As New clsEncryption("!35452didalog=")
                                ' DecryptData throws if the wrong password is used.
                                Try
                                    PWplainText = wrapper.DecryptData(csb.Password)
                                    connstring = Row.Item("Value").ToString.Replace(csb.Password, PWplainText)
                                Catch ex As Exception
                                    ClassLogger.Add("- the Password '" & csb.Password & "' could not be decrypted", False)
                                    connstring = ""
                                End Try

                            Else
                                'Windows-Auth
                                connstring = Row.Item("Value").ToString
                            End If

                            MyConnectionString = connstring
                        Else
                            MyConnectionString = ""
                        End If
                    Case "LogErrorsOnly"
                        LogErrorsOnly = CBool(Row.Item("Value"))
                        clsCURRENT.LOG_ERRORS_ONLY = LogErrorsOnly
                End Select
            Next
           LoadFileExclusion()
        Catch ex As Exception
            MsgBox("Error in LoadMyConfig" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            ClassLogger.Add("Error in LoadMyConfig: " & ex.Message, True)
            Return False
        End Try
        Return True

    End Function
    Private Function GetTablefromXML(path As String)
        Try
            Dim DS As New DataSet
            DS.ReadXml(path)
            Return DS.Tables(0)
        Catch ex As Exception
            MsgBox("Error in GetTablefromXML" & vbNewLine & ex.Message & vbNewLine & "ConfigPath: " & vbNewLine & path, MsgBoxStyle.Critical)
            ClassLogger.Add("Error in GetTablefromXML: " & ex.Message, True)
            ClassLogger.Add(">> ConfigPath: " & ConfigPath, False)
            Return Nothing
        End Try

    End Function
End Module
