Imports DigitalData.GUIs.Common

Public Class frmAdmin_IDBObjectStore
    Implements IAdminForm

    Public Property PrimaryKey As Integer Implements IAdminForm.PrimaryKey


    Public Property HasChanges As Boolean Implements IAdminForm.HasChanges

    Private FormHelper As FormHelper

    Public Property IsInsert As Boolean Implements IAdminForm.IsInsert
    Dim oStorePath As String = String.Empty

    Public Function DeleteData() As Boolean Implements IAdminForm.DeleteData
        Throw New NotImplementedException()
    End Function
    Public Sub New(PrimaryKey As Integer, Optional IsInsert As Boolean = False)
        ' 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 BarButtonItem3_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick

    End Sub



    Private Sub frmAdmin_IDBObjectStore_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.TBIDB_OBJECT_STORETableAdapter.Connection.ConnectionString = My.DatabaseIDB.CurrentSQLConnectionString
        Me.TBIDB_CATALOGTableAdapter.Connection.ConnectionString = My.DatabaseIDB.CurrentSQLConnectionString
        Me.TBIDB_OBJECT_STORETableAdapter.Fill(Me.DSIDB_Stammdaten.TBIDB_OBJECT_STORE)
        If IsInsert Then
            TBIDB_OBJECT_STOREBindingSource.AddNew()
            ADDED_WHOTextBox.Text = My.Application.User.UserName
        Else
            If CAT_IDTextBox.Text <> String.Empty Then
                Me.TBIDB_CATALOGTableAdapter.Fill(Me.DSIDB_Stammdaten.TBIDB_CATALOG, CType(CAT_IDTextBox.Text, Integer))
                oStorePath = CAT_TITLETextBox.Text
            End If
        End If


    End Sub

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

        If SaveData() Then
            Close()
        End If
    End Sub

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

        If SaveData() And HasChanges Then
            ShowStatus("ObjectStore gespeichert!")
        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
    Public Function SaveData() As Boolean
        Try
            TBIDB_OBJECT_STOREBindingSource.EndEdit()

            If DSIDB_Stammdaten.TBIDB_OBJECT_STORE.GetChanges() IsNot Nothing Then
                HasChanges = True

                If IsInsert Then
                    Dim oSQL = $"INSERT INTO TBIDB_CATALOG (CAT_TITLE, CAT_STRING, ADDED_WHO) VALUES ('FILESTORE_' + '{OBJECT_TITLETextBox.Text}','{CAT_TITLETextBox.Text}','{My.Application.User.UserName}')"
                    If My.DatabaseIDB.ExecuteNonQuery(oSQL) Then
                        Dim oID = $"SELECT GUID FROM TBIDB_CATALOG WHERE CAT_TITLE = 'FILESTORE_' + '{OBJECT_TITLETextBox.Text}'"
                        oID = My.DatabaseIDB.GetScalarValue(oID)
                        CAT_IDTextBox.Text = oID
                        TBIDB_OBJECT_STOREBindingSource.EndEdit()
                    End If

                Else
                    If oStorePath <> CAT_TITLETextBox.Text Then
                        Dim oSQL = $"UPDATE TBIDB_CATALOG SET CAT_TITLE = 'FILESTORE_' & '{OBJECT_TITLETextBox.Text}', CAT_STRING = {CAT_TITLETextBox.Text}, ADDED_WHO = '{My.Application.User.UserName}'" &
                                    $"WHERE (GUID = {CAT_IDTextBox.Text})"
                        My.DatabaseIDB.ExecuteNonQuery(oSQL)
                    End If
                    CHANGED_WHOTextBox.Text = My.Application.User.UserName
                End If

                TBIDB_OBJECT_STOREBindingSource.EndEdit()

                ' TODO: Update Database
                TBIDB_OBJECT_STORETableAdapter.Update(DSIDB_Stammdaten.TBIDB_OBJECT_STORE)
            End If

            Return True
        Catch ex As Exception
            FormHelper.ShowErrorMessage(ex, "SaveData")
            Return False
        End Try
    End Function
End Class