Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Windows.Forms
Imports DigitalData.Controls.LookupGrid
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.ZooFlow
Imports WINDREAMLib

''' <summary>
''' Defines common Functions for Checking for and replacing placeholders.
''' This Class also includes a child class `Pattern` for passing around Patterns.
''' 
''' The format of all placeholders is:
''' {#TYPE#VALUE}
''' 
''' Some Examples:
''' {#INT#USERNAME}
''' {#CTRL#CMB_2}
''' {#WMI#String 39}
''' </summary>
Public Class Patterns2
    Private ReadOnly LogConfig As LogConfig
    Private ReadOnly Logger As Logger
    Private ReadOnly Base As Modules.BaseModule
    Private ReadOnly Modules As New List(Of IModule)

    Public Sub New(pLogConfig As LogConfig)
        LogConfig = pLogConfig
        Logger = pLogConfig.GetLogger()
        Base = New Modules.BaseModule(LogConfig)

        Modules.AddRange({
            New Modules.Internal(LogConfig),
            New Modules.Clipboard(LogConfig),
            New Modules.Controls(LogConfig),
            New Modules.User(LogConfig),
            New Modules.FileInformation(LogConfig),
            New Modules.IDB(LogConfig),
            New Modules.Globix.GlobixAutomatic(LogConfig),
            New Modules.Globix.GlobixManual(LogConfig)
        })
    End Sub

    Public Function ReplaceUserValues(pInput As String, pUser As State.UserState) As String
        Try
            Logger.Debug("Replacing User Values")
            Dim oModule = New Modules.User(LogConfig)
            Return oModule.Replace(pInput, pUser)
        Catch ex As Exception
            Logger.Warn("Error occurred while replacing User Values")
            Logger.Error(ex)
            Return pInput
        End Try
    End Function

    Public Function ReplaceFileValues(pInput As String, pFileInfo As FileInfo) As String
        Try
            Logger.Debug("Replacing File Values")
            Dim oModule = New Modules.FileInformation(LogConfig)
            Return oModule.Replace(pInput, pFileInfo)
        Catch ex As Exception
            Logger.Warn("Error occurred while replacing File Values")
            Logger.Error(ex)
            Return pInput
        End Try
    End Function

    Public Function ReplaceControlValues(pInput As String, pPanel As Panel) As String
        Try
            Logger.Debug("Replacing Control Values")
            Dim oModule = New Modules.Controls(LogConfig)
            Return oModule.Replace(pInput, pPanel)
        Catch ex As Exception
            Logger.Warn("Error occurred while replacing Control Values")
            Logger.Error(ex)
            Return pInput
        End Try
    End Function

    Public Function ReplaceWindreamValues(pInput As String, pWMObject As WMObject) As String
        Try
            Logger.Debug("Replacing Windream Values")
            Dim oModule = New Modules.Windream(LogConfig)
            Return oModule.Replace(pInput, pWMObject)
        Catch ex As Exception
            Logger.Warn("Error occurred while replacing Windream Values")
            Logger.Error(ex)
            Return pInput
        End Try
    End Function

    Public Function ReplaceInternalValues(pInput As String, Optional pClipboardContents As String = "") As String
        Logger.Debug("Replacing Internal Values")
        Dim oResult = pInput

        Try
            Dim oInternal = New Modules.Internal(LogConfig)
            Dim oClipboard = New Modules.Clipboard(LogConfig)

            oResult = oInternal.Replace(oResult)
            oResult = oClipboard.Replace(oResult, pClipboardContents)

            Return oResult
        Catch ex As Exception
            Logger.Warn("Error occurred while replacing Internal Values")
            Logger.Error(ex)
            Return oResult
        End Try
    End Function

    Public Function ReplaceGlobixValues(pInput As String, pManualIndexes As Dictionary(Of String, List(Of String)), pAutomaticIndexes As Dictionary(Of String, List(Of String))) As String
        Logger.Debug("Replacing Globix Values")
        Dim oResult = pInput

        Try
            Dim oAutomatic = New Modules.Globix.GlobixAutomatic(LogConfig)
            Dim oManual = New Modules.Globix.GlobixManual(LogConfig)

            oResult = oAutomatic.Replace(oResult, pAutomaticIndexes)
            oResult = oManual.Replace(oResult, pManualIndexes)

            Return oResult
        Catch ex As Exception
            Logger.Warn("Error occurred while replacing Globix Values")
            Logger.Error(ex)
            Return oResult
        End Try
    End Function

#Region "Helper Functions"
    ''' <summary>
    ''' Wraps a pattern-type and -value in the common format: {#type#value}
    ''' </summary>
    Public Function WrapPatternValue(pType As String, pValue As String) As String
        Return New Pattern(pType, pValue).ToString
    End Function

    Public Function HasAnyPatterns(input) As Boolean
        Return Modules.Any(Function(m) Base.HasPattern(input, m.PatternIdentifier))
    End Function

    Public Function HasComplexPatterns(input As String) As Boolean
        Return Modules.
            Where(Function(m) m.IsComplex = True).
            Any(Function(m) Base.HasPattern(input, m.PatternIdentifier))
    End Function

    Public Function HasOnlySimplePatterns(input As String) As Boolean
        Return Not HasComplexPatterns(input)
    End Function
#End Region

End Class