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
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return String.Empty
                Else
                    'Umwandeln in String
                    myArray(0) = CStr(pValue)
                End If
            Case INDEX_TYPE_VECTOR_STRING '4097
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return String.Empty
                Else
                    'Umwandeln in String
                    myArray(0) = CStr(pValue)
                End If
            Case INDEX_TYPE_VECTOR_INTEGER '4098
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    'Umwandeln in Integer
                    myArray(0) = CInt(pValue.Replace(" ", ""))
                End If
            Case INDEX_TYPE_VECTOR_FLOAT  '4099
                pValue = pValue.
                    Replace(" ", "").
                    Replace(".", ",")
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    'Umwandeln in Double
                    myArray(0) = CDbl(pValue)
                End If
            Case INDEX_TYPE_VECTOR_BOOLEAN '4100
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    'Umwandeln in Boolean
                    myArray(0) = CBool(pValue)
                End If

            Case INDEX_TYPE_VECTOR_DATE '4101
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    'Umwandeln in Date
                    myArray(0) = CDate(pValue)
                End If
            Case INDEX_TYPE_VECTOR_INTEGER_64BIT '4107
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    'Umwandeln in Date
                    myArray(0) = Convert.ToInt64(pValue)
                End If
            Case INDEX_TYPE_VECTOR_DATE_TIME '4103
                Return Convert.ToDateTime(pValue)
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    'Umwandeln in Date
                    myArray(0) = Convert.ToDateTime(pValue)
                End If

            Case 8204
                pValue = pValue.Replace(" ", "")
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    'Umwandeln in Integer
                    myArray(0) = CInt(pValue.Replace(" ", ""))
                End If
            Case Else
                'Umwandeln in String
                myArray(0) = CStr(pValue)
        End Select
        Return myArray
    End Function

    Friend Shared Function ConvertIndexValue(Type As Integer, pValue As String) As Object

        pValue = pValue.Trim()
        If Type > 4095 Then
            Return ConvertVectorType(Type, pValue)
        End If


        Select Case Type
            Case INDEX_TYPE_HASH
                Return pValue
            Case INDEX_TYPE_STRING
                ' Leerzeichen an Anfang und Ende entfernen
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return String.Empty
                Else
                    Return pValue
                End If

            Case INDEX_TYPE_VECTOR_STRING
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return String.Empty
                Else
                    Return pValue
                End If
            Case INDEX_TYPE_INTEGER
                pValue = pValue.Replace(" ", String.Empty)
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    Return Convert.ToInt32(pValue)
                End If
            Case INDEX_TYPE_VECTOR_INTEGER
                pValue = pValue.Replace(" ", String.Empty)
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    Return Convert.ToInt32(pValue)
                End If
            Case INDEX_TYPE_VECTOR_INTEGER_64BIT
                pValue = pValue.Replace(" ", String.Empty)
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    Return Convert.ToInt64(pValue)
                End If
            Case INDEX_TYPE_VECTOR_BOOLEAN
                Return Convert.ToBoolean(pValue)
            Case INDEX_TYPE_BOOLEAN
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    Return Convert.ToBoolean(pValue)
                End If

            Case INDEX_TYPE_DATE
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    Return Convert.ToDateTime(pValue)
                End If

            Case INDEX_TYPE_DATE_TIME
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    Return Convert.ToDateTime(pValue)
                End If
            Case INDEX_TYPE_TIME
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    Return Convert.ToDateTime(pValue)
                End If
            Case INDEX_TYPE_VECTOR_DATE_TIME
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    Return Convert.ToDateTime(pValue)
                End If
            Case INDEX_TYPE_VECTOR_DATE
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    Return Convert.ToDateTime(pValue)
                End If
            Case INDEX_TYPE_FLOAT
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    Return Convert.ToDouble(pValue)
                End If

            Case INDEX_TYPE_VECTOR_FLOAT
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    Return Convert.ToDouble(pValue)
                End If
            Case INDEX_TYPE_CURRENCY
                If pValue = DELETE_ATTRIBUTE_VALUE Then
                    Return Nothing
                Else
                    'Wegen currency muß ein eigenes Objekt vom typ Variant erzeugt werden
                    Dim aValueWrapper As System.Runtime.InteropServices.CurrencyWrapper = New System.Runtime.InteropServices.CurrencyWrapper(CDec(pValue))
                    Return aValueWrapper
                End If

            Case Else
                Return pValue
        End Select
    End Function

    Friend Shared Function ConvertAttrValue(Type As Integer, pValue As String) As Object
        ' Leerzeichen an Anfang und Ende entfernen
        pValue = pValue.Trim()
        If Type > 4095 Then
            Return ConvertVectorType1(Type, pValue)
        End If


        Select Case Type
            Case INDEX_TYPE_HASH
                Return pValue
            Case INDEX_TYPE_STRING
                Return pValue
            Case INDEX_TYPE_VECTOR_STRING
                Return pValue
            Case INDEX_TYPE_INTEGER
                pValue = pValue.Replace(" ", String.Empty)
                Return Convert.ToInt32(pValue)
            Case INDEX_TYPE_VECTOR_INTEGER
                pValue = pValue.Replace(" ", String.Empty)
                Return Convert.ToInt32(pValue)
            Case INDEX_TYPE_VECTOR_INTEGER_64BIT
                pValue = pValue.Replace(" ", String.Empty)
                Return Convert.ToInt64(pValue)
            Case INDEX_TYPE_VECTOR_BOOLEAN
                Return Convert.ToBoolean(pValue)
            Case INDEX_TYPE_BOOLEAN
                Return Convert.ToBoolean(pValue)
            Case INDEX_TYPE_DATE
                Return Convert.ToDateTime(pValue)
            Case INDEX_TYPE_DATE_TIME
                Return Convert.ToDateTime(pValue)
            Case INDEX_TYPE_TIME
                Return Convert.ToDateTime(pValue)
            Case INDEX_TYPE_VECTOR_DATE_TIME
                Return Convert.ToDateTime(pValue)
            Case INDEX_TYPE_VECTOR_DATE
                Return Convert.ToDateTime(pValue)
            Case INDEX_TYPE_FLOAT
                Return Convert.ToDouble(pValue)
            Case INDEX_TYPE_VECTOR_FLOAT
                Return Convert.ToDouble(pValue)
            Case INDEX_TYPE_CURRENCY
                'Wegen currency muß ein eigenes Objekt vom typ Variant erzeugt werden
                Dim aValueWrapper As System.Runtime.InteropServices.CurrencyWrapper = New System.Runtime.InteropServices.CurrencyWrapper(CDec(pValue))
                Return aValueWrapper
            Case Else
                Return pValue
        End Select
    End Function
    Friend Shared Function ConvertVectorType1(pIndexType As Integer, pValue As String)
        Select Case pIndexType
            Case INDEX_TYPE_HASH ' 36865
                'Umwandeln in String
                Return CStr(pValue)
            Case INDEX_TYPE_VECTOR_STRING '4097
                'Umwandeln in String
                Return CStr(pValue)
            Case INDEX_TYPE_VECTOR_INTEGER '4098
                'Umwandeln in Integer
                Return CInt(pValue.Replace(" ", ""))
            Case INDEX_TYPE_VECTOR_FLOAT  '4099
                pValue = pValue.
                    Replace(" ", "").
                    Replace(".", ",")
                'Umwandeln in Double
                Return CDbl(pValue)
            Case INDEX_TYPE_VECTOR_BOOLEAN '4100
                'Umwandeln in Boolean
                Return CBool(pValue)
            Case INDEX_TYPE_VECTOR_DATE '4101
                'Umwandeln in Date
                Return CDate(pValue)
            Case INDEX_TYPE_VECTOR_INTEGER_64BIT '4107
                Return Convert.ToInt64(pValue)
            Case INDEX_TYPE_VECTOR_DATE_TIME '4103
                'Umwandeln in Datum Uhrzeit
                Return pValue
            Case 8204
                'Umwandeln in Integer
                Return CInt(pValue.Replace(" ", ""))
            Case Else
                'Umwandeln in String
                Return CStr(pValue)
        End Select
    End Function
    Friend Shared Function IsVectorIndex(indexType As Integer)
        Return VectorIndicies.Contains(indexType)
    End Function
End Class
