Imports System.ComponentModel
Imports System.Drawing.Design
Imports System.Globalization
Imports FormsUtils 'FormsUtils.dll als Fremdbibliothek für die Properties

Module ModuleFinalIndexProperties
    <TypeConverter(GetType(PropertiesDeluxeTypeConverter))>
    Public Class FinalIndexProperties
        <DisplayName("ID")>
        <Category(ClassConstants.CAT_GENERAL)>
        <[ReadOnly](True)>
        Public Property GUID As Integer

        '<DisplayName("Connection ID")>
        '<Category(ClassConstants.CAT_INFORMATION)>
        '<[ReadOnly](True)>
        'Public Property ConnectionId As Integer

        <Category(ClassConstants.CAT_GENERAL)>
        Public Property Description As String

        <Category(ClassConstants.CAT_BEHAVIOUR)>
        Public Property Active As Boolean

        <Category(ClassConstants.CAT_BEHAVIOUR)>
        Public Property Sequence As Integer = 0

        <DisplayName("Continue On Indifferent State")>
        <Category(ClassConstants.CAT_BEHAVIOUR)>
        Public Property ContinueOnIndifferentState As Boolean

        ''' <summary>
        ''' Eigenschaft, die den SQL Editor anzeigt
        ''' </summary>
        <DisplayName("SQL Command")>
        <Category(ClassConstants.CAT_DATA)>
        Public Property SQLCommand As SQLValue

        ' Eigenschaften für die verschiedenen Index-Typen
        <DisplayName("Value")>
        <Category(ClassConstants.CAT_DATA)>
        <PropertyAttributesProvider("IndexTypeStringProvider")>
        Public Property StringValue As String

        <DisplayName("Value")>
        <Category(ClassConstants.CAT_DATA)>
        <PropertyAttributesProvider("IndexTypeBooleanProvider")>
        Public Property BoolValue As Boolean

        <DisplayName("Value")>
        <Category(ClassConstants.CAT_DATA)>
        <PropertyAttributesProvider("IndexTypeFloatProvider")>
        Public Property FloatValue As Double

        <DisplayName("Value")>
        <Category(ClassConstants.CAT_DATA)>
        <PropertyAttributesProvider("IndexTypeDateProvider")>
        Public Property DateValue As Date

        <DisplayName("Value")>
        <Category(ClassConstants.CAT_DATA)>
        <PropertyAttributesProvider("IndexTypeIntegerProvider")>
        Public Property IntegerValue As Integer

        <DisplayName("Index")>
        <Category(ClassConstants.CAT_GENERAL)>
        <TypeConverter(GetType(IndexListConverter))>
        Public Property IndexName As String

        <DisplayName("Vector Index")>
        <Category(ClassConstants.CAT_INFORMATION)>
        <[ReadOnly](True)>
        Public Property VectorIndex As Boolean

        <Category(ClassConstants.CAT_BEHAVIOUR)>
        <DisplayName("Allow Multivalues")>
        <PropertyAttributesProvider("VectorIndexBooleanProvider")>
        Public Property AllowAddNewValues As Boolean

        <Category(ClassConstants.CAT_BEHAVIOUR)>
        <DisplayName("Index Behaviour")>
        <TypeConverter(GetType(ClassVectorBehaviourListConverter))>
        <PropertyAttributesProvider("VectorIndexOnlyProvider")>
        Public Property VectorBehaviour As String

        <DisplayName("Prevent Duplicates")>
        <Category(ClassConstants.CAT_BEHAVIOUR)>
        <PropertyAttributesProvider("VectorIndexBooleanProvider")>
        Public Property PreventDuplicates As Boolean

        ' Eigenschaften für die Liste der Indicies
        <Browsable(False)>
        Public Property Indicies As List(Of String)
        <Browsable(False)>
        Public Property IndiciesType As List(Of Integer)
        <Browsable(False)>
        Public Property VectorBehaviourType As List(Of String)

        Public Sub VectorIndexBooleanProvider(attrs As PropertyAttributes)
            'Sd
            MaybeSetReadOnlyIfNotVectorIndex(attrs)
        End Sub

        Public Sub IndexTypeBooleanProvider(attrs As PropertyAttributes)
            MaybeSetBrowsable(attrs, {FINALINDICES.INDEX_TYPE_BOOLEAN, FINALINDICES.INDEX_TYPE_VECTOR_BOOLEAN})
            MaybeSetReadOnlyIfSQLHasNoValue(attrs)
        End Sub

        Public Sub IndexTypeStringProvider(attrs As PropertyAttributes)
            MaybeSetBrowsable(attrs, {FINALINDICES.INDEX_TYPE_STRING, FINALINDICES.INDEX_TYPE_VECTOR_STRING})
            MaybeSetReadOnlyIfSQLHasNoValue(attrs)
        End Sub

        Public Sub IndexTypeFloatProvider(attrs As PropertyAttributes)
            MaybeSetBrowsable(attrs, {FINALINDICES.INDEX_TYPE_FLOAT})
            MaybeSetReadOnlyIfSQLHasNoValue(attrs)
        End Sub

        Public Sub IndexTypeIntegerProvider(attrs As PropertyAttributes)
            MaybeSetBrowsable(attrs, {FINALINDICES.INDEX_TYPE_INTEGER, FINALINDICES.INDEX_TYPE_VECTOR_INTEGER_64})
            MaybeSetReadOnlyIfSQLHasNoValue(attrs)
        End Sub

        Public Sub IndexTypeDateProvider(attrs As PropertyAttributes)
            MaybeSetBrowsable(attrs, {FINALINDICES.INDEX_TYPE_DATE, FINALINDICES.INDEX_TYPE_VECTOR_DATE})
            MaybeSetReadOnlyIfSQLHasNoValue(attrs)
        End Sub

        Public Sub VectorIndexOnlyProvider(attrs As PropertyAttributes)
            MaybeSetBrowsable(attrs, {FINALINDICES.INDEX_TYPE_VECTOR_STRING})
        End Sub

        Public Sub MaybeSetReadOnlyIfSQLHasNoValue(attrs As PropertyAttributes)
            Dim hasSQLValue As Boolean = (SQLCommand.Value <> String.Empty)

            If hasSQLValue Then
                attrs.IsReadOnly = True
            Else
                attrs.IsReadOnly = False
            End If
        End Sub

        Public Sub MaybeSetReadOnlyIfNotVectorIndex(attrs As PropertyAttributes)
            ' Get index (position in array) for selected index (windream)
            Dim oIndex As Integer = Indicies.IndexOf(IndexName)
            If oIndex < 0 Then
                attrs.IsReadOnly = True
                Exit Sub
            End If

            ' get type of windream index
            Dim oType As Integer = IndiciesType.Item(oIndex)
            Dim oVectoryTypes = {
                FINALINDICES.INDEX_TYPE_VECTOR_DATE,
                FINALINDICES.INDEX_TYPE_VECTOR_BOOLEAN,
                FINALINDICES.INDEX_TYPE_VECTOR_INTEGER_64,
                FINALINDICES.INDEX_TYPE_VECTOR_INTEGER,
                FINALINDICES.INDEX_TYPE_VECTOR_STRING,
                FINALINDICES.INDEX_TYPE_VECTOR_CURRENCY,
                FINALINDICES.INDEX_TYPE_VECTOR_FLOAT,
                FINALINDICES.INDEX_TYPE_VECTOR_DATETIME
            }

            ' if type of index is not a vector, set attribute to read only
            If oVectoryTypes.Contains(oType) Then
                attrs.IsReadOnly = False
            Else
                attrs.IsReadOnly = True
            End If
        End Sub

        Public Sub MaybeSetBrowsable(attrs As PropertyAttributes, indexType As Integer())
            Dim i As Integer = Indicies.IndexOf(IndexName)

            ' IndexName wurde nicth gefunden oder ist leerer/ungültiger String
            If i < 0 Then
                attrs.IsBrowsable = False
                Exit Sub
            End If

            Dim type As Integer = IndiciesType.Item(i)

            If indexType.Contains(type) Then
                attrs.IsBrowsable = True
            Else
                If attrs.DisplayName = "Index Behaviour" Then
                    attrs.IsBrowsable = True
                Else
                    attrs.IsBrowsable = False
                End If

            End If
        End Sub


    End Class
End Module
