Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.ZooFlow

Namespace Modules
    Public Class User
        Inherits BaseModule
        Implements IModule

        Public Const USER_VALUE_PRENAME = "PRENAME"
        Public Const USER_VALUE_SURNAME = "SURNAME"
        Public Const USER_VALUE_EMAIL = "EMAIL"
        Public Const USER_VALUE_SHORTNAME = "SHORTNAME"
        Public Const USER_VALUE_LANGUAGE = "LANGUAGE"
        Public Const USER_VALUE_USER_ID = "USER_ID"
        Public Const USER_VALUE_USER_NAME = "USER_NAME"

        Public Property PatternIdentifier As String = "USER" Implements IModule.PatternIdentifier
        Public Property IsComplex As Boolean = True Implements IModule.IsComplex

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

        Public Function Replace(pInput As String, pUser As State.UserState) As String
            Dim oResult = pInput
            Dim oCounter = 0

            While ContainsPatternAndValue(oResult, PatternIdentifier, USER_VALUE_PRENAME)
                oResult = ReplacePattern(oResult, PatternIdentifier, pUser.GivenName)
                IncrementCounterOrThrow(oCounter)
            End While

            While ContainsPatternAndValue(oResult, PatternIdentifier, USER_VALUE_SURNAME)
                oResult = ReplacePattern(oResult, PatternIdentifier, pUser.Surname)
                IncrementCounterOrThrow(oCounter)
            End While

            While ContainsPatternAndValue(oResult, PatternIdentifier, USER_VALUE_EMAIL)
                oResult = ReplacePattern(oResult, PatternIdentifier, pUser.Email)
                IncrementCounterOrThrow(oCounter)
            End While

            While ContainsPatternAndValue(oResult, PatternIdentifier, USER_VALUE_SHORTNAME)
                oResult = ReplacePattern(oResult, PatternIdentifier, pUser.ShortName)
                IncrementCounterOrThrow(oCounter)
            End While

            While ContainsPatternAndValue(oResult, PatternIdentifier, USER_VALUE_LANGUAGE)
                oResult = ReplacePattern(oResult, PatternIdentifier, pUser.Language)
                IncrementCounterOrThrow(oCounter)
            End While

            While ContainsPatternAndValue(oResult, PatternIdentifier, USER_VALUE_USER_ID)
                oResult = ReplacePattern(oResult, PatternIdentifier, pUser.UserId)
                IncrementCounterOrThrow(oCounter)
            End While

            Return oResult
        End Function
    End Class
End Namespace
