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

Public Class GlobixPostprocessing
    Private _Logger As Logger

    Public Sub New(LogConfig As LogConfig)
        _Logger = LogConfig.GetLogger
    End Sub
    Private Const VBSPLIT = "VBSPLIT"
    Private Const VBREPLACE = "VBREPLACE"
    Private Const REGEXPRESSION = "REG. EXPRESSION"

    Public Function Get_Nachbearbeitung_Wert(idxvalue As String, Datatable As DataTable) As String
        Dim oIndexValues As List(Of String) = idxvalue.Split("~").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

                Select Case oType
                    Case VBSPLIT
                        _Logger.Info(" ...Nachbearbeitung mit 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(" ...Nachbearbeitung mit VBREPLACE")
                        _Logger.Info(" ...Ersetze '" & oFindString & "' mit '" & oReplaceString & "'")

                        For Each oIndexValue In oIndexValues
                            Dim oReplaceResult = oIndexValue.Replace(oFindString, oReplaceString)
                            oResult.Add(oReplaceResult)
                        Next
                    Case REGEXPRESSION
                        _Logger.Info(" ...Nachbearbeitung mit RegEx")

                        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 = extractFromStringviaRE(oIndexValue, oRegexList)
                            oResult.Add(oProcessedString)

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

                oIndexValues = oResult
            Next
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Unexpected error in Get_Nachbearbeitung_Wert:")
            _Logger.Info(" - Unvorhergesehener Unexpected error in Get_Nachbearbeitung_Wert - result: " & idxvalue & " - Fehler: " & vbNewLine & ex.Message)
        End Try

        Return String.Join("~", 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>
    Public Function extractFromStringviaRE(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
