Imports DigitalData.Modules.Logging

Public Class frmGlobixBasicConfig
    Private Logger As Logger
    Private oReload As Boolean = False
    Private clsFW As ClassFolderwatcher
    Public Sub New()

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

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

    End Sub

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

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim fbdialog As New FolderBrowserDialog
        If txtScanFolderWatch.Text <> "" Then
            fbdialog.SelectedPath = txtScanFolderWatch.Text
        End If
        If fbdialog.ShowDialog() = DialogResult.OK Then
            CheckFolder(fbdialog.SelectedPath, "SCAN")
        End If
    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)
                If My.Application.User.Language = "de-DE" Then
                    MsgBox("Unexpected error in ECheckFolder: " & pMypath & vbNewLine & "Bitte überprüfen Sie die Rechte!" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
                Else
                    MsgBox("Error in creating Hotfolder: " & pMypath & vbNewLine & "Please check the rights!" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
                End If

                Exit Sub
            End Try

            Dim folderwatch = My.DatabaseECM.GetScalarValue("SELECT GUID FROM TBGI_FOLDERWATCH_USER WHERE USER_ID = " & My.Application.User.UserId & " AND FOLDER_TYPE = '" & FOLDER_TYPE & "'")
            Dim oSql As String
            If folderwatch Is Nothing 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 = folderwatch
                    Me.txtScanFolderWatch.Text = My.Application.Globix.CURRENT_SCAN_FOLDERWATCH
                Else
                    My.Application.Globix.CurrentFolderWatchPath = folderwatch
                    Me.txtFolderWatch.Text = My.Application.Globix.CurrentFolderWatchPath
                End If
            End If
            If My.Application.Globix.Folderwatchstarted = True And FOLDER_TYPE = "DEFAULT" Then
                clsFW.Restart_FolderWatch()
            End If
            If My.UIConfig.Globix.FolderWatchScanStarted = True And FOLDER_TYPE = "SCAN" Then
                clsFW.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()
        clsFW = New ClassFolderwatcher(My.LogConfig)
        Try
            oReload = True
            Dim oFolderwatch = My.DatabaseECM.GetScalarValue("SELECT FOLDER_PATH FROM TBGI_FOLDERWATCH_USER WHERE FOLDER_TYPE = 'DEFAULT' AND USER_ID = " & My.Application.User.UserId)
            If oFolderwatch IsNot Nothing Then
                My.Application.Globix.CurrentFolderWatchPath = oFolderwatch
            End If
            Me.txtFolderWatch.Text = My.Application.Globix.CurrentFolderWatchPath
            Dim oSCANFolderwatch = My.DatabaseECM.GetScalarValue("SELECT FOLDER_PATH FROM TBGI_FOLDERWATCH_USER WHERE FOLDER_TYPE = 'SCAN' AND USER_ID = " & My.Application.User.UserId)
            If oSCANFolderwatch IsNot Nothing Then
                My.Application.Globix.CURRENT_SCAN_FOLDERWATCH = oSCANFolderwatch
            End If
            Me.txtFolderWatch.Text = My.Application.Globix.CurrentFolderWatchPath
            Me.txtScanFolderWatch.Text = My.Application.Globix.CURRENT_SCAN_FOLDERWATCH

            If Not ClassFolderwatcher.FWFolderWatcher Is Nothing Then
                If ClassFolderwatcher.FWFolderWatcher.EnableRaisingEvents = True Then
                    If My.Application.User.Language = "de-DE" Then
                        btnstartstop1.Text = "Überwachung stoppen"
                    Else
                        btnstartstop1.Text = "Stop hotfolder"
                    End If

                    btnstartstop1.Image = My.Resources.bell_delete
                Else
                    If My.Application.User.Language = "de-DE" Then
                        btnstartstop1.Text = "Überwachung starten"
                    Else
                        btnstartstop1.Text = "Start hotfolder"
                    End If

                    btnstartstop1.Image = My.Resources.bell_go
                End If
            Else
                If My.Application.User.Language = "de-DE" Then
                    btnstartstop1.Text = "Überwachung starten"
                Else
                    btnstartstop1.Text = "Start hotfolder"
                End If


                btnstartstop1.Image = My.Resources.bell_go
            End If

            If Not ClassFolderwatcher.FWScan Is Nothing Then
                If ClassFolderwatcher.FWScan.EnableRaisingEvents = True Then
                    If My.Application.User.Language = "de-DE" Then
                        btnstartstop2.Text = "Überwachung stoppen"
                    Else
                        btnstartstop2.Text = "Stop hotfolder"
                    End If

                    btnstartstop2.Image = My.Resources.bell_delete
                Else
                    If My.Application.User.Language = "de-DE" Then
                        btnstartstop2.Text = "Überwachung starten"
                    Else
                        btnstartstop2.Text = "Stop hotfolder"
                    End If

                    btnstartstop2.Image = My.Resources.bell_go
                End If
            Else
                If My.Application.User.Language = "de-DE" Then
                    btnstartstop2.Text = "Überwachung starten"
                Else
                    btnstartstop2.Text = "Stop hotfolder"
                End If

                btnstartstop2.Image = My.Resources.bell_go
            End If
            oReload = False
            Me.DataGridView1.DataSource = My.Application.Globix.DTEXCLUDE_FILES
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtFolderWatch_TextChanged(sender As Object, e As EventArgs) Handles txtFolderWatch.TextChanged
        If oReload = True Then Exit Sub
        CheckFolder(txtFolderWatch.Text, "DEFAULT")
    End Sub

    Private Sub txtScanFolderWatch_TextChanged(sender As Object, e As EventArgs) Handles txtScanFolderWatch.TextChanged
        If oReload = True Then Exit Sub
        CheckFolder(txtScanFolderWatch.Text, "SCAN")
    End Sub

    Private Sub btnSaveExclusionFiles_Click(sender As Object, e As EventArgs) Handles btnSaveExclusionFiles.Click
        My.Application.Globix.DTEXCLUDE_FILES.AcceptChanges()
        My.Application.Globix.DTEXCLUDE_FILES.WriteXml(My.Application.Globix.PATH_FileExclusions)
        MsgBox("Changes saved.", MsgBoxStyle.Information)
    End Sub

    Private Sub btnstartstop2_Click(sender As Object, e As EventArgs) Handles btnstartstop2.Click
        If My.Application.Globix.CURRENT_SCAN_FOLDERWATCH <> "" Then
            CheckFWSCAN_State()
        End If
    End Sub
    Sub CheckFWSCAN_State()
        Select Case clsFW.StartStop_FolderWatchSCAN()
            Case 1
                If My.Application.User.Language = "de-DE" Then
                    btnstartstop2.Text = "Überwachung stoppen"
                Else
                    btnstartstop2.Text = "Stop Hotfolder"
                End If
                btnstartstop2.Image = My.Resources.bell_delete
            Case 0
                If My.Application.User.Language = "de-DE" Then
                    btnstartstop2.Text = "Überwachung starten"
                Else
                    btnstartstop2.Text = "Start Hotfolder"
                End If
                btnstartstop2.Image = My.Resources.bell_go
        End Select
    End Sub
End Class