Imports System.IO
Imports DigitalData.Modules.Logging

Namespace Modules
    Public Class FileInformation
        Inherits BaseModule
        Implements IModule

        Public Const FILE_VALUE_FILEINFO = "FILEINFO"
        Public Const FILE_VALUE_FILENAME = "FILENAME"
        Public Const FILE_VALUE_EXTENSION = "EXTENSION"
        Public Const FILE_VALUE_FILENAME_EXT = "FILENAME_EXT"
        Public Const FILE_VALUE_DATE_CREATED = "DATE_CREATED"
        Public Const FILE_VALUE_DATE_MODIFIED = "DATE_MODIFIED"

        Public Property PatternIdentifier As String = "FILE" 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, pFileInfo As FileInfo) As String
            Dim oResult = pInput
            Dim oCounter = 0

            ' Replace Filename without extension
            While ContainsPatternAndValue(oResult, PatternIdentifier, FILE_VALUE_FILENAME)
                Dim oFilenameWithoutExtension = Path.GetFileNameWithoutExtension(pFileInfo.Name)
                oResult = ReplacePattern(oResult, PatternIdentifier, oFilenameWithoutExtension)
                IncrementCounterOrThrow(oCounter)
            End While

            ' Replace Filename with extension
            While ContainsPatternAndValue(oResult, PatternIdentifier, FILE_VALUE_FILENAME_EXT)
                Dim oFilename As String = pFileInfo.Name
                oResult = ReplacePattern(oResult, PatternIdentifier, oFilename)
                IncrementCounterOrThrow(oCounter)
            End While

            ' Replace Extension
            While ContainsPatternAndValue(oResult, PatternIdentifier, FILE_VALUE_FILENAME_EXT)
                Dim oExtension As String = pFileInfo.Extension.Substring(1)
                oResult = ReplacePattern(oResult, PatternIdentifier, oExtension)
                IncrementCounterOrThrow(oCounter)
            End While

            ' Replace creation date
            While ContainsPatternAndValue(oResult, PatternIdentifier, FILE_VALUE_DATE_CREATED)
                Dim oDateCreated = pFileInfo.CreationTime.ToString("yyyy-MM-dd")
                oResult = ReplacePattern(oResult, PatternIdentifier, oDateCreated)
                IncrementCounterOrThrow(oCounter)
            End While

            ' Replace last modification date
            While ContainsPatternAndValue(oResult, PatternIdentifier, FILE_VALUE_DATE_CREATED)
                Dim oDateModified = pFileInfo.LastWriteTime.ToString("yyyy-MM-dd")
                oResult = ReplacePattern(oResult, PatternIdentifier, oDateModified)
                IncrementCounterOrThrow(oCounter)
            End While

            Return oResult
        End Function
    End Class

End Namespace