Imports System.Collections
Imports System.ComponentModel
Imports DevExpress.XtraEditors
Imports DigitalData.GUIs.ZooFlow.Search
Imports DigitalData.GUIs.ZooFlow.Search.SearchToken
Imports DigitalData.Modules.EDMI.API

Public Class frmSearchNeu
    Private Database As DatabaseWithFallback

    Private TokenListDefault As New Dictionary(Of String, Object)
    Private TokenListOperands As New Dictionary(Of String, Object)
    Private TokenListAttrValues As New Dictionary(Of String, Object)



    Private Sub XtraForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Database = New DatabaseWithFallback(My.LogConfig, My.Application.Service.Client, My.DatabaseECM, My.DatabaseIDB)

        'Dim oTokens = Search.GetAttributeTokens()
        'AddTokens(SearchControl2, oTokens)

        'GridControl1.DataSource = Search.Query
        'cmbSelect.SelectedIndex = 0
    End Sub

    Private Sub SetTokens(Editor As TokenEdit, Tokens As Dictionary(Of String, Object))
        Editor.Properties.Tokens.Clear()
        AddTokens(Editor, Tokens)
    End Sub

    Private Sub AddTokens(Editor As TokenEdit, Tokens As Dictionary(Of String, Object))
        For Each oToken In Tokens
            Dim oTokenEditToken = New TokenEditToken With {
                .Description = oToken.Key,
                .Value = oToken.Value
            }
            Editor.Properties.Tokens.Add(oTokenEditToken)
        Next
    End Sub

    Private Sub SearchControl2_Properties_TokenAdded(sender As Object, e As DevExpress.XtraEditors.TokenEditTokenAddedEventArgs) Handles SearchControl2.Properties.TokenAdded
        Dim oEditor As TokenEdit = sender
        SetNewTokens(oEditor)
    End Sub

    Private Sub SearchControl2_Properties_TokenRemoved(sender As Object, e As TokenEditTokenRemovedEventArgs) Handles SearchControl2.Properties.TokenRemoved
        Dim oEditor As TokenEdit = sender
        SetNewTokens(oEditor)
    End Sub

    Private Sub SetNewTokens(pEditor As TokenEdit)
        'Dim oLastToken = pEditor.GetTokenList().LastOrDefault()
        'pEditor.Properties.BeginUpdate()

        'If oLastToken IsNot Nothing Then
        '    Select Case oLastToken.Value.GetType

        '        Case GetType(AttributeKeyToken)
        '            ' After the attribute key comes an operator
        '            SetTokens(pEditor, Search.GetOperatorTokens(GetType(String)))
        '            Search.InputMode = InputMode.Operator

        '        Case GetType(AttributeOperatorToken)
        '            ' After the attribute operator comes a value
        '            SetTokens(pEditor, TokenListAttrValues)
        '            Search.InputMode = InputMode.Value

        '        Case GetType(AttributeValueToken)
        '            ' After the attribute value comes another value
        '            SetTokens(pEditor, TokenListAttrValues)
        '            Search.InputMode = InputMode.Value

        '        Case Else
        '            SetTokens(pEditor, TokenListDefault)
        '            Search.InputMode = InputMode.Default

        '    End Select
        'Else
        '    SetTokens(pEditor, TokenListDefault)
        '    Search.InputMode = InputMode.Default
        'End If

        'pEditor.Properties.EndUpdate()
    End Sub

    Private Sub SearchControl2_CustomDrawTokenGlyph(sender As Object, e As TokenEditCustomDrawTokenGlyphEventArgs) Handles SearchControl2.CustomDrawTokenGlyph
        '' Set Background according to token type
        'Select Case e.Value.GetType()
        '    Case GetType(AttributeKeyToken)
        '        e.Graphics.FillRectangle(New SolidBrush(ColorTranslator.FromHtml("#F87171")), e.Bounds)

        '    Case GetType(AttributeOperatorToken)
        '        e.Graphics.FillRectangle(New SolidBrush(ColorTranslator.FromHtml("#34D399")), e.Bounds)

        '    Case GetType(AttributeValueToken)
        '        e.Graphics.FillRectangle(New SolidBrush(ColorTranslator.FromHtml("#60A5FA")), e.Bounds)
        '    Case Else
        'End Select

        '' Draw the glyph on top
        '' This fixes: https://supportcenter.devexpress.com/ticket/details/t215578/tokenedit-glyph-is-not-visible-when-customdrawtokentext-is-used
        'e.DefaultDraw()
    End Sub

    Private Sub SearchControl2_KeyUp(sender As Object, e As KeyEventArgs) Handles SearchControl2.KeyUp
        'If Search.InputMode = InputMode.Value And e.KeyCode = Keys.Enter And SearchControl2.IsPopupOpen Then
        '    Search.Query.Add(New SearchCriteria With {
        '        .ParenLeft = False,
        '        .Key = "test",
        '        .Op = OperatorToken.Equals,
        '        .Value = "test",
        '        .ParentRight = False
        '    })
        'End If
    End Sub

    Private Sub RibbonControl1_Click(sender As Object, e As EventArgs) Handles RibbonControl1.Click

    End Sub
End Class