Imports System.IO
Imports Independentsoft
Imports System.Threading

Public Class ClassFolderWatcher
    Public Shared FolderWatcher As FileSystemWatcher
    Public Shared FolderWatcher_SCAN As FileSystemWatcher
    Public Shared Function Restart_FolderWatch()
        Try
            If FolderWatcher.EnableRaisingEvents = True Then
                'Gestartet also Stoppen
                FolderWatcher.EnableRaisingEvents = False
                FW_started = False
                'FolderWatch neu instanzieren
                FolderWatcher = New System.IO.FileSystemWatcher(CURRENT_FOLDERWATCH, "*.*")
                ClassLogger.Add(" >> FolderWatch neu instanziert", False)
                FolderWatcher.IncludeSubdirectories = False
                FolderWatcher.EnableRaisingEvents = True
                AddHandler FolderWatcher.Created, AddressOf OnCreated
                FW_started = True
                'SaveConfigValue("FW_started", "True")
                CONFIG.Config.FolderWatchStarted = True
                CONFIG.Save()
            End If
        Catch ex As Exception
            ClassLogger.Add($"Error in Restart_FolderWatch: {ex.Message}", False)
        End Try
    End Function
    Public Shared Function Restart_FolderWatchSCAN()
        Try
            If FolderWatcher_SCAN.EnableRaisingEvents = True Then
                'Gestartet also Stoppen
                FolderWatcher_SCAN.EnableRaisingEvents = False
                'FolderWatch neu instanzieren
                FolderWatcher_SCAN = New System.IO.FileSystemWatcher(CURRENT_SCAN_FOLDERWATCH, "*.*")
                ClassLogger.Add(" >> FolderWatchScan neu instanziert", False)
                FolderWatcher_SCAN.IncludeSubdirectories = False
                FolderWatcher_SCAN.EnableRaisingEvents = True
                AddHandler FolderWatcher_SCAN.Created, AddressOf OnCreated
                'SaveConfigValue("FWSCAN_started", "True")
                CONFIG.Config.FolderWatchScanStarted = True
                CONFIG.Save()
            End If
        Catch ex As Exception
            ClassLogger.Add($"Error in Restart_FolderWatchSCAN: {ex.Message}", False)
        End Try
    End Function
    Public Shared Function StartStop_FolderWatch()
        Try
            If CURRENT_FOLDERWATCH = "" Then
                'MsgBox("Bitte definieren Sie einen Überwachungsordner:", MsgBoxStyle.Exclamation)
                Return False
            End If
            If FolderWatcher Is Nothing Then
                FolderWatcher = New System.IO.FileSystemWatcher(CURRENT_FOLDERWATCH, "*.*")
                ClassLogger.Add(" >> FolderWatch Gestartet", False)
                FolderWatcher.IncludeSubdirectories = False
                FolderWatcher.EnableRaisingEvents = True
                AddHandler FolderWatcher.Created, AddressOf OnCreated
                FW_started = True
                'SaveConfigValue("FW_started", "True")
                CONFIG.Config.FolderWatchStarted = True
                CONFIG.Save()
                Return 1
            End If
            If FolderWatcher.EnableRaisingEvents = False Then
                ' Dim watcher As New FileSystemWatcher()
                ' watcher.Path = CURRENT_FOLDERWATCH
                FolderWatcher = New System.IO.FileSystemWatcher(CURRENT_FOLDERWATCH, "*.*")
                ClassLogger.Add(" >> FolderWatch Gestartet", False)
                FolderWatcher.IncludeSubdirectories = False
                FolderWatcher.EnableRaisingEvents = True
                AddHandler FolderWatcher.Created, AddressOf OnCreated
                FW_started = True
                'SaveConfigValue("FW_started", "True")
                CONFIG.Config.FolderWatchStarted = True
                CONFIG.Save()
                Return 1
            Else
                'Gestartet also Stoppen
                FolderWatcher.EnableRaisingEvents = False
                FW_started = False
                ClassLogger.Add(" >> FolderWatch gestoppt", False)
                'SaveConfigValue("FW_started", "False")
                CONFIG.Config.FolderWatchStarted = False
                CONFIG.Save()
                Return 0
            End If
            'If watcher.EnableRaisingEvents = False Then
            '    watcher = New System.IO.FileSystemWatcher(CURRENT_FOLDERWATCH, "*.*")
            '    ClassLogger.Add(" - vFolderWatch.Gestartet", False)
            '    watcher.IncludeSubdirectories = False
            '    watcher.EnableRaisingEvents = True
            '    AddHandler watcher.Created, AddressOf OnCreated
            '    Return 1
            'Else
            '    'Gestartet also Stoppen
            '    watcher.EnableRaisingEvents = False
            '    Return 0
            'End If
        Catch ex As Exception
            MsgBox("Error in StartStop_FolderWatch:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return 99
        End Try
    End Function
    Public Shared Function StartStop_FolderWatchSCAN()
        Try
            If CURRENT_SCAN_FOLDERWATCH = "" Then
                If FolderWatcher.EnableRaisingEvents = True Then
                    Stop_FWSCAN()
                    Return 0
                Else
                    If USER_LANGUAGE = "de-DE" Then
                        MsgBox("Bitte definieren Sie einen Überwachungsordner für Scan-Eingänge:", MsgBoxStyle.Exclamation)
                    Else
                        MsgBox("Please define a watchfolder for Scanning:", MsgBoxStyle.Exclamation)
                    End If

                    Return False
                End If
            End If
            If FolderWatcher_SCAN Is Nothing Then
                FolderWatcher_SCAN = New System.IO.FileSystemWatcher(CURRENT_SCAN_FOLDERWATCH, "*.*")
                ClassLogger.Add(" >> FolderWatch Scan Gestartet", False)
                FolderWatcher_SCAN.IncludeSubdirectories = False
                FolderWatcher_SCAN.EnableRaisingEvents = True
                AddHandler FolderWatcher_SCAN.Created, AddressOf OnCreated
                CONFIG.Config.FolderWatchScanStarted = True
                CONFIG.Save()
                Return 1
            End If
            If FolderWatcher_SCAN.EnableRaisingEvents = False Then
                ' Dim watcher As New FileSystemWatcher()
                ' watcher.Path = CURRENT_FOLDERWATCH
                FolderWatcher_SCAN = New System.IO.FileSystemWatcher(CURRENT_SCAN_FOLDERWATCH, "*.*")
                ClassLogger.Add(" >> FolderWatch Scan Gestartet", False)
                FolderWatcher_SCAN.IncludeSubdirectories = False
                FolderWatcher_SCAN.EnableRaisingEvents = True
                AddHandler FolderWatcher_SCAN.Created, AddressOf OnCreated
                CONFIG.Config.FolderWatchScanStarted = True
                CONFIG.Save()
                Return 1
            Else
                'Gestartet also Stoppen
                FolderWatcher_SCAN.EnableRaisingEvents = False
                ClassLogger.Add(" >> FolderWatch Scan gestoppt", False)
                'SaveConfigValue("FWSCAN_started", "False")
                CONFIG.Config.FolderWatchScanStarted = False
                CONFIG.Save()
                Return 0
            End If
           
        Catch ex As Exception
            MsgBox("Error in StartStop_FolderWatchSCAN:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return 99
        End Try
    End Function
    Public Shared Function Stop_FWSCAN()
        If FolderWatcher.EnableRaisingEvents = True Then
            'Gestartet also Stoppen
            FolderWatcher.EnableRaisingEvents = False
            FW_started = False
            ClassLogger.Add(" >> FolderWatch gestoppt", False)
            'SaveConfigValue("FW_started", "False")
            CONFIG.Config.FolderWatchStarted = True
            CONFIG.Save()
            Return True
        Else
            Return False
        End If
    End Function
    Private Shared Sub OnCreated(source As Object, e As FileSystemEventArgs)
        If ClassDatabase.DatabaseConnectionTimeout = True Then
            ClassLogger.Add(">> File handling aborted because of database timeout error!", False)
            Exit Sub
        End If

        Try
            For Each row As DataRow In DTEXCLUDE_FILES.Rows
                Dim content As String = row.Item(0).ToString.ToLower
                If e.FullPath.ToLower.Contains(content) Then
                    Exit Sub
                End If
            Next

            Dim handleType As String
            If e.FullPath.ToLower.EndsWith(".msg") Then
                handleType = "|FW_OUTLOOK_MESSAGE|"
            Else
                handleType = "|FW_SIMPLEINDEXER|"
            End If
            'Die Datei übergeben
            If LogErrorsOnly = False Then ClassLogger.Add(">> OnCreated-File:" & e.FullPath, False)
            If ClassIndexFunctions.FileExistsinDropTable(e.FullPath) = False Then
                ClassFilehandle.Decide_FileHandle(e.FullPath, handleType)
            Else
                ClassLogger.Add(">> Folderwatcher: File already exists:" & e.FullPath, False)
            End If

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error in folder_watch_Created")
        End Try

    End Sub

End Class
