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

Namespace Search
    Public Class Search
        Private ReadOnly _SearchQuery As New BindingList(Of SearchCriteria)
        Private ReadOnly _LogConfig As LogConfig
        Private ReadOnly _Database As DatabaseWithFallback
        Private ReadOnly _UserState As UserState

        Private ReadOnly _OpEquals As New KeyValuePair(Of String, Object)("gleich", New AttributeOperatorToken(OperatorToken.Equals))
        Private ReadOnly _OpNotEquals As New KeyValuePair(Of String, Object)("nicht gleich", New AttributeOperatorToken(OperatorToken.NotEquals))
        Private ReadOnly _OpGreaterThan As New KeyValuePair(Of String, Object)("größer als", New AttributeOperatorToken(OperatorToken.GreaterThan))
        Private ReadOnly _OpLessThan As New KeyValuePair(Of String, Object)("kleiner als", New AttributeOperatorToken(OperatorToken.LessThan))
        Private ReadOnly _OpContains As New KeyValuePair(Of String, Object)("enthält", New AttributeOperatorToken(OperatorToken.Contains))

        Public ReadOnly Property Query As BindingList(Of SearchCriteria)



        Private ReadOnly TokenListAttributes As New Dictionary(Of String, Object)
        'Private ReadOnly TokenListAttributes As New Dictionary(Of String, Object) From {
        '    {"Rechnungsnummer", New AttributeKeyToken("InvoiceNo")},
        '    {"Rechnungsdatum", New AttributeKeyToken("InvoiceDate")},
        '    {"Kundennummer", New AttributeKeyToken("CustNo")}
        '}
        Private ReadOnly TokenListOperands As New Dictionary(Of String, Object) From {
            {"gleich", New AttributeOperatorToken(OperatorToken.Equals)},
            {"nicht gleich", New AttributeOperatorToken(OperatorToken.NotEquals)},
            {"größer als", New AttributeOperatorToken(OperatorToken.Equals)},
            {"kleiner als", New AttributeOperatorToken(OperatorToken.Equals)},
            {"enthält", New AttributeOperatorToken(OperatorToken.Equals)}
        }
        Private ReadOnly TokenListAttrValues As New Dictionary(Of String, Object)
        'Private ReadOnly TokenListAttrValues As New Dictionary(Of String, Object) From {
        '    {"1233", New AttributeValueToken(1233)},
        '    {"1234", New AttributeValueToken(1234)},
        '    {"1235", New AttributeValueToken(1235)},
        '    {"4711", New AttributeValueToken(4711)},
        '    {"4712", New AttributeValueToken(4712)}
        '}
        Private ReadOnly TokenListDate As New Dictionary(Of String, Object) From {
            {"heute", New DateToken(Date.Now)},
            {"gestern", New DateToken(Date.Now.AddDays(-1))},
            {"letzte Woche", New DateToken(TimeSpan.FromDays(-7))},
            {"letzter Monat", New DateToken(TimeSpan.FromDays(-30))}
        }

        Public InputMode As InputMode = InputMode.Default

        Public Sub New(pLogConfig As LogConfig, pUserState As UserState, pDatabase As DatabaseWithFallback)
            _LogConfig = pLogConfig
            _Database = pDatabase
            _UserState = pUserState
        End Sub

        Public Function GetAttributeTokens() As Dictionary(Of String, Object)
            Dim oSQL = $"SELECT * FROM VWIDB_BE_ATTRIBUTE WHERE DEFAULT_SEARCH_ATTRIBUTE = 1 AND LANG_CODE = '{_UserState.Language}'"
            Dim oTable = _Database.GetDatatable("VWIDB_BE_ATTRIBUTE", oSQL, Constants.DatabaseType.IDB, $"DEFAULT_SEARCH_ATTRIBUTE = 1 AND LANG_CODE = '{_UserState.Language}'")
            Dim oTokens As New Dictionary(Of String, Object)

            For Each oRow As DataRow In oTable.rows
                oTokens.Add(oRow.Item("ATTR_TITLE"), New AttributeKeyToken(oRow.Item("ATTR_ID")))
            Next

            Return oTokens
        End Function

        Public Function GetValueTokensForAttribute() As Dictionary(Of String, Object)
            Dim oSQL = $"SELECT * FROM VWIDB_BE_ATTRIBUTE WHERE DEFAULT_SEARCH_ATTRIBUTE = 1 AND LANG_CODE = '{_UserState.Language}'"
            Dim oTable = _Database.GetDatatable("VWIDB_BE_ATTRIBUTE", oSQL, Constants.DatabaseType.IDB, $"DEFAULT_SEARCH_ATTRIBUTE = 1 AND LANG_CODE = '{_UserState.Language}'")
            Dim oTokens As New Dictionary(Of String, Object)
        End Function

        Public Function GetOperatorTokens(pDataType As Type) As Dictionary(Of String, Object)
            Dim oResult = New Dictionary(Of String, Object)

            Select Case pDataType.GetType
                Case GetType(Date)
                    oResult.Add(_OpEquals.Key, _OpEquals.Value)

                Case GetType(Integer)
                    oResult.Add(_OpEquals.Key, _OpEquals.Value)
                    oResult.Add(_OpNotEquals.Key, _OpNotEquals.Value)
                    oResult.Add(_OpGreaterThan.Key, _OpGreaterThan.Value)
                    oResult.Add(_OpLessThan.Key, _OpLessThan.Value)

                Case GetType(Boolean)
                    oResult.Add(_OpEquals.Key, _OpEquals.Value)
                    oResult.Add(_OpNotEquals.Key, _OpNotEquals.Value)

                Case Else
                    oResult.Add(_OpEquals.Key, _OpEquals.Value)
                    oResult.Add(_OpNotEquals.Key, _OpNotEquals.Value)
                    oResult.Add(_OpContains.Key, _OpContains.Value)

            End Select

            Return oResult
        End Function

    End Class
End Namespace
