Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing.Design
Imports System.Globalization
Imports DevExpress.XtraPrinting.Native
Imports DigitalData.Modules.Language.Utils

Public Module ModuleControlProperties

    Public Enum IndexTypes
        SimpleIndex = 0
        VectorIndex = 1
    End Enum

    Public Class BaseProperties
        Private _size As Size
        Private _font As Font

        <DisplayName("Changed At")>
        <Category(ClassConstants.CAT_INFORMATION)>
        <[ReadOnly](True)>
        Public Property ChangedAt As Date

        <DisplayName("Changed Who")>
        <Category(ClassConstants.CAT_INFORMATION)>
        <[ReadOnly](True)>
        Public Property ChangedWho As String

        <Category(ClassConstants.CAT_GENERAL)>
        <[ReadOnly](True)>
        Public Property ID() As Integer

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


        <Category(ClassConstants.CAT_DISPLAY)>
        Public Property Location() As Point

        <Category(ClassConstants.CAT_DISPLAY)>
        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(ClassConstants.CAT_DISPLAY)>
        <TypeConverter(GetType(FontConverter))>
        Public Overridable Property Font As Font
            Get
                Return _font
            End Get
            Set(value As Font)
                _font = value
            End Set
        End Property

        <DisplayName("Font Color")>
        <Category(ClassConstants.CAT_DISPLAY)>
        Public Property TextColor As Color

        <DisplayName("Text Alignement")>
        <Category(ClassConstants.CAT_DISPLAY)>
        <TypeConverter(GetType(TextAlignmentConverter))>
        Public Property TextAlignment As String
        ' Converter für Dropdown-Werte Near/Center/Far
        Public Class TextAlignmentConverter
            Inherits TypeConverter

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

            ' Optional: True, damit nur die vorgegebenen Werte erlaubt sind
            Public Overrides Function GetStandardValuesExclusive(context As ITypeDescriptorContext) As Boolean
                Return True
            End Function

            Public Overrides Function GetStandardValues(context As ITypeDescriptorContext) As StandardValuesCollection
                Dim values = New List(Of String) From {"Near", "Center", "Far"}
                Return New StandardValuesCollection(values)
            End Function

            Public Overrides Function ConvertTo(context As ITypeDescriptorContext, culture As CultureInfo, value As Object, destinationType As Type) As Object
                If destinationType Is GetType(String) Then
                    Return If(value Is Nothing, "", value.ToString())
                End If
                Return MyBase.ConvertTo(context, culture, value, destinationType)
            End Function
        End Class
        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
        'Friend _sql_connection As Integer = 0
        Private _Enable_SQL As String
        Private _Enable_SQL_ONLOAD As String
        Private _default_value
        Friend _set_control_data As String

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

        <Category(ClassConstants.CAT_VALIDATION)>
        Public Property Required() As Boolean

        <DisplayName("Read Only")>
        <Category(ClassConstants.CAT_VALIDATION)>
        Public Property [ReadOnly]() As Boolean

        <DisplayName("Save Changes On Read Only")>
        <Description("Stores data of Read Only fields when document is saved. Default behaviour is NOT to save Read Only fields.")>
        <Category(ClassConstants.CAT_BEHAVIOUR)>
        Public Property [SaveChangeOnReadOnly]() As Boolean

        <Browsable(False)>
        <Category(ClassConstants.CAT_GENERAL)>
        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(ClassConstants.CAT_GENERAL)>
        <TypeConverter(GetType(IndexListConverter))>
        Public Property Index() As String

        <DisplayName("SQL Command")>
        <Category(ClassConstants.CAT_DATA)>
        Public Property SQLCommand() As SQLValue
            Get
                Return New SQLValue(ClassAllgemeineFunktionen.NotNullString(_sql_command, "")) ', _sql_connection
            End Get
            Set(ByVal value As SQLValue)
                _sql_command = value.Value
                'SQLConnection = value.ConnectionId

            End Set
        End Property

        '<DisplayName("SQL Connection")>
        '<Category(ClassConstants.CAT_INFORMATION)>
        '<[ReadOnly](True)>
        'Public Property SQLConnection() As Integer
        '    Get
        '        Return _sql_connection
        '    End Get
        '    Set(value As Integer)
        '        _sql_connection = value
        '    End Set
        'End Property

        <DisplayName("Enable On Change SQL")>
        <Description("SQL Command that determines if a control should be enabled/disabled when another, referenced control changes. Should return 0 or 1. Can include a Control-Placeholder.")>
        <Category(ClassConstants.CAT_BEHAVIOUR)>
        Public Property Enable_SQL() As SQLValue
            Get
                Return New SQLValue(ClassAllgemeineFunktionen.NotNullString(_Enable_SQL, "")) ', _sql_connection
            End Get
            Set(ByVal value As SQLValue)
                _Enable_SQL = value.Value
                'SQLConnection = value.ConnectionId
            End Set
        End Property

        <DisplayName("Enable On Load SQL")>
        <Description("SQL Command that determines if a control should be enabled/disabled when the form loads. Should return 0 or 1. Can include a Control-Placeholder.")>
        <Category(ClassConstants.CAT_BEHAVIOUR)>
        Public Property Enable_SQL_OnLoad() As SQLValue
            Get
                Return New SQLValue(ClassAllgemeineFunktionen.NotNullString(_Enable_SQL_ONLOAD, "")) ', _sql_connection
            End Get
            Set(ByVal value As SQLValue)
                _Enable_SQL_ONLOAD = value.Value
                'SQLConnection = value.ConnectionId
            End Set
        End Property

        <DisplayName("Default Value")>
        <Category(ClassConstants.CAT_DATA)>
        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 InputPropertiesWithControlData
        Inherits InputProperties

        <DisplayName("Set Control Data SQL")>
        <Category(ClassConstants.CAT_BEHAVIOUR)>
        Public Property SetControlData As SQLValue
            Get
                Return New SQLValue(_set_control_data) ', _sql_connection
            End Get
            Set(value As SQLValue)
                _set_control_data = value.Value
                'SQLConnection = value.ConnectionId
            End Set
        End Property
    End Class

    Public Class TextboxProperties
        Inherits InputPropertiesWithControlData

        <DisplayName("Regex Pattern")>
        <Category(ClassConstants.CAT_VALIDATION)>
        <Editor(GetType(ClassRegexEditor), GetType(UITypeEditor))>
        Public Property Regex As String

        <DisplayName("Regex Failure Message")>
        <Category(ClassConstants.CAT_VALIDATION)>
        <Editor(GetType(MultilineStringEditor), GetType(UITypeEditor))>
        Public Property RegexMessage As String

        <DisplayName("Display Format")>
        <Category(ClassConstants.CAT_DISPLAY)>
        <TypeConverter(GetType(DisplayFormatConverter))>
        Public Property DisplayFormat As String

        Public Class DisplayFormatConverter
            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 displayFormatList = New List(Of String) From {
                    ClassControlCreator.CONTROL_TYPE_TEXT,
                    ClassControlCreator.CONTROL_TYPE_DOUBLE,
                    ClassControlCreator.CONTROL_TYPE_CURRENCY,
                    ClassControlCreator.CONTROL_TYPE_DATE,
                    ClassControlCreator.CONTROL_TYPE_DATETIME
                }
                Dim values As New StandardValuesCollection(displayFormatList)
                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 LabelProperties
        Inherits BaseProperties

        <Category(ClassConstants.CAT_DISPLAY)>
        Public Property Text() As String
    End Class

    Public Class CheckboxProperties
        Inherits InputPropertiesWithControlData

        <Category(ClassConstants.CAT_DISPLAY)>
        Public Property Text() As String

    End Class

    Public Class ComboboxProperties
        Inherits InputPropertiesWithControlData

        <Category(ClassConstants.CAT_DISPLAY)>
        Public Property Text() As String

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

        <DisplayName("Static Choice List")>
        <Category(ClassConstants.CAT_DATA)>
        <TypeConverter(GetType(ChoiceListConverter))>
        Public Property ChoiceList() As String

        <DisplayName("Display As Lookup Control")>
        <Category(ClassConstants.CAT_BEHAVIOUR)>
        Public Property DisplayAsLookUpControl As Boolean

        <DisplayName("Add new values")>
        <Category(ClassConstants.CAT_BEHAVIOUR)>
        Public Property AddNewValues 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

        <DisplayName("Allow Adding New Values")>
        <Category(ClassConstants.CAT_BEHAVIOUR)>
        Public Property AllowAddNewValues As Boolean
    End Class

    Public Class ButtonProperties
        Inherits InputProperties

        Private _image_Value As String
        Private _Override_SQL As String

        <Category(ClassConstants.CAT_DISPLAY)>
        Public Property Text() As String

        <DisplayName("Image")>
        <Category(ClassConstants.CAT_DISPLAY)>
        Public Property CtrlImage() As ImageValue
            Get
                Return New ImageValue(ClassAllgemeineFunktionen.NotNullString(_image_Value, ""))
            End Get
            Set(ByVal value As ImageValue)
                _image_Value = value.Value
            End Set
        End Property

        <DisplayName("Completion Override SQL")>
        <Category(ClassConstants.CAT_VALIDATION)>
        Public Property Override_SQL() As SQLValue
            Get
                Return New SQLValue(ClassAllgemeineFunktionen.NotNullString(_Override_SQL, "")) ', _sql_connection
            End Get
            Set(ByVal value As SQLValue)
                _Override_SQL = value.Value
                'SQLConnection = value.ConnectionId
            End Set
        End Property
    End Class

    Public Class LookupControlProperties
        Inherits InputPropertiesWithControlData

        <DisplayName("Multi Select")>
        <Category(ClassConstants.CAT_BEHAVIOUR)>
        Public Property MultiSelect As Boolean

        <DisplayName("Allow Adding New Values")>
        <Category(ClassConstants.CAT_BEHAVIOUR)>
        Public Property AllowAddNewValues As Boolean

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

        <DisplayName("Static Choice List")>
        <Category(ClassConstants.CAT_DATA)>
        Public Property ChoiceList() As String

        <DisplayName("Display As Combobox Control")>
        <Category(ClassConstants.CAT_BEHAVIOUR)>
        Public Property DisplayAsComboBox As Boolean
    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
