Imports DevExpress.XtraEditors
Imports DigitalData.Modules.Language
Imports DigitalData.Modules.Logging

Public Class frmConfigBasic
    Private FormLoaded As Boolean = False
    Private Logger As Logger
    Dim oFormLoaded As Boolean = False

    Private Sub frmConfigBasic_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Logger = My.LogConfig.GetLogger()
        ToggleSwitchDebug.IsOn = My.LogConfig.Debug

        For Each oRow As DataRow In My.Tables.DTIDB_CATALOG_USER.Rows
            Dim oId = oRow.Item("GUID")
            Dim oTitle As String = oRow.Item("CAT_TITLE")
            Dim oValue As String = oRow.ItemEx("CAT_STRING", String.Empty)

            Select Case oTitle
                Case ClassConstants.USER_CATALOG_QUICKSEARCH1_TITLE
                    Quicksearch1TitleTextBox.Text = oValue
                    Quicksearch1TitleTextBox.Tag = oId

                Case ClassConstants.USER_CATALOG_QUICKSEARCH1_POS
                    Quicksearch1CB_Pos.EditValue = oValue
                    Quicksearch1CB_Pos.Tag = oId

                Case ClassConstants.USER_AD_ROOT_PATH
                    ADRootPathTextBox.EditValue = oValue
                    ADRootPathTextBox.Tag = oId

                Case ClassConstants.USER_CATALOG_APPLICATION_THEME
                    Dim oItem = SkinPaletteRibbonGalleryBarItem1.Gallery.GetAllItems().
                        Where(Function(item) item.Value = oValue).
                        FirstOrDefault()

                    If Not IsNothing(oItem) Then
                        SkinPaletteRibbonGalleryBarItem1.Gallery.SetItemCheck(oItem, True)
                    End If
            End Select
        Next

        If My.SystemConfig.ProductPaths.ProcessManagerPath IsNot Nothing Then
            ProcessManagerPathTextbox.EditValue = My.SystemConfig.ProductPaths.ProcessManagerPath
        End If

        oFormLoaded = True
    End Sub

    Private Sub frmConfigBasic_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        FormLoaded = True
    End Sub

    Private Sub BarButtonItem2_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        Process.Start(My.LogConfig.LogDirectory)
    End Sub

    Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
        Process.Start(Application.LocalUserAppDataPath)
    End Sub


    Private Sub SaveCatalogSetting(pKey As Integer, pValue As String)
        Try
            Dim oUpd = $"UPDATE TBIDB_CATALOG_USER SET CHANGED_WHO = '{My.Application.User.UserName}', CAT_STRING  = '{pValue}'
            WHERE GUID  = {pKey}"

            If My.DatabaseIDB.ExecuteNonQuery(oUpd) = True Then
                bsiInfo.Caption = "Position Quicksearch1 saved!"
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub TextEdit_Leave(sender As Object, e As EventArgs) Handles _
        Quicksearch1TitleTextBox.Leave,
        ADRootPathTextBox.Leave,
        Quicksearch1CB_Pos.SelectedIndexChanged

        If FormLoaded = False Then
            Exit Sub
        End If

        Dim oTextbox As BaseEdit = sender
        SaveCatalogSetting(oTextbox.Tag, oTextbox.EditValue)
    End Sub


    Private Sub SkinPaletteRibbonGalleryBarItem1_GalleryItemClick(sender As Object, e As DevExpress.XtraBars.Ribbon.GalleryItemClickEventArgs) Handles SkinPaletteRibbonGalleryBarItem1.GalleryItemClick
        UpdateThemeSettings()
    End Sub

    Private Sub SkinRibbonGalleryBarItem1_GalleryItemClick(sender As Object, e As DevExpress.XtraBars.Ribbon.GalleryItemClickEventArgs) Handles SkinRibbonGalleryBarItem1.GalleryItemClick
        Dim oDefaultPalette = SkinPaletteRibbonGalleryBarItem1.Gallery.GetAllItems().FirstOrDefault()

        If oDefaultPalette IsNot Nothing Then
            SkinPaletteRibbonGalleryBarItem1.Gallery.FocusedItem = oDefaultPalette
        End If

        UpdateThemeSettings()
    End Sub

    Private Sub UpdateThemeSettings()
        Try
            Dim oPalette = SkinPaletteRibbonGalleryBarItem1.Gallery.GetCheckedItem()
            Dim oSkin = SkinRibbonGalleryBarItem1.Gallery.GetCheckedItem()
            Dim oThemeString = ""

            If oSkin Is Nothing Then
                Exit Sub
            End If

            oThemeString = oSkin.Value

            If oPalette IsNot Nothing Then
                oThemeString &= "|" & oPalette.Value
            End If

            Dim oUpd = $"UPDATE TBIDB_CATALOG_USER SET CHANGED_WHO = '{My.Application.User.UserName}', CAT_STRING  = '{oThemeString}'
            WHERE CAT_TITLE = '{ClassConstants.USER_CATALOG_APPLICATION_THEME}' AND USR_ID = {My.Application.User.UserId}"
            If My.Database.ExecuteNonQuery(oUpd, DigitalData.Modules.EDMI.API.Constants.DatabaseType.IDB) = True Then
                bsiInfo.Caption = "Application Theme saved!"
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
        End Try
    End Sub

    Private Sub ToggleSwitchDebug_Toggled(sender As Object, e As EventArgs) Handles ToggleSwitchDebug.Toggled
        If oFormLoaded = False Then
            Exit Sub
        End If
        Dim oSwitch As ToggleSwitch = sender
        If oSwitch.IsOn Then
            My.LogConfig.Debug = True
        Else
            My.LogConfig.Debug = False
        End If

        My.UIConfigManager.Save()
        bsiInfo.Caption = "DEBUG saved!"
    End Sub

    Private Sub ProcessManagerPathTextbox_Leave(sender As Object, e As EventArgs) Handles ProcessManagerPathTextbox.Leave
        Dim oProcessManagerPath As String = Utils.NotNull(ProcessManagerPathTextbox.EditValue, String.Empty)

        If oProcessManagerPath.Length = 0 Then
            My.SystemConfig.ProductPaths.ProcessManagerPath = Nothing
            My.SystemConfigManager.Save()

            bsiInfo.Caption = "Process Manager path cleared!"
        Else
            If Not IO.File.Exists(oProcessManagerPath) Then
                Logger.Warn($"ProcessManager application path [{oProcessManagerPath}] does not exist!")

            End If

            My.SystemConfig.ProductPaths.ProcessManagerPath = oProcessManagerPath
            My.SystemConfigManager.Save()

            bsiInfo.Caption = "Process Manager path saved!"
        End If


    End Sub


End Class