Imports DevExpress.XtraEditors
Imports DigitalData.GUIs.Common
Imports DigitalData.Modules.Logging

Public Class frmGlobixBasicConfig
    Private Logger As Logger
    Private FormLoading As Boolean = False
    Private FolderWatcher As ClassFolderwatcher
    Private ReadOnly FileExclusions As New ClassExclusions()

    Public Sub New()

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub



    Sub CheckFolder(pMypath As String, FOLDER_TYPE As String)
        Try
            If pMypath = "" Then
                My.DatabaseECM.ExecuteNonQuery("DELETE FROM TBGI_FOLDERWATCH_USER WHERE USER_ID = " & My.Application.User.UserId & " AND FOLDER_TYPE = '" & FOLDER_TYPE & "'")
                If FOLDER_TYPE = "SCAN" Then
                    My.Application.Globix.CURRENT_SCAN_FOLDERWATCH = ""
                    My.UIConfig.Globix.FolderWatchScanStarted = False
                    My.UIConfigManager.Save()
                Else
                    My.Application.Globix.Folderwatchstarted = False
                    'SaveConfigValue("FW_started", "False")
                    My.UIConfig.Globix.FolderWatchStarted = False
                    My.UIConfigManager.Save()
                    My.Application.Globix.CurrentFolderWatchPath = ""
                End If

                Exit Sub
            End If
            Try
                If (Not System.IO.Directory.Exists(pMypath)) Then
                    System.IO.Directory.CreateDirectory(pMypath)
                End If
            Catch ex As Exception
                Logger.Info(">> Unexpected error in CheckFolder: " & pMypath)
                Logger.Error(ex.Message)
                Logger.Info(" >> " & ex.Message)
                Dim otext As String

                If My.Application.User.Language = "de-DE" Then
                    otext = "Unexpected error in ECheckFolder: " & pMypath & vbNewLine & "Bitte überprüfen Sie die Rechte!" & vbNewLine & ex.Message
                Else
                    otext = "Error in creating Hotfolder: " & pMypath & vbNewLine & "Please check the rights!" & vbNewLine & ex.Message
                End If
                MsgBox("No Match- There might be an error in the RegEx!", MsgBoxStyle.Exclamation)
                Exit Sub
            End Try

            Dim folderwatch = My.DatabaseECM.GetScalarValue("SELECT COALESCE(MAX(GUID),999999) FROM TBGI_FOLDERWATCH_USER WHERE USER_ID = " & My.Application.User.UserId & " AND FOLDER_TYPE = '" & FOLDER_TYPE & "'")
            Dim oSql As String
            If folderwatch = 999999 Then
                oSql = "INSERT INTO TBGI_FOLDERWATCH_USER (USER_ID, FOLDER_PATH, FOLDER_TYPE, ADDED_WHO) VALUES (" & My.Application.User.UserId & ",'" & pMypath & "','" & FOLDER_TYPE & "','" & My.Application.User.UserName & "')"
            Else
                oSql = "UPDATE TBGI_FOLDERWATCH_USER SET FOLDER_PATH = '" & pMypath & "', CHANGED_WHO = '" & My.Application.User.UserName & "' where GUID = " & folderwatch
            End If
            If My.DatabaseECM.ExecuteNonQuery(oSql) Then
                folderwatch = My.DatabaseECM.GetScalarValue("SELECT FOLDER_PATH FROM TBGI_FOLDERWATCH_USER WHERE USER_ID = " & My.Application.User.UserId & " AND FOLDER_TYPE = '" & FOLDER_TYPE & "'")

                If FOLDER_TYPE = "SCAN" Then
                    My.Application.Globix.CURRENT_SCAN_FOLDERWATCH = pMypath
                    Me.TextEditScanfolder.Text = My.Application.Globix.CURRENT_SCAN_FOLDERWATCH
                Else
                    My.Application.Globix.CurrentFolderWatchPath = pMypath
                    Me.TextEditHotfolder.Text = My.Application.Globix.CurrentFolderWatchPath
                End If
            Else
                Logger.Error($"Unexpected error in Adding Folder to TBGI_FOLDERWATCH_USER [{oSql}]")
            End If
            If My.Application.Globix.Folderwatchstarted = True And FOLDER_TYPE = "DEFAULT" Then
                FolderWatcher.Restart_FolderWatch()
            End If
            If My.UIConfig.Globix.FolderWatchScanStarted = True And FOLDER_TYPE = "SCAN" Then
                FolderWatcher.Restart_FolderWatchSCAN()
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error in CheckFolder:")
        End Try

    End Sub

    Private Sub frmGlobixBasicConfig_Load(sender As Object, e As EventArgs) Handles Me.Load
        Logger = My.LogConfig.GetLogger()
        FolderWatcher = New ClassFolderwatcher(My.LogConfig)
        Try
            FormLoading = True
            If My.Application.Globix.Folderwatchstarted = True Then
                ToggleSwitchHotfolder.IsOn = True
            Else
                ToggleSwitchHotfolder.IsOn = False
            End If

            If My.Application.Globix.ScanFolderwatchstarted = True Then
                ToggleSwitchScanfolder.IsOn = True
            Else
                ToggleSwitchScanfolder.IsOn = False
            End If

            Me.TextEditHotfolder.Text = My.Application.Globix.CurrentFolderWatchPath
            Me.TextEditScanfolder.Text = My.Application.Globix.CURRENT_SCAN_FOLDERWATCH


            FormLoading = False
            Me.DataGridView1.DataSource = My.Application.Globix.DTEXCLUDE_FILES
            If My.Application.GlobixDropAreaStyle = "PROGRESSIVE" Then
                Me.PictureEdit1.EditValue = Global.DigitalData.GUIs.ZooFlow.My.Resources.Resources._1_LOGO_ZOO_FLOW_DROP3
                ToggleSwitchDropArea.IsOn = False
            Else
                Me.PictureEdit1.EditValue = Global.DigitalData.GUIs.ZooFlow.My.Resources.Resources._1_LOGO_ZOO_FLOW_DROP2
                ToggleSwitchDropArea.IsOn = True
            End If
        Catch ex As Exception

        End Try
    End Sub


    Sub CheckFWHF_State()
        FolderWatcher.StartStop_FolderWatch()
    End Sub



    Private Sub SimpleButton1_Click(sender As Object, e As EventArgs) Handles SimpleButton1.Click

        My.Application.Globix.DTEXCLUDE_FILES.AcceptChanges()
        My.Application.Globix.DTEXCLUDE_FILES.WriteXml(FileExclusions.FileExclusionPath)
        MsgBox("Changes saved.", MsgBoxStyle.Information)
    End Sub


    Private Sub btnScanfolder_Click(sender As Object, e As EventArgs) Handles btnScanfolder.Click
        Dim fbdialog As New FolderBrowserDialog
        If TextEditScanfolder.Text <> "" Then
            fbdialog.SelectedPath = TextEditScanfolder.Text
        End If
        If fbdialog.ShowDialog() = DialogResult.OK Then
            CheckFolder(fbdialog.SelectedPath, "SCAN")
        End If
    End Sub
    Private Sub ToggleSwitchHotfolder_Toggled(sender As Object, e As EventArgs) Handles ToggleSwitchHotfolder.Toggled
        Dim oToggle As ToggleSwitch = TryCast(sender, ToggleSwitch)
        If FormLoading = True Then
            Exit Sub
        End If
        If oToggle.IsOn Then
            If My.Application.Globix.CurrentFolderWatchPath <> "" Then
                CheckFWHF_State()
            Else
                ToggleSwitchHotfolder.IsOn = False
            End If
        Else
            CheckFWHF_State()
        End If
    End Sub

    Private Sub ToggleSwitch1_Toggled(sender As Object, e As EventArgs) Handles ToggleSwitchScanfolder.Toggled
        Dim oToggle As ToggleSwitch = TryCast(sender, ToggleSwitch)
        If FormLoading = True Then
            Exit Sub
        End If
        If oToggle.IsOn Then
            If My.Application.Globix.CURRENT_SCAN_FOLDERWATCH <> "" Then
                FolderWatcher.StartStop_FolderWatchSCAN()
            Else
                ToggleSwitchScanfolder.IsOn = False
            End If
        Else
            FolderWatcher.StartStop_FolderWatchSCAN()
        End If

    End Sub

    Private Sub btnHotfolder_Click(sender As Object, e As EventArgs) Handles btnHotfolder.Click
        Dim fbdialog As New FolderBrowserDialog
        If TextEditHotfolder.Text <> "" Then
            fbdialog.SelectedPath = TextEditHotfolder.Text
        End If
        If fbdialog.ShowDialog() = DialogResult.OK Then
            CheckFolder(fbdialog.SelectedPath, "DEFAULT")
        End If
    End Sub

    Private Sub ToggleSwitch1_Toggled_1(sender As Object, e As EventArgs) Handles ToggleSwitchDropArea.Toggled
        Dim oToggle As ToggleSwitch = TryCast(sender, ToggleSwitch)
        If oToggle.IsOn Then
            Me.PictureEdit1.EditValue = Global.DigitalData.GUIs.ZooFlow.My.Resources.Resources._1_LOGO_ZOO_FLOW_DROP2
            Dim oUpd = $"UPDATE TBIDB_CATALOG_USER SET CHANGED_WHO = '{My.Application.User.UserName}', CAT_STRING  = 'ALTERNATIVE'
            WHERE USR_ID = {My.Application.User.UserId} AND CAT_TITLE  = '{ClassConstants.USER_CATALOG_GLOBIX_DROPAREA}'"
            If My.Database.ExecuteNonQuery(oUpd, DigitalData.Modules.EDMI.API.Constants.DatabaseType.IDB) = True Then
                My.Application.GlobixDropAreaStyle = "ALTERNATIVE"
                tsinfo.Text = "DropArea-Theme saved!"
            End If
        Else
            Me.PictureEdit1.EditValue = Global.DigitalData.GUIs.ZooFlow.My.Resources.Resources._1_LOGO_ZOO_FLOW_DROP3
            Dim oUpd = $"UPDATE TBIDB_CATALOG_USER SET CHANGED_WHO = '{My.Application.User.UserName}', CAT_STRING  = 'PROGRESSIVE'
            WHERE USR_ID = {My.Application.User.UserId} AND CAT_TITLE  = '{ClassConstants.USER_CATALOG_GLOBIX_DROPAREA}'"
            If My.Database.ExecuteNonQuery(oUpd, DigitalData.Modules.EDMI.API.Constants.DatabaseType.IDB) = True Then
                My.Application.GlobixDropAreaStyle = "PROGRESSIVE"
                tsinfo.Text = "DropArea-Theme saved to progressive!"
            End If
        End If
    End Sub


End Class