
Imports System.ComponentModel
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Controls

Public Class LookupControl
    <Category("Einstellungen"), Description("Gibt an, ob mehrere Werte auswählbar sind"), DefaultValue(False)>
    Public Property MultiSelect As Boolean
    <Category("Einstellungen"), Description("Gibt an, ob neue Werte hinzugefügt werden können"), DefaultValue(False)>
    Public Property AllowAddNewValues As Boolean
    <Category("Einstellungen"), Description("Gibt an, ob das Hinzufügen von identischen Werten erlaubt ist"), DefaultValue(False)>
    Public Property PreventDuplicates As Boolean


    Public Property DataSource As DataTable

    Public Property SelectedValues As List(Of String)
        Get
            If _selectedValues Is Nothing Then
                Return New List(Of String)
            End If

            Return _selectedValues
        End Get
        Set(value As List(Of String))
            _selectedValues = value

            UpdateSelectedValues(value)
        End Set
    End Property
    Private _selectedValues As List(Of String)

    Private _lookupControlMulti As GridLookUpEdit
    Private _lookupControlSingle As ButtonEdit

    Private Const TAG_BUTTON_LOOKUP_FORM = "openLookupForm"
    Private Const TEXT_NO_RECORDS = "Keine Datensätze ausgewählt"
    Private Const TEXT_N_RECORDS = "{0} Datensätze ausgewählt"

    Private Function GetLookupForm() As frmLookupGrid
        Dim oForm As New frmLookupGrid() With {
            .MultiSelect = MultiSelect,
            .AddNewValues = AllowAddNewValues,
            .PreventDuplicates = PreventDuplicates,
            .DataSource = DataSource,
            .SelectedValues = SelectedValues,
            .StartPosition = Windows.Forms.FormStartPosition.Manual,
            .Location = PointToScreen(New System.Drawing.Point(Width, 0))
        }

        Return oForm
    End Function

    Private Sub Grid_Load(sender As Object, e As EventArgs) Handles Me.Load
        If MultiSelect = False Then
            _lookupControlSingle = New ButtonEdit With {
                .Dock = Windows.Forms.DockStyle.Fill
            }

            AddHandler _lookupControlSingle.ButtonClick, AddressOf lookupControlSingle_ButtonClick

            Controls.Add(_lookupControlSingle)
        Else
            _lookupControlMulti = New GridLookUpEdit With {
                .Dock = Windows.Forms.DockStyle.Fill
            }

            With _lookupControlMulti.Properties
                .View.OptionsBehavior.ReadOnly = True
                .View.OptionsBehavior.Editable = False
                .View.OptionsView.ShowColumnHeaders = False
                .PopupFormSize = New System.Drawing.Size(.PopupFormSize.Width, 100)
            End With

            _lookupControlMulti.Properties.Buttons.Add(New EditorButton() With {
                .Kind = ButtonPredefines.Ellipsis,
                .Tag = TAG_BUTTON_LOOKUP_FORM
            })

            AddHandler _lookupControlMulti.ButtonClick, AddressOf lookupControlMulti_ButtonClick
            AddHandler _lookupControlMulti.EditValueChanging, AddressOf lookupControlMulti_EditValueChanging

            Controls.Add(_lookupControlMulti)
        End If

        UpdateSelectedValues(SelectedValues)
    End Sub

    Private Sub UpdateSelectedValues(Values As List(Of String))
        If MultiSelect = True Then
            _lookupControlMulti.Properties.DataSource = Values
            _lookupControlMulti.Properties.NullText = IIf(Values.Count = 0, TEXT_NO_RECORDS, String.Format(TEXT_N_RECORDS, Values.Count))
        Else
            _lookupControlSingle.Text = Values.FirstOrDefault()
        End If
    End Sub

    Private Sub lookupControlMulti_EditValueChanging(sender As Object, e As ChangingEventArgs)
        e.Cancel = True
    End Sub

    Private Sub lookupControlMulti_ButtonClick(sender As Object, e As ButtonPressedEventArgs)
        If e.Button.Tag <> TAG_BUTTON_LOOKUP_FORM Then
            Exit Sub
        End If

        Dim oForm As frmLookupGrid = GetLookupForm()
        Dim oResult = oForm.ShowDialog()

        If oResult = Windows.Forms.DialogResult.OK Then
            Dim oValues = oForm.SelectedValues

            UpdateSelectedValues(oValues)

            SelectedValues = oValues
        End If

        oForm.Dispose()
    End Sub

    Private Sub lookupControlSingle_ButtonClick(sender As Object, e As ButtonPressedEventArgs)
        Dim oForm As frmLookupGrid = GetLookupForm()
        Dim oResult = oForm.ShowDialog()

        If oResult = Windows.Forms.DialogResult.OK Then
            Dim oValues = oForm.SelectedValues

            UpdateSelectedValues(oValues)

            SelectedValues = oValues
        End If

        oForm.Dispose()
    End Sub
End Class


