Imports System.Text.RegularExpressions
Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Logging

Public Class ClassPostprocessing
    Inherits BaseClass

    Public Const VBSPLIT = "VBSPLIT"
    Public Const VBREPLACE = "VBREPLACE"
    Public Const REGEXPRESSION = "REG. EXPRESSION"

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

    Public Function Get_Nachbearbeitung_Wert(idxvalue As String, Datatable As DataTable) As String
        Dim oIndexValues As List(Of String) = idxvalue.Split(ClassConstants.VECTORSEPARATOR).ToList()

        Try
            For Each oDataRow As DataRow In Datatable.Rows
                Dim oResult As New List(Of String)
                Dim oType As String = oDataRow.Item("TYPE").ToString.ToUpper

                Logger.Info(" ...Nachbearbeitung mit [{0}]", oType)

                Select Case oType
                    Case VBSPLIT

                        Dim oSeparator As String = oDataRow.Item("TEXT1")
                        Dim oSplitIndex As Integer = 0
                        Integer.TryParse(oDataRow.Item("TEXT2"), oSplitIndex)

                        For Each oIndexValue In oIndexValues
                            Dim oSplitted As List(Of String) = oIndexValue.Split(oSeparator).ToList()
                            oResult.Add(oSplitted.Item(oSplitIndex))
                        Next

                    Case VBREPLACE
                        Dim oFindString = oDataRow.Item("TEXT1")
                        Dim oReplaceString = oDataRow.Item("TEXT2")

                        Logger.Info(" ...Ersetze '" & oFindString & "' mit '" & oReplaceString & "'")

                        For Each oIndexValue In oIndexValues
                            Dim oReplaceResult = oIndexValue.Replace(oFindString, oReplaceString)
                            oResult.Add(oReplaceResult)
                        Next

                    Case REGEXPRESSION
                        Dim oRegexList As New List(Of Regex)
                        Dim oRegex As New Regex(oDataRow.Item("TEXT1"), RegexOptions.IgnoreCase)

                        oRegexList.Add(oRegex)

                        For Each oIndexValue In oIndexValues
                            Dim oProcessedString = ExtractFromStringWithRegex(oIndexValue, oRegexList)
                            oResult.Add(oProcessedString)

                            Logger.Info(" ...Ergebnis des RegEx: " & oProcessedString)
                        Next
                End Select

                oIndexValues = oResult
            Next
        Catch ex As Exception
            Logger.Error(ex)
        End Try

        Return String.Join(ClassConstants.VECTORSEPARATOR, oIndexValues.ToArray)
    End Function

    ''' <summary>
    ''' Extrahiert aus dem String anhand einer Liste von Regular Expressions ein Ergebnis.
    ''' </summary>
    ''' <param name="SearchString">Der zu untersuchende String erzeugt wurden.</param>
    ''' <param name="RegexList">Eine Liste von Regular Expressions</param>
    ''' <param name="RegexGroup">Die Ergebnisgruppe, die die Adresse enthält</param>
    ''' <returns>Eine Emailadresse oder Nothing, wenn keine der Regular Expressions ein Ergebnis lieferte.</returns>
    Private Function ExtractFromStringWithRegex(SearchString As String, RegexList As List(Of Regex), Optional RegexGroup As Integer = 1)
        If IsNothing(SearchString) Then
            Return Nothing
        End If

        For Each rx In RegexList
            Dim match As Match = rx.Match(SearchString)
            Dim result As String = match.Groups(RegexGroup).Value
            If Not String.IsNullOrWhiteSpace(result) Then
                'Nur den ersten Wert zurückgeben
                Return result
            End If
        Next

        Return Nothing
    End Function
End Class
