Imports System.Windows.Forms
Imports DigitalData.Controls.LookupGrid
Imports DigitalData.Modules.Logging

Namespace Modules
    ''' <summary>
    ''' Patterns for Windream Indicies
    ''' </summary>
    Public Class Windream
        Inherits BaseModule
        Implements IModule

        Public Const WM_VALUE_DOCUMENT = "WM_DOCUMENT"

        Public Property PatternIdentifier As String = "WMI" 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, pWMObject As WINDREAMLib.WMObject) As String
            Dim oResult = pInput
            Dim oCounter = 0

            While ContainsPattern(oResult, PatternIdentifier)
                Try
                    Dim oIndexName As String = GetNextPattern(oResult, PatternIdentifier).Value
                    Dim oWMValue As Object = pWMObject.GetVariableValue(oIndexName)

                    If oWMValue Is Nothing Then
                        Logger.Warn("Value for Index [{0}] is empty and will not be used for replacing. Skipping.")
                        Return oResult
                    End If

                    oResult = ReplacePattern(oResult, PatternIdentifier, oWMValue.ToString)

                Catch ex As Exception
                    Logger.Error(ex)
                    Return oResult
                Finally
                    IncrementCounterOrThrow(oCounter)
                End Try
            End While

            Return oResult
        End Function
    End Class
End Namespace
