Imports System.Text
Imports System.Text.RegularExpressions

Module ModuleHelpers
    Public Function encode_utf8(ByVal str As String) As String
        Try
            'supply True as the construction parameter to indicate
            'that you wanted the class to emit BOM (Byte Order Mark)
            'NOTE: this BOM value is the indicator of a UTF-8 string
            Dim utf8Encoding As New System.Text.UTF8Encoding(True)
            Dim encodedString() As Byte
            encodedString = utf8Encoding.GetBytes(str)
            Return utf8Encoding.GetString(encodedString)
        Catch ex As Exception
            MsgBox("Unexpected error in encode_utf8: " & ex.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try

    End Function
    Public Function StringAsUtf8Bytes(ByVal strData As String) As Byte()
        Try
            Dim bytes() As Byte
            ' get unicode string as bytes
            bytes = Encoding.UTF8.GetBytes(strData)
            ' return byte data
            Return bytes
        Catch ex As Exception
            MsgBox("Unexpected error in StringAsUtf8Bytes: " & ex.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try

    End Function

    Public Function GetConnectionString(id As Integer)
        Dim connectionString As String = ""
        Try
            Dim oSQL = "SELECT * FROM TBDD_CONNECTION WHERE GUID = " & id
            Dim DTConnection As DataTable = My.Database.GetDatatable("TBDD_CONNECTION", oSQL, Modules.EDMI.API.Constants.DatabaseType.ECM, $"GUID = {id}")


            If DTConnection.Rows.Count = 1 Then
                Dim CSType = DTConnection.Rows(0).Item("SQL_PROVIDER").ToString.ToUpper
                Select Case CSType
                    Case "MS-SQL".ToUpper
                        If DTConnection.Rows(0).Item("USERNAME").ToString.ToLower = "winauth" Then
                            connectionString = "Data Source=" & DTConnection.Rows(0).Item("SERVER") & ";Initial Catalog= " & DTConnection.Rows(0).Item("DATENBANK") & ";Trusted_Connection=True;"
                        Else
                            connectionString = "Data Source=" & DTConnection.Rows(0).Item("SERVER") & ";Initial Catalog= " & DTConnection.Rows(0).Item("DATENBANK") & ";User Id=" & DTConnection.Rows(0).Item("USERNAME") & ";Password=" & DTConnection.Rows(0).Item("PASSWORD") & ";"
                        End If
                    Case "MS-SQLServer".ToUpper
                        If DTConnection.Rows(0).Item("USERNAME").ToString.ToLower = "winauth" Then
                            connectionString = "Data Source=" & DTConnection.Rows(0).Item("SERVER") & ";Initial Catalog= " & DTConnection.Rows(0).Item("DATENBANK") & ";Trusted_Connection=True;"
                        Else
                            connectionString = "Data Source=" & DTConnection.Rows(0).Item("SERVER") & ";Initial Catalog= " & DTConnection.Rows(0).Item("DATENBANK") & ";User Id=" & DTConnection.Rows(0).Item("USERNAME") & ";Password=" & DTConnection.Rows(0).Item("PASSWORD") & ";"
                        End If

                    Case "Oracle".ToUpper
                        If DTConnection.Rows(0).Item("BEMERKUNG").ToString.Contains("without tnsnames") Then
                            connectionString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" & DTConnection.Rows(0).Item("SERVER") & ")(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=" &
                               DTConnection.Rows(0).Item("DATENBANK") & ")));User Id=" & DTConnection.Rows(0).Item("USERNAME") & ";Password=" & DTConnection.Rows(0).Item("PASSWORD") & ";"
                        Else
                            connectionString = "Data Source=" & DTConnection.Rows(0).Item("SERVER") & ";Persist Security Info=True;User Id=" & DTConnection.Rows(0).Item("USERNAME") & ";Password=" & DTConnection.Rows(0).Item("PASSWORD") & ";Unicode=True"
                        End If


                    Case Else
                        MsgBox("ConnectionType not integrated", MsgBoxStyle.Critical, "Please check connection:")
                End Select
            End If

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error in GetConnectionString:")

        End Try
        Return connectionString
    End Function
End Module
