Imports System.ComponentModel

Public Class frmEmailIndexing
    Private SourceAttributes As List(Of String)
    Private SourceObjectTypes As List(Of String)

    Private Sub frmEmailIndexing_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            TBDD_DOKUMENTARTTableAdapter.Connection.ConnectionString = MyConnectionString
            TBGI_OBJECTTYPE_EMAIL_INDEXTableAdapter.Connection.ConnectionString = MyConnectionString

            TBDD_DOKUMENTARTTableAdapter.Fill(MyDataset.TBDD_DOKUMENTART)
            ObjektTypenEintragen()
        Catch ex As Exception
            LOGGER.Error(ex)
        End Try
    End Sub

    Private Sub LoadCurrentIndiciesForObjectType(ObjectType As String)
        Try
            TBGI_OBJECTTYPE_EMAIL_INDEXTableAdapter.Fill(MyDataset.TBGI_OBJECTTYPE_EMAIL_INDEX, ObjectType)
        Catch ex As Exception
            MsgBox("Error in LoadIDXEmail:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub ListBoxControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBoxControl1.SelectedIndexChanged
        If ListBoxControl1.SelectedIndex <> -1 Then
            LoadCurrentIndiciesForObjectType(ListBoxControl1.SelectedItem)
            LoadAvailableIndiciesForObjectType(ListBoxControl1.SelectedItem)
            EnableControls(Panel1)
        End If
    End Sub

    'Private Sub TBGI_OBJECTTYPE_EMAIL_INDEXBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles TBGI_OBJECTTYPE_EMAIL_INDEXBindingSource.CurrentChanged
    '    If ListBoxControl1.SelectedIndex <> -1 Then
    '        LoadCurrentIndiciesForObjectType(ListBoxControl1.SelectedItem)
    '        LoadAvailableIndiciesForObjectType(ListBoxControl1.SelectedItem)
    '        EnableControls(Panel1)
    '    End If
    'End Sub


    Private Sub EnableControls(Panel As Panel)
        For Each oControl As Control In Panel.Controls
            oControl.Enabled = True
        Next
    End Sub


    Sub LoadAvailableIndiciesForObjectType(Otype As String)
        Try
            IDX_EMAIL_DATE_INComboBox.Items.Clear()
            IDX_EMAIL_FROMComboBox.Items.Clear()
            IDX_EMAIL_IDComboBox.Items.Clear()
            IDX_EMAIL_SUBJECTComboBox.Items.Clear()
            IDX_EMAIL_TOComboBox.Items.Clear()
            IDX_CHECK_ATTACHMENTComboBox.Items.Clear()
            If SourceAttributes Is Nothing Then
                SourceAttributes = WINDREAM.GetIndiciesByObjecttype(Otype)
            End If

            If SourceAttributes IsNot Nothing Then
                For Each index As String In SourceAttributes
                    Select Case WINDREAM.GetIndexType(index)
                        Case 4 'Boolean
                            Me.IDX_CHECK_ATTACHMENTComboBox.Items.Add(index)
                        Case Else
                            Me.IDX_EMAIL_DATE_INComboBox.Items.Add(index)
                            Me.IDX_EMAIL_FROMComboBox.Items.Add(index)
                            Me.IDX_EMAIL_IDComboBox.Items.Add(index)
                            Me.IDX_EMAIL_SUBJECTComboBox.Items.Add(index)
                            Me.IDX_EMAIL_TOComboBox.Items.Add(index)
                    End Select
                Next
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error in load_WDIndicesemail: ")
        End Try
    End Sub

    Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
        Me.TBGI_OBJECTTYPE_EMAIL_INDEXBindingSource.AddNew()
    End Sub

    Private Sub BarButtonItem2_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        Try
            Me.TBGI_OBJECTTYPE_EMAIL_INDEXBindingSource.EndEdit()
            If MyDataset.TBGI_OBJECTTYPE_EMAIL_INDEX.GetChanges Is Nothing = False Then
                CHANGED_WHOTextBox2.Text = Environment.UserName
                TBGI_OBJECTTYPE_EMAIL_INDEXBindingSource.EndEdit()
                TBGI_OBJECTTYPE_EMAIL_INDEXTableAdapter.Update(MyDataset.TBGI_OBJECTTYPE_EMAIL_INDEX)
            End If
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox("Fehler beim Speichern der E-Mail Einstellungen:" & vbNewLine & ex.Message, MsgBoxStyle.Critical, Text)
        End Try
    End Sub

    Private Sub BarButtonItem4_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem4.ItemClick
        Try
            Dim oSQL = $"DELETE FROM TBGI_OBJECTTYPE_EMAIL_INDEX WHERE OBJECTTYPE = '{OBJECTTYPETextBox.Text}'"
            DATABASE_ECM.ExecuteNonQuery(oSQL)
            TBGI_OBJECTTYPE_EMAIL_INDEXTableAdapter.Fill(MyDataset.TBGI_OBJECTTYPE_EMAIL_INDEX, OBJECTTYPETextBox.Text)
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox("Fehler beim Löschen der E-Mail Einstellungen:" & vbNewLine & ex.Message, MsgBoxStyle.Critical, Text)
        End Try
    End Sub

    Private Sub BarButtonItem3_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick
        ObjektTypenEintragen()
    End Sub

    Private Sub ObjektTypenEintragen()
        Try
            ListBoxControl1.Items.Clear()
            If SourceObjectTypes Is Nothing Then
                SourceObjectTypes = WINDREAM.ObjectTypes
            End If

            For Each otype As String In SourceObjectTypes
                ListBoxControl1.Items.Add(otype)
            Next
        Catch ex As Exception
            MsgBox("Fehlernachricht:" & vbNewLine & ex.Message, MsgBoxStyle.Critical, "Unexpected error inm Eintragen der Objekttypen")
        End Try
    End Sub

    Private Sub TBGI_OBJECTTYPE_EMAIL_INDEXBindingSource_AddingNew(sender As Object, e As AddingNewEventArgs) Handles TBGI_OBJECTTYPE_EMAIL_INDEXBindingSource.AddingNew
        MyDataset.TBGI_OBJECTTYPE_EMAIL_INDEX.ADDED_WHOColumn.DefaultValue = Environment.UserName
        MyDataset.TBGI_OBJECTTYPE_EMAIL_INDEX.OBJECTTYPEColumn.DefaultValue = ListBoxControl1.Text
    End Sub
End Class