Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Language
Imports DigitalData.Services.EDMIService.Methods.IDB

Namespace Methods.GlobalIndexer.ImportFile.Steps
    Public Class PostProcessing
        Inherits BaseClass

        Public Const TYPE_VBSPLIT = "VBSPLIT"
        Public Const TYPE_VBREPLACE = "VBREPLACE"
        Public Const TYPE_REGEXPRESSION = "REG. EXPRESSION"

        Private PostprocessingSteps As List(Of PostProcessingStep)

        Public Sub New(pLogConfig As LogConfig, pPostProcessingSteps As List(Of PostProcessingStep))
            MyBase.New(pLogConfig)
            PostprocessingSteps = pPostProcessingSteps

            Logger.Info("Initializing Postprocessing of Manual Indexes")
        End Sub

        Public Function ApplyManualPostprocessing(pManualAttributes As List(Of UserAttributeValue)) As List(Of UserAttributeValue)
            Logger.Debug("Start of Method [ApplyManualPostprocessing]")
            Dim oAttributes = pManualAttributes

            Try
                If PostprocessingSteps Is Nothing Then
                    Logger.Debug("No Postprocessing steps found. Exiting.")
                    Return oAttributes
                End If

                For Each oStep As PostProcessingStep In PostprocessingSteps
                    Dim oIndexId = oStep.IndexId
                    Dim oIndex As UserAttributeValue = pManualAttributes.
                        Where(Function(attr) attr.Id = oStep.IndexId).
                        FirstOrDefault()
                    Dim oIndexPosition = pManualAttributes.IndexOf(oIndex)

                    Logger.Info("Postprocessing Index [{0}]", oIndex.Name)

                    Dim oValues = GetPostprocessingValue(oIndex.Values, oStep)

                    Logger.Info("New Value for Index [{0}] is [{1}]", oIndex.Name, String.Join(",", oValues))

                    ' Replace the old AttributeValue with the new one
                    oAttributes.Item(oIndexPosition) = New UserAttributeValue With {
                        .Id = oIndexId,
                        .Name = oIndex.Name,
                        .Values = oValues,
                        .ControlName = oIndex.ControlName
                    }
                Next

                Return oAttributes
            Catch ex As Exception
                Logger.Warn("Postprocessing failed. Returning incomplete Attributes.")
                Logger.Error(ex)
                Return oAttributes

            End Try
        End Function

        Public Function GetPostprocessingValue(pValues As List(Of String), pStep As PostProcessingStep) As List(Of String)
            Logger.Debug("Start of Method [GetPostprocessingValue]")

            Dim oResult As New List(Of String)

            Logger.Debug("Type of Postprocessing is [{0}]", pStep.Type)

            Select Case pStep.Type
                Case TYPE_VBREPLACE
                    Dim oFindString = pStep.Text1
                    Dim oReplaceString = pStep.Text2

                    Logger.Debug("Replacing [{0}] with [{1}]", oFindString, oReplaceString)

                    For Each oIndexValue In pValues
                        Dim oReplaceResult = oIndexValue.Replace(oFindString, oReplaceString)
                        If oReplaceResult.Equals(oIndexValue) Then
                            Logger.Debug("Replace did not succeed, ReplaceString was not found.")
                        Else
                            Logger.Debug("Replace successful for [{0}].", oIndexValue)
                        End If

                        oResult.Add(oReplaceResult)
                    Next

                Case TYPE_VBSPLIT
                    Dim oSeparator As String = pStep.Text1
                    Dim oSplitIndex As Integer = 0
                    Integer.TryParse(pStep.Text1, oSplitIndex)

                    Logger.Debug("Splitting String at Separator [{0}] and Index [{1}]", oSeparator, oSplitIndex)

                    For Each oIndexValue In pValues
                        Dim oSplitted As List(Of String) = oIndexValue.Split(oSeparator).ToList()
                        Logger.Debug("Split succeeded, resulting list has [{0}] items.", oSplitted.Count)

                        If oSplitIndex < oSplitted.Count Then
                            Dim oValue = oSplitted.Item(oSplitIndex)
                            Logger.Debug("Saving value [{0}] from Index [{1}]", oValue, oSplitIndex)
                            oResult.Add(oValue)
                        Else
                            Logger.Debug("SplitIndex(TEXT2) was out of array bounds. Skipping.")
                        End If

                    Next

                Case Else
                    Logger.Warn("Postprocessing type [{0}] is not supported!", pStep.Type)

            End Select

            Return oResult
        End Function
    End Class

End Namespace