Imports System.Text.RegularExpressions
Imports DigitalData.Modules.Windream.Constants

Public Class Helpers
    Private Shared ReadOnly VectorIndicies As New List(Of Integer) From {
        INDEX_TYPE_VECTOR_BOOLEAN,
        INDEX_TYPE_VECTOR_CURRENCY,
        INDEX_TYPE_VECTOR_DATE,
        INDEX_TYPE_VECTOR_DATE_TIME,
        INDEX_TYPE_VECTOR_FIXED_POINT,
        INDEX_TYPE_VECTOR_FLOAT,
        INDEX_TYPE_VECTOR_INTEGER,
        INDEX_TYPE_VECTOR_INTEGER_64BIT,
        INDEX_TYPE_VECTOR_STRING,
        INDEX_TYPE_VECTOR_TIME
    }

    Friend Shared Function ConvertVectorType(pIndexType As Integer, pValue As String)
        Dim myArray
        ReDim myArray(0)
        Select Case pIndexType
            Case INDEX_TYPE_HASH ' 36865
                'Umwandeln in String
                myArray(0) = CStr(pValue)
                Return myArray
            Case INDEX_TYPE_VECTOR_STRING '4097
                'Umwandeln in String
                myArray(0) = CStr(pValue)
                Return myArray
            Case INDEX_TYPE_VECTOR_INTEGER '4098
                'Umwandeln in Integer
                myArray(0) = CInt(pValue.Replace(" ", ""))
                Return myArray
            Case INDEX_TYPE_VECTOR_FLOAT  '4099
                pValue = pValue.
                    Replace(" ", "").
                    Replace(".", ",")
                'Umwandeln in Double
                myArray(0) = CDbl(pValue)
                Return myArray
            Case INDEX_TYPE_VECTOR_BOOLEAN '4100
                'Umwandeln in Boolean
                myArray(0) = CBool(pValue)
                Return myArray
            Case INDEX_TYPE_VECTOR_DATE '4101
                'Umwandeln in Date
                myArray(0) = CDate(pValue)
                Return myArray
            Case INDEX_TYPE_VECTOR_INTEGER_64BIT '4107
                myArray(0) = Convert.ToInt64(pValue)
                Return myArray
            Case INDEX_TYPE_VECTOR_DATE_TIME '4103
                'Umwandeln in Datum Uhrzeit
                Return pValue
            Case 8204
                'Umwandeln in Integer
                myArray(0) = CInt(pValue.Replace(" ", ""))
                Return myArray
            Case Else
                'Umwandeln in String
                myArray(0) = CStr(pValue)
                Return myArray
        End Select
    End Function

    Friend Shared Function ConvertIndexValue(Type As Integer, Value As String) As Object
        ' Leerzeichen an Anfang und Ende entfernen
        Value = Value.Trim()

        Select Case Type
            Case INDEX_TYPE_HASH
                Return Value
            Case INDEX_TYPE_STRING
                Return Value
            Case INDEX_TYPE_VECTOR_STRING
                Return Value
            Case INDEX_TYPE_INTEGER
                Value = Value.Replace(" ", String.Empty)
                Return Convert.ToInt32(Value)
            Case INDEX_TYPE_VECTOR_INTEGER
                Value = Value.Replace(" ", String.Empty)
                Return Convert.ToInt32(Value)
            Case INDEX_TYPE_VECTOR_INTEGER_64BIT
                Value = Value.Replace(" ", String.Empty)
                Return Convert.ToInt64(Value)
            Case INDEX_TYPE_VECTOR_BOOLEAN
                Return Convert.ToBoolean(Value)
            Case INDEX_TYPE_BOOLEAN
                Return Convert.ToBoolean(Value)
            Case INDEX_TYPE_DATE
                Return Convert.ToDateTime(Value)
            Case INDEX_TYPE_DATE_TIME
                Return Convert.ToDateTime(Value)
            Case INDEX_TYPE_TIME
                Return Convert.ToDateTime(Value)
            Case INDEX_TYPE_VECTOR_DATE_TIME
                Return Convert.ToDateTime(Value)
            Case INDEX_TYPE_VECTOR_DATE
                Return Convert.ToDateTime(Value)
            Case INDEX_TYPE_FLOAT
                Return Convert.ToDouble(Value)
            Case INDEX_TYPE_VECTOR_FLOAT
                Return Convert.ToDouble(Value)
            Case Else
                Return Value
        End Select
    End Function

    Friend Shared Function IsVectorIndex(indexType As Integer)
        Return VectorIndicies.Contains(indexType)
    End Function
End Class
