Imports System.Globalization
Imports DigitalData.Modules.Logging

Public Class StringFunctions
    Public Shared Function SplitText_Length(ByVal input As String, ByVal maxLength As Integer) As List(Of String)
        Dim result As New List(Of String)

        For i As Integer = 0 To input.Length - 1 Step maxLength
            ' Textabschnitt extrahieren
            Dim chunk As String = input.Substring(i, Math.Min(maxLength, input.Length - i))
            result.Add(chunk)
        Next

        Return result
    End Function
    Public Shared Function SplitTextByNewLine(text As String) As List(Of String)
        If String.IsNullOrEmpty(text) Then
            Return New List(Of String)()
        End If

        ' Zerlege den Text anhand von Zeilenumbrüchen
        Dim lines As List(Of String) = text.Split({vbCrLf, vbLf, vbCr}, StringSplitOptions.None).ToList()
        Return lines
    End Function

    Public Shared Function DatetimeStringToGermanStringConverter(pDatetimeString As String, pLogger As Logger) As String

        If pDatetimeString.IsNullOrEmpty() = True Then
            ' Wenn nichts kommt, kommt nichts zurueck
            Return String.Empty
        End If

        Dim formatList As List(Of String) = New List(Of String) From {
                "yyyyMMdd",
                "MM/dd/yyyy", "M/d/yyyy", "MM/d/yyyy", "M/dd/yyyy",
                "yyyy-MM-dd", "yyyy-M-d", "yyyy-MM-d", "yyyy-M-dd",
                "dd.MM.yyyy", "d.M.yyyy", "d.MM.yyyy", "dd.M.yyyy"
        }

        Dim dateStringResult As Date = Date.MinValue
        Dim oConvertResult As Boolean = False

        For Each formatStringItem In formatList
            Try
                dateStringResult = DateTime.ParseExact(pDatetimeString, formatStringItem, CultureInfo.InvariantCulture)
                oConvertResult = True
                Exit For
            Catch ex As FormatException
                oConvertResult = False
                pLogger?.Debug("DatetimeStringToGermanStringConverter() - Could not parse date string {0} ({1})", pDatetimeString, formatStringItem)
            End Try
        Next

        If oConvertResult = True Then
            ' In deutsches Format umwandeln (dd.MM.yyyy)
            Dim germanDateFormat As String = dateStringResult.ToString("dd.MM.yyyy")
            Return germanDateFormat
        Else
            ' Wenn nichts konvertiert werden konnte, geben wir den ursprünglichen Wert zurück
            Return pDatetimeString
        End If

    End Function
End Class
