Imports DD_LIB_Standards
Imports DevExpress.Data.Helpers
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Controls

Public Class frmWM_ObjecttypeConfig
    Private Shared _Instance As frmWM_ObjecttypeConfig = Nothing

    Public Shared Function Instance() As frmWM_ObjecttypeConfig
        If _Instance Is Nothing OrElse _Instance.IsDisposed = True Then
            _Instance = New frmWM_ObjecttypeConfig
        End If
        _Instance.BringToFront()
        Return _Instance
    End Function
    Private Sub TBPMO_WD_OBJECTTYPEBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TBPMO_WD_OBJECTTYPEBindingNavigatorSaveItem.Click
        Save_ObjecttypeKonfig()
    End Sub
    Private Sub Save_ObjecttypeKonfig()
        Try
            TBPMO_WD_OBJECTTYPEBindingSource.EndEdit()

            If IsNothing(DD_DMSDataSet.TBPMO_WD_OBJECTTYPE.GetChanges()) = False Then
                CHANGED_WHOTextBox.Text = USER_USERNAME
                TBPMO_WD_OBJECTTYPEBindingSource.EndEdit()
                TBPMO_WD_OBJECTTYPETableAdapter.Update(DD_DMSDataSet.TBPMO_WD_OBJECTTYPE)
                lblSaveDokart.Visible = True
            Else
                lblSaveDokart.Visible = False
            End If
        Catch ex As Exception
            MsgBox("Error in Save Objecttype-Config:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Private Sub frmObjecttypeConfig_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            TBPMO_WD_OBJECTTYPETableAdapter.Connection.ConnectionString = MYDB_ECM.CurrentConnectionString
            TBPMO_WD_OBJECTTYPETableAdapter.Fill(DD_DMSDataSet.TBPMO_WD_OBJECTTYPE)

            LoadObjectTypes()
            LoadIndices()
        Catch ex As Exception
            MsgBox("Error loading Form:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Private Sub LoadObjectTypes()
        Try
            Dim oDokumentTyp As WINDREAMLib.WMObject
            ' Combobox leeren
            OBJECT_TYPEComboBox.Items.Clear()
            ' alle Objekttypen durchlaufen
            For Each oDokumentTyp In clsWD_GET.GetObjecttypesAsObjects
                ' und in die Combobox eintragen
                OBJECT_TYPEComboBox.Items.Add(oDokumentTyp.aName)
            Next
        Catch ex As Exception
            MsgBox("Fehlernachricht:" & vbNewLine & ex.Message, MsgBoxStyle.Critical, "Fehler beim Eintragen der Objekttypen")
        End Try

    End Sub

    Private Sub OBJECT_TYPEComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles OBJECT_TYPEComboBox.SelectedIndexChanged
        LoadIndices()

        ' Doctype Combobox Text leeren wenn der aktuell ausgewählte Index nicht im aktuellen Objekttyp vorhanden ist
        If ComboboxContains(cmbDoctype, cmbDoctype.Text) = False Then
            cmbDoctype.Text = String.Empty
        End If

        ' Relation Combobox Text leeren wenn der aktuell ausgewählte Index nicht im aktuellen Objekttyp vorhanden ist
        If ComboboxContains(cmbADDIRelIdx, cmbADDIRelIdx.Text) = False Then
            cmbADDIRelIdx.Text = String.Empty
        End If
    End Sub

    Private Function ComboboxContains(combobox As System.Windows.Forms.ComboBox, toFind As String)
        Try
            Dim found = False

            For Each item As String In combobox.Items
                If item = toFind Then
                    found = True
                End If
            Next

            Return found
        Catch ex As Exception
            MsgBox("Error in ComboboxContains: " & ex.Message, MsgBoxStyle.Exclamation)
            Return False
        End Try
    End Function

    Private Sub LoadIndices()
        Try
            If OBJECT_TYPEComboBox.Text.Count > 0 Then
                cmbDoctype.Items.Clear()
                cmbADDIRelIdx.Items.Clear()
                Dim indexe = clsWD_GET.GetIndicesByObjecttype(OBJECT_TYPEComboBox.Text)
                If indexe IsNot Nothing Then
                    For Each index As String In indexe
                        cmbDoctype.Items.Add(index)
                        cmbADDIRelIdx.Items.Add(index)
                    Next
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error in LoadIndices: ")
        End Try
    End Sub

    Private Sub TBPMO_WD_OBJECTTYPEBindingSource_AddingNew(sender As Object, e As ComponentModel.AddingNewEventArgs) Handles TBPMO_WD_OBJECTTYPEBindingSource.AddingNew
        Try
            DD_DMSDataSet.TBPMO_WD_OBJECTTYPE.ADDED_WHOColumn.DefaultValue = USER_USERNAME
        Catch ex As Exception
            MsgBox("Error in Save Objecttype-Config:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
End Class