Imports System.Text.RegularExpressions

Public Class ClassPostprocessing

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

    Public Shared 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

                Select Case oType
                    Case VBSPLIT
                        If LogErrorsOnly = False Then ClassLogger.Add(" ...Nachbearbeitung mit VBSPLIT", False)

                        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")

                        If LogErrorsOnly = False Then ClassLogger.Add(" ...Nachbearbeitung mit VBREPLACE", False)
                        If LogErrorsOnly = False Then ClassLogger.Add(" ...Ersetze '" & oFindString & "' mit '" & oReplaceString & "'", False)

                        For Each oIndexValue In oIndexValues
                            Dim oReplaceResult = oIndexValue.Replace(oFindString, oReplaceString)
                            oResult.Add(oReplaceResult)
                        Next
                    Case REGEXPRESSION
                        If LogErrorsOnly = False Then ClassLogger.Add(" ...Nachbearbeitung mit RegEx", False)

                        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)

                            If LogErrorsOnly = False Then ClassLogger.Add(" ...Ergebnis des RegEx: " & oProcessedString, False)
                        Next
                End Select

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

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

        'Dim result As String = idxvalue
        'Try
        '    For Each row As DataRow In Datatable.Rows
        '        Select Case row.Item("TYPE").ToString.ToUpper
        '            Case "VBSPLIT"
        '                If LogErrorsOnly = False Then ClassLogger.Add(" ...Nachbearbeitung mit VBSPLIT", False)
        '                Dim strSplit() As String
        '                strSplit = result.Split(row.Item("TEXT1"))
        '                For i As Integer = 0 To strSplit.Length - 1
        '                    If i = CInt(row.Item("TEXT2")) Then
        '                        If LogErrorsOnly = False Then ClassLogger.Add(" ...Split-Ergebnis für Index (" & i.ToString & "): " & strSplit(i), False)
        '                        result = strSplit(i).ToString
        '                    End If
        '                Next
        '            Case "VBREPLACE"
        '                If LogErrorsOnly = False Then
        '                    ClassLogger.Add(" ...Nachbearbeitung mit VBREPLACE", False)
        '                    ClassLogger.Add(" ...Ersetze '" & row.Item("TEXT1") & "' mit '" & row.Item("TEXT2") & "'", False)
        '                    result = result.Replace(row.Item("TEXT1"), row.Item("TEXT2"))
        '                End If

        '                result = result.Replace(row.Item("TEXT1"), row.Item("TEXT2"))
        '            Case "REG. EXPRESSION"
        '                If LogErrorsOnly = False Then ClassLogger.Add(" ...Nachbearbeitung mit RegEx", False)
        '                Dim RegexList As New List(Of System.Text.RegularExpressions.Regex)
        '                Dim Regex As New System.Text.RegularExpressions.Regex(row.Item("TEXT1"), System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        '                RegexList.Add(Regex)
        '                '
        '                Dim resultRegex = ClassPostprocessing.extractFromStringviaRE(result, RegexList)
        '                If Not IsNothing(resultRegex) Then
        '                    If LogErrorsOnly = False Then ClassLogger.Add(" ...Ergebnis des RegEx: " & resultRegex.ToString, False)
        '                    result = resultRegex.ToString
        '                Else
        '                    ClassLogger.Add("Postprocessing RegEx konnte kein Ergebnis auswerten!", True)
        '                End If
        '        End Select
        '    Next
        '    Return result
        'Catch ex As Exception
        '    ClassLogger.Add(" - Unvorhergesehener Unexpected error in Get_Nachbearbeitung_Wert - result: " & result & " - Fehler: " & vbNewLine & ex.Message)
        '    MsgBox(ex.Message, MsgBoxStyle.Critical, "Unexpected error in Get_Nachbearbeitung_Wert:")
        '    Return result
        'End Try

    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 Shared 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
