Imports System.IO
Imports System.Threading
Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Logging

Public Class ClassFolderWatcher
    Inherits BaseClass

    Public FolderWatcher As FileSystemWatcher
    Public FolderWatcher_SCAN As FileSystemWatcher

    Public Event FileCreated As EventHandler(Of FileSystemEventArgs)

    Public Sub New(pLogConfig As LogConfig)
        MyBase.New(pLogConfig)
    End Sub

    Private Sub OnCreated(source As Object, e As IO.FileSystemEventArgs)
        RaiseEvent FileCreated(source, e)
    End Sub

    Public Sub 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, "*.*")

                Logger.Info(" >> FolderWatch neu instanziert")
                FolderWatcher.IncludeSubdirectories = False
                FolderWatcher.EnableRaisingEvents = True
                AddHandler FolderWatcher.Created, AddressOf OnCreated
                FW_started = True
                CONFIG.Config.FolderWatchStarted = True
                CONFIG.Save()
            End If
        Catch ex As Exception
            Logger.Info($"Error in Restart_FolderWatch: {ex.Message}")
            Logger.Error(ex)
        End Try
    End Sub
    Public Sub 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, "*.*")
                Logger.Info(" >> FolderWatchScan neu instanziert")
                FolderWatcher_SCAN.IncludeSubdirectories = False
                FolderWatcher_SCAN.EnableRaisingEvents = True
                AddHandler FolderWatcher_SCAN.Created, AddressOf OnCreated
                CONFIG.Config.FolderWatchScanStarted = True
                CONFIG.Save()
            End If
        Catch ex As Exception
            Logger.Info($"Error in Restart_FolderWatchSCAN: {ex.Message}")
            Logger.Error(ex)
        End Try
    End Sub
    Public Function StartStop_FolderWatch() As Integer
        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, "*.*")
                Logger.Info(" >> FolderWatch Gestartet")
                FolderWatcher.IncludeSubdirectories = False
                FolderWatcher.EnableRaisingEvents = True
                AddHandler FolderWatcher.Created, AddressOf OnCreated
                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, "*.*")
                Logger.Info(" >> FolderWatch Gestartet")
                FolderWatcher.IncludeSubdirectories = False
                FolderWatcher.EnableRaisingEvents = True
                AddHandler FolderWatcher.Created, AddressOf OnCreated
                FW_started = True
                CONFIG.Config.FolderWatchStarted = True
                CONFIG.Save()
                Return 1
            Else
                'Gestartet also Stoppen
                FolderWatcher.EnableRaisingEvents = False
                FW_started = False
                Logger.Info(" >> FolderWatch gestoppt")
                CONFIG.Config.FolderWatchStarted = False
                CONFIG.Save()
                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 Function StartStop_FolderWatchSCAN() As Integer
        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 FileSystemWatcher(CURRENT_SCAN_FOLDERWATCH, "*.*")
                Logger.Info(" >> FolderWatch Scan Gestartet")
                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 FileSystemWatcher(CURRENT_SCAN_FOLDERWATCH, "*.*")
                Logger.Info(" >> FolderWatch Scan Gestartet")
                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
                Logger.Info(" >> FolderWatch Scan gestoppt")
                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 Function Stop_FWSCAN() As Boolean
        If FolderWatcher.EnableRaisingEvents = True Then
            'Gestartet also Stoppen
            FolderWatcher.EnableRaisingEvents = False
            FW_started = False
            Logger.Info(" >> FolderWatch gestoppt")
            'SaveConfigValue("FW_started", "False")
            CONFIG.Config.FolderWatchStarted = True
            CONFIG.Save()
            Return True
        Else
            Return False
        End If
    End Function
    'Private 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 oHandleType As String
    '        If e.FullPath.ToLower.EndsWith(".msg") Then
    '            oHandleType = "|FW_OUTLOOK_MESSAGE|"
    '        Else
    '            oHandleType = "|FW_SIMPLEINDEXER|"
    '        End If
    '        'Die Datei übergeben
    '        Logger.Info(">> OnCreated-File:" & e.FullPath)

    '        If ClassIndexFunctions.CheckDuplicateFiles(e.FullPath, "FolderWatch/Scan") Then
    '            FileHandler.Decide_FileHandle(e.FullPath, oHandleType)
    '        End If
    '    Catch ex As Exception
    '        MsgBox(ex.Message, MsgBoxStyle.Critical, "Error in folder_watch_Created")
    '    End Try

    'End Sub

End Class
