Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing.Design
Imports System.Globalization

Public Module ModuleControlProperties
    Public Enum IndexTypes
        SimpleIndex = 0
        VectorIndex = 1
    End Enum

    Public Class BaseProperties
        Private _size As Size
        Private _font As Font


        <Category("Allgemein")>
        <[ReadOnly](True)>
        Public Property ChangedAt As Date

        <Category("Allgemein")>
        <[ReadOnly](True)>
        Public Property ChangedWho As String

        <Category("Allgemein")>
        <[ReadOnly](True)>
        Public Property ID() As Integer

        <Category("Allgemein")>
        Public Property Name() As String


        <Category("Anzeige")>
        Public Property Location() As Point

        <Category("Anzeige")>
        Public Property Size() As Size
            Get
                Return _size
            End Get
            Set(value As Size)
                If (value.Height <= 0) Then
                    value.Height = 1
                End If

                If (value.Width <= 20) Then
                    value.Width = 20
                End If

                _size = value
            End Set
        End Property

        <Category("Anzeige")>
        <TypeConverter(GetType(FontConverter))>
        Public Overridable Property Font As Font
            Get
                Return _font
            End Get
            Set(value As Font)
                _font = value
            End Set
        End Property

        <Category("Anzeige")>
        Public Property TextColor As Color

        Class FontConverter
            Inherits TypeConverter

            Public Overrides Function ConvertTo(context As ITypeDescriptorContext, culture As CultureInfo, value As Object, destinationType As Type) As Object
                Dim font = DirectCast(value, Font)
                Return $"{font.Name}, {font.Size}"
            End Function
        End Class
    End Class

    Public Class InputProperties
        Inherits BaseProperties

        Private _index_type As String
        Private _sql_command As String
        Private _Override_SQL As String
        Private _Enable_SQL As String
        Private _Enable_SQL_ONLOAD As String

        Private _default_value
        <Category("Allgemein")> Public Property Active() As Boolean

        Public Property Required() As Boolean
        <Category("Validierung")>
        Public Property [ReadOnly]() As Boolean
        <Browsable(False)>
        <Category("Indexierung")>
        Public Property IndexType() As IndexTypes
        ''' <summary>
        ''' Diese Eigenschaft enthält die auswählbaren Indicies, die für das Control verfügbar sind. Wird nicht direkt angezeigt.
        ''' </summary>
        <Browsable(False)>
        Public Property Indicies() As List(Of String)
        ''' <summary>
        ''' Diese Eigenschaft enthält des ausgewählten Index
        ''' </summary>
        <Category("Indexierung")>
        <TypeConverter(GetType(IndexListConverter))>
        Public Property Index() As String
        <Category("Daten")>
        Public Property SQLCommand() As SQLValue
            Get
                Return New SQLValue(NotNull(_sql_command, ""))
            End Get
            Set(ByVal value As SQLValue)
                _sql_command = value.Value
            End Set
        End Property
        Public Property Override_SQL() As SQLValue
            Get
                Return New SQLValue(NotNull(_Override_SQL, ""))
            End Get
            Set(ByVal value As SQLValue)
                _Override_SQL = value.Value
            End Set
        End Property
        Public Property Enable_SQL() As SQLValue
            Get
                Return New SQLValue(NotNull(_Enable_SQL, ""))
            End Get
            Set(ByVal value As SQLValue)
                _Enable_SQL = value.Value
            End Set
        End Property
        Public Property Enable_SQL_OnLoad() As SQLValue
            Get
                Return New SQLValue(NotNull(_Enable_SQL_ONLOAD, ""))
            End Get
            Set(ByVal value As SQLValue)
                _Enable_SQL_ONLOAD = value.Value
            End Set
        End Property

        <Category("Daten")>
        Public Property DefaultValue() As String
            Get
                Return _default_value
            End Get
            Set(value As String)
                _default_value = value
            End Set
        End Property
    End Class

    Public Class TextboxProperties
        Inherits InputProperties
        <Category("Sonstiges")>
        Public Property SetControlData As SQLValue
        <Category("Validierung")>
        <Editor(GetType(ClassRegexEditor), GetType(UITypeEditor))>
        Public Property Regex As String

        <Category("Validierung")>
        <Editor(GetType(MultilineStringEditor), GetType(UITypeEditor))>
        Public Property RegexMessage As String
    End Class

    Public Class LabelProperties
        Inherits BaseProperties

        <Category("Allgemein")>
        Public Property Text() As String
    End Class

    Public Class CheckboxProperties
        Inherits InputProperties

        <Category("Allgemein")>
        Public Property Text() As String
        <Category("Sonstiges")>
        Public Property SetControlData As SQLValue
    End Class

    Public Class ComboboxProperties
        Inherits InputProperties

        <Category("Allgemein")>
        Public Property Text() As String
        <Category("Sonstiges")>
        Public Property SetControlData As SQLValue

        <Browsable(False)>
        Public Property ChoiceLists() As List(Of String)

        <Category("Daten")>
        <TypeConverter(GetType(ChoiceListConverter))>
        Public Property ChoiceList() As String
        <Category("SpecialBehaviour")>
        Public Property DisplayAsLookUpControl As Boolean

        Public Class ChoiceListConverter
            Inherits TypeConverter

            Public Overrides Function GetStandardValuesSupported(context As ITypeDescriptorContext) As Boolean
                Return True
            End Function

            Public Overrides Function GetStandardValues(context As ITypeDescriptorContext) As StandardValuesCollection
                Dim choiceListList = DirectCast(context.Instance, ComboboxProperties).ChoiceLists
                Dim values As New StandardValuesCollection(choiceListList)
                Return values
            End Function

            Public Overrides Function ConvertTo(context As ITypeDescriptorContext, culture As CultureInfo, value As Object, destinationType As Type) As Object
                If IsNothing(value) Then
                    Return ""
                Else
                    Return value.ToString()
                End If
            End Function
        End Class
    End Class

    Public Class DatepickerProperties
        Inherits InputProperties
    End Class

    Public Class GridViewProperties
        Inherits InputProperties
    End Class

    Public Class GridControlProperties
        Inherits InputProperties

        <Category("Einstellungen")>
        Public Property AllowAddNewValues As Boolean
    End Class
    Public Class ButtonProperties
        Inherits InputProperties

        Private _image_Value As String


        <Category("Allgemein")>
        Public Property Text() As String

        <Category("Image")>
        Public Property CtrlImage() As ImageValue
            Get
                Return New ImageValue(NotNull(_image_Value, ""))
            End Get
            Set(ByVal value As ImageValue)
                _image_Value = value.Value
            End Set
        End Property
    End Class
    Public Class LookupControlProperties
        Inherits InputProperties

        <Category("Einstellungen")>
        Public Property MultiSelect As Boolean
        <Category("Einstellungen")>
        Public Property AllowAddNewValues As Boolean
        <Category("Einstellungen")>
        Public Property PreventDuplicates As Boolean
        Public Property ChoiceList() As String
        <Category("SpecialBehaviour")>
        Public Property DisplayAsComboBox As Boolean
        <Category("Sonstiges")>
        Public Property SetControlData As SQLValue

    End Class

    Public Class LineLabelProperties
        Inherits BaseProperties

        <Browsable(False)>
        Public Overrides Property Font As Font
            Get
                Return Nothing
            End Get
            Set(value As Font)
            End Set
        End Property
    End Class
End Module
