Imports System.IO
Imports Independentsoft
Imports System.Threading

Public Class ClassFolderWatcher
    Public Shared FolderWatcher_SCAN As FileSystemWatcher
    Public Shared Function Restart_FolderWatchSCAN()
        Try
            If FolderWatcher_SCAN.EnableRaisingEvents = True Then
                'Gestartet also Stoppen
                FolderWatcher_SCAN.EnableRaisingEvents = False
                FWSCAN_started = 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
                FWSCAN_started = True
                SaveMySettingsValue("FWSCAN_started", "True", "ConfigMain")

            End If
        Catch ex As Exception
            MsgBox("Error in Restart_FolderWatchSCAN:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
      
    End Function
    Public Shared Function StartStop_FolderWatchSCAN()
        Try
            If CURRENT_SCAN_FOLDERWATCH = "" Then
                MsgBox("Bitte definieren Sie einen Überwachungsordner für Scan-Eingänge:", MsgBoxStyle.Exclamation)
                Return False
            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
                FWSCAN_started = True
                SaveMySettingsValue("FWSCAN_started", "True", "ConfigMain")
                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
                FWSCAN_started = True
                SaveMySettingsValue("FWSCAN_started", "True", "ConfigMain")
                Return 1
            Else
                'Gestartet also Stoppen
                FolderWatcher_SCAN.EnableRaisingEvents = False
                FWSCAN_started = False
                ClassLogger.Add(" >> FolderWatch Scan gestoppt", False)
                SaveMySettingsValue("FWSCAN_started", "False", "ConfigMain")
                Return 0
            End If

        Catch ex As Exception
            MsgBox("Error in StartStop_FolderWatchSCAN:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return 99
        End Try
    End Function
    Private Shared Sub OnCreated(source As Object, e As FileSystemEventArgs)
        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.EndsWith(".msg") Then
                handleType = "SCAN_OUTLOOK_MESSAGE"
            Else
                handleType = "SCAN"
            End If
            'Die Datei übergeben
            If LogErrorsOnly = False Then ClassLogger.Add(">> OnCreated-File:" & e.FullPath, False)
            If FileExistsinDropTable(CURRENT_FILENAME) = False Then
                Insert_SCAN_File(e.FullPath, handleType)
            Else
                Console.WriteLine("File existiert bereits")
            End If
          
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler bei folder_watch_Created")
        End Try

    End Sub
    Public Shared Function FileExistsinDropTable(Filename As String)
        Dim check As String
        Try
            check = "SELECT COUNT(*) FROM TBPMO_FILES_USER WHERE UPPER(FILENAME2WORK) = UPPER('" & Filename & "') AND WORKED = 0"
            Dim result = ClassDatabase.Execute_Scalar(check, True)
            Return result
        Catch ex As Exception
            MsgBox("Error in FileExistsinDropTable - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & check, MsgBoxStyle.Critical)
            Return Nothing
        End Try

    End Function
    Private Shared Function Insert_SCAN_File(filename As String, handleType As String)
        Try
            Dim filename_only As String = Path.GetFileName(filename)

            Dim ins As String = String.Format("INSERT INTO TBPMO_FILES_USER (FILENAME2WORK, USER_WORK,HANDLE_TYPE,FILENAME_ONLY) VALUES ('{0}','{1}','{2}','{3}')", filename, Environment.UserName, handleType, filename_only)
            Return ClassDatabase.Execute_non_Query(ins, True)
        Catch ex As Exception
            MsgBox("Unexpected Error in Insert Scan-File: " & ex.Message, MsgBoxStyle.Critical)
            Return False
        End Try
    End Function
   
End Class
