Imports System.ComponentModel
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Controls
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraLayout
Imports DevExpress.XtraTab
Imports DigitalData.Controls.RegexEditor
Imports DigitalData.Controls.SQLEditor
Imports DigitalData.GUIs.Common
Imports DigitalData.Modules.Logging

Public Class frmAdmin_ClipboardWatcher
    Implements IAdminForm

    Public Property PrimaryKey As Integer Implements IAdminForm.PrimaryKey
    Public Property HasChanges As Boolean Implements IAdminForm.HasChanges
    Public Property IsInsert As Boolean Implements IAdminForm.IsInsert

    Private Const BUTTON_REGEX_PROFILE = "BUTTON_REGEX_PROFILE"
    Private Const BUTTON_SEARCH_SQL = "BUTTON_SEARCH_SQL"
    Private Const BUTTON_COUNT_SQL = "BUTTON_COUNT_SQL"

    Private Const TAB_PAGE_PROFILE = "TAB_PAGE_PROFILE"
    Private Const TAB_PAGE_DOCSEARCH = "TAB_PAGE_DOCSEARCH"
    Private Const TAB_PAGE_DATASEARCH = "TAB_PAGE_DATASEARCH"

    Private Const SEARCH_POSITION_PRIMARY As Integer = 0
    Private Const SEARCH_POSITION_SECONDARY As Integer = 1
    Private Const SEARCH_POSITION_TERTIARY As Integer = 2

    Private Const PROFILE_TYPE_DATA_DOCS As Integer = 0
    Private Const PROFILE_TYPE_DOCS_ONLY As Integer = 1
    Private Const PROFILE_TYPE_DATA_ONLY As Integer = 2

    Private Pages As ClassDetailPageManager
    Private FormHelper As FormHelper

    Friend Class ProfileType
        Public Property Id As Integer
        Public Property Name As String

        Public Overrides Function ToString() As String
            Return Name
        End Function
    End Class

    Friend Class SearchPosition
        Public Property Id As Integer
        Public Property Name As String

        Public Overrides Function ToString() As String
            Return Name
        End Function
    End Class

    Public Sub New(PrimaryKey As Integer)
        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        Me.PrimaryKey = PrimaryKey
        Me.IsInsert = IsInsert
        FormHelper = New FormHelper(My.LogConfig, Me)
    End Sub

    Private Sub frmAdmin_CWProfile_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            TBCW_PROFILESTableAdapter.Connection.ConnectionString = My.DatabaseECM.CurrentSQLConnectionString
            TBCW_PROFILESTableAdapter.Fill(DBCW_Stammdaten.TBCW_PROFILES, PrimaryKey)

            TBCW_PROF_DOC_SEARCHTableAdapter.Connection.ConnectionString = My.DatabaseECM.CurrentSQLConnectionString
            TBCW_PROF_DOC_SEARCHTableAdapter.Fill(DBCW_Stammdaten.TBCW_PROF_DOC_SEARCH, PrimaryKey)

            TBCW_PROF_DATA_SEARCHTableAdapter.Connection.ConnectionString = My.DatabaseECM.CurrentSQLConnectionString
            TBCW_PROF_DATA_SEARCHTableAdapter.Fill(DBCW_Stammdaten.TBCW_PROF_DATA_SEARCH, PrimaryKey)

            TBDD_CONNECTIONTableAdapter.Connection.ConnectionString = My.DatabaseECM.CurrentSQLConnectionString
            TBDD_CONNECTIONTableAdapter.Fill(DSDD_Stammdaten.TBDD_CONNECTION)

            ' Configure the GridViews with some default options
            Dim oViews As New List(Of GridView) From {GridViewDataSearch, GridViewDocSearch}
            Dim oGridBuilder As New GridBuilder(oViews)
            oGridBuilder.
                WithDefaults().
                WithReadOnlyOptions()

            ' Add Focus Handler to all controls in all LayoutControls
            Dim oLayoutControls = New List(Of LayoutControl) From {LayoutControlProfile, LayoutControlDocSearch, LayoutControlDataSearch}

            DBCW_Stammdaten.TBLOCAL_SEARCH_POSITION.Rows.Add(SEARCH_POSITION_PRIMARY, "Haupttabelle")
            DBCW_Stammdaten.TBLOCAL_SEARCH_POSITION.Rows.Add(SEARCH_POSITION_SECONDARY, "Erste Detailtabelle")
            DBCW_Stammdaten.TBLOCAL_SEARCH_POSITION.Rows.Add(SEARCH_POSITION_TERTIARY, "Zweite Detailtabelle")

            DBCW_Stammdaten.TBLOCAL_PROFILE_TYPE.Rows.Add(PROFILE_TYPE_DATA_DOCS, "Dokumente und Daten")
            DBCW_Stammdaten.TBLOCAL_PROFILE_TYPE.Rows.Add(PROFILE_TYPE_DOCS_ONLY, "Nur Dokumente")
            DBCW_Stammdaten.TBLOCAL_PROFILE_TYPE.Rows.Add(PROFILE_TYPE_DATA_ONLY, "Nur Daten")

            Pages = New ClassDetailPageManager(My.LogConfig, Me, oLayoutControls)
            Pages.AddRange({
                New ClassDetailPageManager.PrimaryPage(IsInsert) With {
                    .Name = "Profil",
                    .TabPage = PageProfile,
                    .BindingSource = TBCW_PROFILESBindingSource,
                    .DataTable = DBCW_Stammdaten.TBCW_PROFILES,
                    .AddedWhoEdit = txtAddedWho,
                    .ChangedWhoEdit = txtChangedWho
                },
                New ClassDetailPageManager.DetailPage With {
                    .Name = "Dokument-Suche",
                    .TabPage = PageDocumentSearch,
                    .BindingSource = TBCW_PROF_DOC_SEARCHBindingSource,
                    .DataTable = DBCW_Stammdaten.TBCW_PROF_DOC_SEARCH,
                    .AddedWhoEdit = txtAddedWho1,
                    .ChangedWhoEdit = txtChangedWho1
                },
                New ClassDetailPageManager.DetailPage With {
                    .Name = "Daten-Suche",
                    .TabPage = PageDataSearch,
                    .BindingSource = TBCW_PROF_DATA_SEARCHBindingSource,
                    .DataTable = DBCW_Stammdaten.TBCW_PROF_DATA_SEARCH,
                    .AddedWhoEdit = txtAddedWho11,
                    .ChangedWhoEdit = txtChangedWho11
                }
            })

            Pages.PrepareLoad()

            AddHandler Pages.CurrentPage_Changed, AddressOf CurrentPage_Changed
        Catch ex As Exception
            FormHelper.ShowErrorMessage(ex, "frmAdmin_CWProfile_Load")
        End Try
    End Sub

    Private Sub CurrentPage_Changed(sender As Object, e As ClassDetailPageManager.DetailPageEventArgs)
        If Not IsNothing(e.Page) Then
            If e.Page.IsPrimary = True Then
                BarButtonNew.Enabled = False
            Else
                BarButtonNew.Enabled = True
            End If

            RibbonPageGroup1.Text = e.Page.Name
        End If
    End Sub

    Private Sub ResetMessages()
        labelStatus.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
    End Sub

    Private Sub ShowStatus(Message As String)
        labelStatus.Caption = Message
        labelStatus.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
    End Sub

    Private Sub BarButtonSave_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonSave.ItemClick
        ResetMessages()

        If Pages.PrepareSave() = True Then
            Try
                Dim oPage = Pages.Current

                Select Case oPage.TabPage.Name
                    Case PageProfile.Name
                        TBCW_PROFILESTableAdapter.Update(oPage.DataTable)

                    Case PageDocumentSearch.Name
                        TBCW_PROF_DOC_SEARCHTableAdapter.Update(oPage.DataTable)

                    Case PageDataSearch.Name
                        TBCW_PROF_DATA_SEARCHTableAdapter.Update(oPage.DataTable)
                End Select

                oPage.IsInsert = False

                ShowStatus($"{oPage.Name} gespeichert!")
            Catch ex As Exception
                FormHelper.ShowErrorMessage(ex, "BarButtonSave_ItemClick")
            End Try
        Else
            ShowStatus("Keine Änderungen!")
        End If
    End Sub

    Public Function DeleteData() As Boolean Implements IAdminForm.DeleteData
        Throw New NotImplementedException()
    End Function

    Public Function AddData() As Boolean
        Dim oPage = Pages.Current

        If Pages.Current Is Nothing Then
            Return False
        End If

        If oPage.IsPrimary = False Then
            oPage.DataTable.Columns.Item("PROFILE_ID").DefaultValue = PrimaryKey
        End If

        Dim oNewRecord As DataRowView = oPage.BindingSource.AddNew()

        Return True
    End Function

    Private Sub TextEdit4_ButtonClick(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles TextEdit4.ButtonClick
        Dim oTextEdit As TextEdit = sender

        If e.Button.Tag = BUTTON_REGEX_PROFILE Then
            Dim oForm As New frmRegexEditor(oTextEdit.EditValue)
            Dim oResult = oForm.ShowDialog()

            If oResult = DialogResult.OK Then
                oTextEdit.EditValue = oForm.RegexString
            End If
        End If
    End Sub

    Private Sub TextEdit7_ButtonClick(sender As Object, e As ButtonPressedEventArgs) Handles TextEdit7.ButtonClick
        Dim oTextEdit As TextEdit = sender

        If e.Button.Tag = BUTTON_SEARCH_SQL Then
            Dim oForm As New frmSQLEditor(My.LogConfig, My.DatabaseECM) With {
                .SQLCommand = oTextEdit.EditValue
            }
            Dim oResult = oForm.ShowDialog()

            If oResult = DialogResult.OK Then
                oTextEdit.EditValue = oForm.SQLCommand
            End If
        End If
    End Sub

    Private Sub TextEdit8_ButtonClick(sender As Object, e As ButtonPressedEventArgs) Handles TextEdit8.ButtonClick
        Dim oTextEdit As TextEdit = sender

        If e.Button.Tag = BUTTON_COUNT_SQL Then
            Dim oForm As New frmSQLEditor(My.LogConfig, My.DatabaseECM) With {
                .SQLCommand = oTextEdit.EditValue
            }
            Dim oResult = oForm.ShowDialog()

            If oResult = DialogResult.OK Then
                oTextEdit.EditValue = oForm.SQLCommand
            End If
        End If
    End Sub

    Private Sub BarButtonNew_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonNew.ItemClick
        Pages.Current.IsInsert = True

        AddData()
    End Sub

    Private Sub XtraTabControl2_SelectedPageChanged(sender As Object, e As TabPageChangedEventArgs) Handles XtraTabControl2.SelectedPageChanged
        Dim oPage = Pages.GetDetailPage(e.Page)

        If oPage IsNot Nothing Then
            Pages.Current = oPage
        End If
    End Sub
End Class