Imports Config
Imports NLog

''' <summary>
''' Sample Config Class inheriting from BaseConfig
''' 
''' 
''' 
''' Things this class should do:
''' 
''' - Provide defaults for all values
''' - Load the current config using the LoadConfig method of BaseConfig
'''     - If no configfile was found, it should create a new datatable with only default values
'''     - If a configfile was found, it should merge the values from this file with the defaults from this class
'''         - For each propertyname defined in PropertyNames
'''             - Check for existing value in datatable
'''                 - If a value is present, use it
'''                 - If no value is exists, use the default value
''' - Assign the resulting values to class properties
''' - Save the new config to disk
''' </summary>
Public Class SampleConfig
    Inherits BaseConfig

    Private _logger As Logger

    Public ReadOnly ConnectionString As String
    Public ReadOnly UniversalViewer As String

    Public Overloads ReadOnly Property PropertyNames As Dictionary(Of String, String)
        Get
            Return New Dictionary(Of String, String) From {
                {"ConnectionString", ""},
                {"UniversalViewer", ""}
            }
        End Get
    End Property

    Public Sub New(LogFactory As LogFactory)
        MyBase.New(LogFactory)

        _logger = LogFactory.GetCurrentClassLogger()

        ' Load the existing values from the config file into PropertyNames
        ' overwriting the default values
        Dim oDataTable = LoadConfig()

        For Each oRow As DataRow In oDataTable.Rows
            Dim oValue = oRow.Item(_configValue)
            Dim oKey = oRow.Item(_configKey)

            PropertyNames.Item(oKey) = oValue
        Next

        ' Assign the merged properties to class properties, optionally converting them beforehand
        For Each oProperty As KeyValuePair(Of String, String) In PropertyNames
            Select Case oProperty.Key
                Case "ConnectionString"
                    ConnectionString = oProperty.Value
                Case "UniversalViewer"
                    UniversalViewer = oProperty.Value
                Case Else
                    _logger.Warn("Property {0} was found in PropertyNames but was not assigned to a config property", oProperty.Key)
            End Select
        Next

        ' Convert the dictionary back to a datatable and save it
        SaveConfig(ConvertToDataTable(PropertyNames))
    End Sub
End Class
