Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Base

Public Class ClassDBConfig
    Public Class Config
        Public Property PathError As String
        Public Property PathAttachments As String
        Public Property BodyFont As String
        Public Property WindreamConnectionString As String
        Public Property TimerInterval As Integer
        Public Property WindreamDrive As String = "W"
    End Class

    Private ReadOnly Logger As Logger
    Private ReadOnly Database As MSSQLServer

    Public Sub New(pLogConfig As LogConfig, pDatabase As MSSQLServer)
        Logger = pLogConfig.GetLogger()
        Database = pDatabase
    End Sub

    Private Function GetConfigTable() As DataTable
        Try
            Logger.Debug("Getting Config Table..")
            Dim oSQL As String = "SELECT * FROM TBEMLP_CONFIG"
            Dim oTable As DataTable = Database.GetDatatable(oSQL)
            Return oTable
        Catch ex As Exception
            Logger.Error(ex)
            Return Nothing
        End Try
    End Function

    Private Function GetBaseConfigTable() As DataTable
        Try
            Logger.Debug("Getting Base Config Table..")
            Dim oSQL As String = "SELECT * FROM TBDD_BASECONFIG"
            Dim oTable As DataTable = Database.GetDatatable(oSQL)
            Return oTable
        Catch ex As Exception
            Logger.Error(ex)
            Return Nothing
        End Try
    End Function

    Public Function GetConfig() As Config
        Try
            Dim oConfigTable = GetConfigTable()
            Dim oBaseTable = GetBaseConfigTable()

            If oBaseTable Is Nothing Then
                Logger.Warn("Config from TBDD_BASECONFIG could not be loaded!")
                Return Nothing
            End If

            If oBaseTable.Rows.Count = 0 Then
                Logger.Warn("Config from TBDD_BASECONFIG could not be loaded!")
                Return Nothing
            End If

            If oConfigTable Is Nothing Then
                Logger.Warn("Config from TBEMLP_CONFIG could not be loaded!")
                Return Nothing
            End If

            If oConfigTable.Rows.Count = 0 Then
                Logger.Warn("Config from TBEMLP_CONFIG is empty!")
                Return Nothing
            End If

            Dim oRow As DataRow = oConfigTable.Rows.Item(0)

            Dim oConfig As New Config With {
                .PathAttachments = oRow.ItemEx("PATH_EMAIL_TEMP", ""),
                .PathError = oRow.ItemEx("PATH_EMAIL_ERRORS", ""),
                .BodyFont = oRow.ItemEx("FONT_BODY", "Arial"),
                .TimerInterval = oRow.ItemEx("CHECK_INTERVALL_MINUTES", 5),
                .WindreamConnectionString = oRow.ItemEx("WM_CON_STRING", ""),
                .WindreamDrive = oRow.ItemEx("WM_DRIVE", "W")
            }

            Return oConfig
        Catch ex As Exception
            Logger.Error(ex)
            Return Nothing
        End Try
    End Function
End Class
