Imports DevExpress.Utils
Imports DevExpress.Utils.Svg
Imports DevExpress.XtraGrid
Imports DigitalData.GUIs.ZooFlow.Search
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging

Public Class frmListSearches
    Private ReadOnly LogConfig As LogConfig
    Private ReadOnly Logger As Logger
    Private ReadOnly Database As MSSQLServer
    Private SearchLoader As SearchLoader
    Private Searches As List(Of SavedSearch.SavedSearch)

    Public Sub New(pLogConfig As LogConfig, pDatabase As MSSQLServer)
        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        LogConfig = pLogConfig
        Logger = pLogConfig.GetLogger
        Database = pDatabase
    End Sub

    Private Sub frmListSearches_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SearchLoader = New SearchLoader(My.LogConfig, My.DatabaseIDB, My.Application.User, frmSearchFlow.SvgImageCollection1)
        Searches = SearchLoader.LoadCustomSearches()
        GridControlSearches.DataSource = Searches
        GridViewSearches.FocusedRowHandle = GridControl.InvalidRowHandle

        Dim oImages = frmEditSearch.Images
        For Each oImage As SvgImageInfo In oImages.ImageInfos
            Dim oItem = New DevExpress.XtraEditors.Controls.ImageComboBoxItem(oImage.Name, oImage.Bitmap)
            cmbImages.Properties.Items.Add(oItem)
        Next
    End Sub

    Private Sub GridViewSearches_FocusedRowChanged(sender As Object, e As Views.Base.FocusedRowChangedEventArgs) Handles GridViewSearches.FocusedRowChanged
        Dim oSearch As SavedSearch.CustomSearch = GridViewSearches.GetRow(GridViewSearches.FocusedRowHandle)

        If oSearch IsNot Nothing Then
            txtTitle.EditValue = oSearch.Name
            txtDescription.EditValue = oSearch.Description
            peSearchImage.EditValue = oSearch.Image

            btnDelete.Enabled = True
            btnSave.Enabled = True
        Else
            btnDelete.Enabled = False
            btnSave.Enabled = False
        End If
    End Sub

    Private Sub cmbImages_SelectedValueChanged(sender As Object, e As EventArgs) Handles cmbImages.SelectedValueChanged
        Dim oImage As SvgBitmap = cmbImages.EditValue
        peSearchImage.EditValue = oImage.SvgImage
    End Sub

    Private Sub btnSave_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSave.ItemClick
        Dim oSearch As SavedSearch.CustomSearch = GridViewSearches.GetRow(GridViewSearches.FocusedRowHandle)

        If oSearch IsNot Nothing Then
            oSearch.Name = txtTitle.EditValue
            oSearch.Description = txtDescription.EditValue
            oSearch.Image = peSearchImage.EditValue

            SearchLoader.SaveSearch(oSearch)
        End If
    End Sub

    Private Sub btnDelete_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnDelete.ItemClick

    End Sub
End Class