Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Patterns
Imports DigitalData.Modules.Language.Utils
Imports DigitalData.GUIs.Common
Imports DevExpress.XtraGrid.Views.Grid

Public Class frmSQLEditor
    Public SQLString As String

    Private LogConfig As LogConfig
    Private SQLServer As MSSQLServer
    Private CurrentPosition As String

    Private Placeholders As New List(Of Placeholder)
    Private Connections As New List(Of Connection)

    Private Patterns As ClassPatterns

    Friend Class Placeholder
        Public Property Placeholder As String
        Public Property Replacement As String
    End Class

    Friend Class Connection
        Public Property Guid As Integer
        Public Property Name As String
        Public Property Description As String
        Public Property Provider As String
        Public Property Server As String
        Public Property Database As String
        Public Property Username As String
        Public Property Password As String

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

    Public Sub New(LogConfig As LogConfig, SQLServer As MSSQLServer)
        InitializeComponent()

        Me.LogConfig = LogConfig
        Me.SQLServer = SQLServer
        Patterns = New ClassPatterns(LogConfig)
    End Sub

    Private Sub frmSQLEditor_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            InitializeBaseForm(LogConfig)

            txtSQL.Text = SQLString

            BindingSourcePlaceholder.DataSource = Placeholders
            Placeholders.Clear()

            Connections = Load_SQLConnections()

            Dim oViews As New List(Of GridView) From {GridViewResult, GridViewPlaceholder}
            Dim oGridBuilder As New GridBuilder(oViews)
            oGridBuilder.
                WithDefaults().
                WithReadOnlyOptions(GridViewResult)

            cmbConnections.Properties.Items.Clear()
            For Each oConnection In Connections
                cmbConnections.Properties.Items.Add(oConnection)
            Next
        Catch ex As Exception
            ShowErrorMessage(ex)
        End Try
    End Sub

    Private Function Load_SQLConnections()
        Try
            Dim oSQL As String = "SELECT * FROM TBDD_CONNECTION"
            Dim oTable As DataTable = SQLServer.GetDatatable(oSQL)
            Dim oConnections As New List(Of Connection)

            For Each oRow As DataRow In oTable.Rows
                oConnections.Add(New Connection With {
                    .Guid = NotNull(oRow.Item("GUID"), String.Empty),
                    .Name = NotNull(oRow.Item("BEZEICHNUNG"), String.Empty),
                    .Provider = NotNull(oRow.Item("SQL_PROVIDER"), String.Empty),
                    .Database = NotNull(oRow.Item("DATENBANK"), String.Empty),
                    .Description = NotNull(oRow.Item("BEMERKUNG"), String.Empty),
                    .Password = NotNull(oRow.Item("PASSWORD"), String.Empty),
                    .Server = NotNull(oRow.Item("SERVER"), String.Empty),
                    .Username = NotNull(oRow.Item("USERNAME"), String.Empty)
                })
            Next

            Return oConnections
        Catch ex As Exception
            ShowErrorMessage(ex)
            Return New List(Of Connection)
        End Try
    End Function

    Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
        SQLString = txtSQL.Text
        DialogResult = DialogResult.OK
        Close()
    End Sub

    Private Sub BarButtonItem2_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        If cmbConnections.SelectedItem Is Nothing Then
            ShowErrorMessage("Bitte wählen sie eine Datenbank Verbindung!")
            Exit Sub
        End If

        Try
            Dim oSQL As String = txtSQL.EditValue.ToString
            Dim oConnection As Connection = cmbConnections.SelectedItem
            Dim oConnectionString = SQLServer.GetConnectionString(oConnection.Server, oConnection.Database, oConnection.Username, oConnection.Password)
            Dim oTable As DataTable = SQLServer.GetDatatableWithConnection(oSQL, oConnectionString)

            GridControlResult.DataSource = oTable
        Catch ex As Exception
            ShowErrorMessage(ex)
        End Try
    End Sub

    Private Sub txtSQL_EditValueChanged(sender As Object, e As EventArgs) Handles txtSQL.EditValueChanged
        CurrentPosition = txtSQL.SelectionStart
        Dim oValue = txtSQL.EditValue.ToString.Trim

        If oValue.Length > 0 Then
            GridControlPlaceholders.Enabled = True
            CheckForPlaceholders(txtSQL.EditValue)
        Else
            GridControlPlaceholders.Enabled = False
        End If
    End Sub

    Private Sub CheckForPlaceholders(Text As String)
        Placeholders.Clear()

        For Each oPattern In Patterns.GetAllPatterns(Text)
            Placeholders.Add(New Placeholder With {
                .Placeholder = oPattern.ToString,
                .Replacement = ""
            })
        Next
    End Sub
End Class
