Imports DigitalData.Modules.Logging

Namespace Modules
    ''' <summary>
    ''' Simple patterns that only rely on .NET functions
    ''' </summary>
    Public Class Internal
        Inherits BaseModule
        Implements IModule

        Public Const INT_VALUE_USERNAME = "USERNAME"
        Public Const INT_VALUE_MACHINE = "MACHINE"
        Public Const INT_VALUE_DOMAIN = "DOMAIN"
        Public Const INT_VALUE_DATE = "DATE"

        Public Const INT_VALUE_FILENAME_EXT = "FILENAME_EXT"
        Public Const INT_VALUE_FILENAME = "FILENAME_ONLY"
        Public Const INT_VALUE_FILE_DATE = "FILE_DATE"
        Public Const INT_VALUE_FILEEXT = "FILE_EXT"

        Public Const INT_VALUE_DATE_YYYY = "YYYY"
        Public Const INT_VALUE_DATE_MM = "MM"
        Public Const INT_VALUE_DATE_DD = "DD"
        Public Const INT_VALUE_DATE_YYYY_MM_DD = "YYYY/MM/DD"
        Public Const INT_VALUE_DATE_YYYY_MM_DD_2 = "YYYY_MM_DD"

        Public Property PatternIdentifier As String = "INT" Implements IModule.PatternIdentifier
        Public Property IsComplex As Boolean = False Implements IModule.IsComplex

        Public Sub New(pLogConfig As LogConfig)
            MyBase.New(pLogConfig)
        End Sub

        Public Function Replace(pInput As String) As String
            Dim oResult = pInput
            Dim oCounter = 0
            Dim oNow As Date = Now

            'TODO: Make date patterns dynamic

            Logger.Trace("Replacing Internal Patterns")

            ' Replace CurrentDate(s)
            While ContainsPatternAndValue(oResult, PatternIdentifier, INT_VALUE_DATE_YYYY_MM_DD)
                Logger.Trace("Replacing Pattern [{0}]", INT_VALUE_DATE_YYYY_MM_DD)
                oResult = ReplacePattern(oResult, PatternIdentifier, oNow.ToString("yyyy\\MM\\dd"))
                IncrementCounterOrThrow(oCounter)
            End While

            Logger.Trace("Replace Counter: [{0}]", oCounter)

            ' Replace CurrentDate(s)
            While ContainsPatternAndValue(oResult, PatternIdentifier, INT_VALUE_DATE_YYYY_MM_DD_2)
                Logger.Trace("Replacing Pattern [{0}]", INT_VALUE_DATE_YYYY_MM_DD_2)
                oResult = ReplacePattern(oResult, PatternIdentifier, oNow.ToString("yyyy_MM_dd"))
                IncrementCounterOrThrow(oCounter)
            End While

            Logger.Trace("Replace Counter: [{0}]", oCounter)

            ' Replace Year(s)
            While ContainsPatternAndValue(oResult, PatternIdentifier, INT_VALUE_DATE_YYYY)
                Logger.Trace("Replacing Pattern [{0}]", INT_VALUE_DATE_YYYY)
                oResult = ReplacePattern(oResult, PatternIdentifier, oNow.ToString("yyyy"))
                IncrementCounterOrThrow(oCounter)
            End While

            Logger.Trace("Replace Counter: [{0}]", oCounter)

            ' Replace Month(s)
            While ContainsPatternAndValue(oResult, PatternIdentifier, INT_VALUE_DATE_MM)
                Logger.Trace("Replacing Pattern [{0}]", INT_VALUE_DATE_MM)
                oResult = ReplacePattern(oResult, PatternIdentifier, oNow.ToString("MM"))
                IncrementCounterOrThrow(oCounter)
            End While

            Logger.Trace("Replace Counter: [{0}]", oCounter)

            ' Replace Day(s)
            While ContainsPatternAndValue(oResult, PatternIdentifier, INT_VALUE_DATE_DD)
                Logger.Trace("Replacing Pattern [{0}]", INT_VALUE_DATE_DD)
                oResult = ReplacePattern(oResult, PatternIdentifier, oNow.ToString("dd"))
                IncrementCounterOrThrow(oCounter)
            End While

            Logger.Trace("Replace Counter: [{0}]", oCounter)

            Return oResult
        End Function
    End Class
End Namespace
