Imports DigitalData.Modules.Language.Utils

Public Class ClassFinalIndex
    Public INDEX_TYPE_STRING = 1
    Public INDEX_TYPE_INTEGER = 2
    Public INDEX_TYPE_INTEGER64 = 11
    Public INDEX_TYPE_FLOAT = 3
    Public INDEX_TYPE_BOOLEAN = 4
    Public INDEX_TYPE_DATE = 5
    Public INDEX_TYPE_VECTOR_INTEGER_64 = 4107
    Public INDEX_TYPE_VECTOR_INTEGER = 4098
    Public INDEX_TYPE_VECTOR_STRING = 4097
    Public INDEX_TYPE_VECTOR_BOOLEAN = 4100
    Public INDEX_TYPE_VECTOR_DATE = 4101
    Public INDEX_TYPE_VECTOR_CURRENCY = 4104
    Public INDEX_TYPE_VECTOR_FLOAT = 4099
    Public INDEX_TYPE_VECTOR_DATETIME = 4103


    Public Const PREFIX_VECTOR = "[%VKT"

    Public LIST_VECTOR_INDICIES As New List(Of Integer) From {
        INDEX_TYPE_VECTOR_INTEGER_64,
        INDEX_TYPE_VECTOR_INTEGER,
        INDEX_TYPE_VECTOR_STRING,
        INDEX_TYPE_VECTOR_BOOLEAN,
        INDEX_TYPE_VECTOR_DATE,
        INDEX_TYPE_VECTOR_CURRENCY,
        INDEX_TYPE_VECTOR_FLOAT,
        INDEX_TYPE_VECTOR_DATETIME
    }

    Public Function init()
        If IDB_ACTIVE = True Then
            INDEX_TYPE_STRING = 1
            INDEX_TYPE_INTEGER = 2
            INDEX_TYPE_FLOAT = 3
            INDEX_TYPE_BOOLEAN = 2
            INDEX_TYPE_DATE = 5
            INDEX_TYPE_VECTOR_INTEGER_64 = 9
            INDEX_TYPE_VECTOR_INTEGER = 9
            INDEX_TYPE_VECTOR_STRING = 8
            INDEX_TYPE_VECTOR_BOOLEAN = 9
            INDEX_TYPE_VECTOR_DATE = 8
            INDEX_TYPE_VECTOR_CURRENCY = 8
            INDEX_TYPE_VECTOR_FLOAT = 8
            INDEX_TYPE_VECTOR_DATETIME = 8
            LIST_VECTOR_INDICIES = New List(Of Integer) From {
         INDEX_TYPE_VECTOR_INTEGER_64,
            INDEX_TYPE_VECTOR_INTEGER,
            INDEX_TYPE_VECTOR_STRING,
            INDEX_TYPE_VECTOR_BOOLEAN,
            INDEX_TYPE_VECTOR_DATE,
            INDEX_TYPE_VECTOR_CURRENCY,
            INDEX_TYPE_VECTOR_FLOAT,
            INDEX_TYPE_VECTOR_DATETIME}
        Else
            INDEX_TYPE_STRING = 1
            INDEX_TYPE_INTEGER = 2
            INDEX_TYPE_FLOAT = 3
            INDEX_TYPE_BOOLEAN = 4
            INDEX_TYPE_DATE = 5
            INDEX_TYPE_VECTOR_INTEGER_64 = 4107
            INDEX_TYPE_VECTOR_INTEGER = 4098
            INDEX_TYPE_VECTOR_STRING = 4097
            INDEX_TYPE_VECTOR_BOOLEAN = 4100
            INDEX_TYPE_VECTOR_DATE = 4101
            INDEX_TYPE_VECTOR_CURRENCY = 4104
            INDEX_TYPE_VECTOR_FLOAT = 4099
            INDEX_TYPE_VECTOR_DATETIME = 4103
        End If
    End Function

    Public Function GetValue(obj As Object, indexName As String, indcies As List(Of String), types As List(Of Integer), isVector As Boolean)
        Try
            Dim props As FinalIndexProperties = obj
            Dim i As Integer = indcies.IndexOf(indexName)
            Dim type As Integer = types.Item(i)
            Dim value As String = ""

            If type = INDEX_TYPE_STRING Or type = INDEX_TYPE_VECTOR_STRING Then
                value = props.StringValue
            ElseIf type = INDEX_TYPE_INTEGER Or type = INDEX_TYPE_VECTOR_INTEGER_64 Then
                value = props.IntegerValue.ToString
            ElseIf type = INDEX_TYPE_FLOAT Then
                value = props.FloatValue.ToString
            ElseIf type = INDEX_TYPE_BOOLEAN Or type = INDEX_TYPE_VECTOR_BOOLEAN Then
                value = IIf(props.BoolValue, "1", "0")
            End If

            Return value
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox($"Error in GetValue: {ex.Message}", MsgBoxStyle.Critical)
        End Try
    End Function

    Public Function SetValue(value As String, obj As Object, indexName As String, indcies As List(Of String), types As List(Of Integer))
        Try
            Dim props As FinalIndexProperties = obj
            Dim i As Integer = indcies.IndexOf(indexName)

            If Not indcies.Contains(indexName) Then
                Return obj
            End If

            Dim type As Integer = types.Item(i)
            Dim isVector = value.StartsWith(PREFIX_VECTOR)

            If isVector Then
                value = value.Replace(PREFIX_VECTOR, String.Empty)
            End If

            props.VectorIndex = isVector

            If type = INDEX_TYPE_STRING Or type = INDEX_TYPE_VECTOR_STRING Then
                value = ClassAllgemeineFunktionen.NotNullString(value, "")

                props.StringValue = value
            ElseIf type = INDEX_TYPE_INTEGER Or type = INDEX_TYPE_INTEGER64 Or type = INDEX_TYPE_VECTOR_INTEGER_64 Then
                Try
                    value = ClassAllgemeineFunktionen.NotNullString(value, 0)
                    If value = String.Empty Then
                        props.IntegerValue = 0
                    Else
                        props.IntegerValue = Integer.Parse(value)
                    End If
                Catch ex As Exception
                    If value = "SQL-Command" Then
                        Return props
                    Else
                        props.IntegerValue = 99
                    End If

                End Try

            ElseIf type = INDEX_TYPE_FLOAT Then
                If value = "SQL-Command" Then
                    props.FloatValue = 0
                Else
                    value = ClassAllgemeineFunktionen.NotNullString(value, 0)

                    If value = String.Empty Then
                        props.FloatValue = 0
                    Else
                        props.FloatValue = Double.Parse(value)
                    End If
                End If

            ElseIf type = INDEX_TYPE_BOOLEAN Or type = INDEX_TYPE_VECTOR_BOOLEAN Then
                value = ClassAllgemeineFunktionen.NotNullString(value, "False")

                If value = "1" Or value.ToLower = "true" Then
                    props.BoolValue = True
                Else
                    props.BoolValue = False
                End If
            End If

            Return props
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox($"Error in ClassFinalIndexSetValue: {ex.Message}", MsgBoxStyle.Critical)
        End Try
    End Function

    Public Function IsVectorIndex(type As Integer) As Boolean
        Return LIST_VECTOR_INDICIES.Contains(type)
    End Function

    Public Function GetIndexType(indexName As String, indicies As List(Of String), types As List(Of Integer)) As Integer
        Try
            Dim position As Integer = indicies.IndexOf(indexName)
            Dim type As Integer = types.Item(position)

            Return type
        Catch ex As Exception
            MsgBox(ex.Message & vbNewLine & $"Please check whether attribute {indexName} exists in AtributesList!", MsgBoxStyle.Critical)
            Return Nothing
        End Try

    End Function

End Class
