Imports System.IO

Public Class frmScanFiles

    Dim DragID
    Dim MouseIsDown As Boolean = False
    Private Shared _Instance As frmScanFiles = Nothing

    Public Shared Function Instance() As frmScanFiles
        If _Instance Is Nothing OrElse _Instance.IsDisposed = True Then
            _Instance = New frmScanFiles
        End If
        _Instance.BringToFront()
        Return _Instance
    End Function
    Private Sub frmScanFiles_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        ClassWindowLocation.SaveFormLocationSize(Me, 9999, CURRENT_SCREEN_ID, Me.Name)
    End Sub
    Private Sub frmScanFiles_Load(sender As Object, e As EventArgs) Handles Me.Load
        Load_Files()
        ClassWindowLocation.LoadFormLocationSize(Me, 9999, CURRENT_SCREEN_ID, Me.Name)
    End Sub
    Sub Clear_and_Reload_Files()
        Try
            PdfViewer1.DocumentFilePath = ""
            Dim del = "DELETE FROM TBPMO_FILES_USER where HANDLE_TYPE = 'SCAN' AND UPPER(USER_WORK) = UPPER('" & USER_USERNAME & "')"
            If MYDB_ECM.ExecuteNonQuery(del) Then
                ' Make a reference to a directory.
                Dim di As New DirectoryInfo(CURRENT_SCAN_FOLDERWATCH)
                ' Get a reference to each file in that directory.
                Dim fiArr As FileInfo() = di.GetFiles()
                ' Display the names of the files.

                Dim fri As FileInfo
                For Each fri In fiArr
                    Dim irregular As Boolean = False
                    For Each row As DataRow In DTEXCLUDE_FILES.Rows
                        Dim content As String = row.Item(0).ToString.ToLower
                        If fri.Name.ToLower.Contains(content) Then
                            irregular = True
                            Exit For
                        End If
                    Next
                    If irregular = False Then
                        Dim handletype As String
                        If fri.FullName.EndsWith(".msg") Then
                            handletype = "SCAN_OUTLOOK_MESSAGE"
                        Else
                            handletype = "SCAN"
                        End If
                        ClassFolderWatcher.Insert_USER_File(fri.FullName, handletype)
                    End If
                Next fri
                ClassFolderWatcher.Check_Scan_Files()
                Load_Files()
            End If
        Catch ex As Exception

        End Try
    End Sub
    Sub Load_Files()
        Try
            ListView1.Items.Clear()
            Dim i = 0
            For Each row As DataRow In CURRENT_SCAN_TABLE.Rows
                Dim extension = Path.GetExtension(row.Item("FILENAME2WORK").ToString)
                Dim filestring = row.Item("FILENAME2WORK")
                If ClassHelper.CheckFileIsInUse(filestring) = False Then
                    Dim filename = row.Item("FILENAME_ONLY").ToString
                    Dim ID = row.Item("GUID").ToString
                    'We can only find associated exes by extension, so don't show any files that have no extension
                    If IsNothing(extension) Then
                    Else
                        If extension.Contains("pdf") Then
                            'Add the file to the ListView, with the executable path as the key to the ImageList's image
                            ListView1.Items.Add(filename, 0)
                        Else
                            ListView1.Items.Add(filename)
                        End If
                        ListView1.Items(i).Tag = ID
                        i += 1
                    End If
                End If
            Next
            If CURRENT_SCAN_TABLE.Rows.Count = 0 Then
                Me.Close()
            End If
        Catch ex As Exception
            MsgBox("unexpected Error in Load Scanfiles:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub btnrefresh_Click(sender As Object, e As EventArgs) Handles btnrefresh.Click
        Clear_and_Reload_Files()
    End Sub

    Private Sub PdfViewer1_CurrentPageChanged(sender As Object, e As DevExpress.XtraPdfViewer.PdfCurrentPageChangedEventArgs) Handles PdfViewer1.CurrentPageChanged
        PDF_Pagenumber()
    End Sub
    Sub PDF_Pagenumber()
        Try
            pdfstatuslblPageNumber.Text = "Page " & PdfViewer1.CurrentPageNumber & "/" & PdfViewer1.PageCount
        Catch ex As Exception

        End Try
    End Sub

    Private Sub ListView1_ItemSelectionChanged(sender As Object, e As ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged
        If ListView1.SelectedItems.Count = 1 Then
            Dim i = ListView1.FocusedItem
            Dim i1 = i.Text
            If Not IsNothing(i) Then
                If i.Text.ToLower.EndsWith("pdf") Then
                    Me.Cursor = Cursors.WaitCursor
                    Panel1.Visible = True
                    PdfViewer1.DocumentFilePath = ""
                    Dim docpath = CURRENT_SCAN_FOLDERWATCH & "\" & i.Text
                    PdfViewer1.LoadDocument(docpath)
                    'PdfViewer1.HorizontalScroll.Visible = True
                Else
                    Panel1.Visible = False
                    PdfViewer1.DocumentFilePath = ""
                End If
            End If
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub ToolStripDropDownButton1_Click(sender As Object, e As EventArgs) Handles ToolStripDropDownButton1.Click
        PdfViewer1.ZoomFactor = 30
    End Sub
    'Private Sub ListView1_MouseDown(sender As Object, e As MouseEventArgs) Handles ListView1.MouseDown
    '    If ListView1.SelectedItems.Count = 1 Then
    '        Dim i = ListView1.FocusedItem
    '        Dim i1 = i.Text
    '        If Not IsNothing(i) Then
    '            MouseIsDown = True
    '            DragID = i.Tag
    '            Console.WriteLine("Mouse Down")
    '        End If
    '    End If
    'End Sub

    Private Sub ListView1_MouseMove(sender As Object, e As MouseEventArgs) Handles ListView1.MouseMove
        If MouseIsDown Then
            'Initiate dragging. 
            PdfViewer1.DocumentFilePath = ""
            ListView1.DoDragDrop(DragID, DragDropEffects.Copy)

        End If
        MouseIsDown = False
    End Sub

    Private Sub ListView1_ItemDrag(sender As Object, e As ItemDragEventArgs) Handles ListView1.ItemDrag
        If ListView1.SelectedItems.Count = 1 Then
            Dim i = ListView1.FocusedItem
            Dim i1 = i.Text
            If Not IsNothing(i) Then
                MouseIsDown = True
                DragID = i.Tag
                'Console.WriteLine("Mouse Down")
                PdfViewer1.DocumentFilePath = ""
                ListView1.DoDragDrop("SCAN;" & DragID.ToString, DragDropEffects.Copy)
            End If
        End If

    End Sub

    Private Sub PdfViewer1_DocumentChanged(sender As Object, e As DevExpress.XtraPdfViewer.PdfDocumentChangedEventArgs) Handles PdfViewer1.DocumentChanged
        PDF_Pagenumber()
    End Sub

    Private Sub btndelete_Click(sender As Object, e As EventArgs) Handles btndelete.Click
        Try
            If ListView1.SelectedItems.Count = 1 Then
                PdfViewer1.DocumentFilePath = ""
                Dim i = ListView1.FocusedItem
                Dim i1 = i.Text
                If Not IsNothing(i) Then
                    Dim del = "DELETE FROM TBPMO_FILES_USER where GUID = " & i.Tag
                    If MYDB_ECM.ExecuteNonQuery(del) Then
                        ClassFolderWatcher.Check_Scan_Files()
                        Load_Files()
                    Else
                        MsgBox("Could not delete the entry. Check the logfile!", MsgBoxStyle.Information)
                    End If
                End If
            Else
                MsgBox("Please choose a file!", MsgBoxStyle.Information)
            End If
        Catch ex As Exception
            MsgBox("Unexpected Error in Delete Scanfile:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try


    End Sub

    Private Sub frmScanFiles_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Me.BringToFront()
        TimerCheckScanFiles.Start()
    End Sub

    Private Sub chkInFront_CheckedChanged(sender As Object, e As EventArgs) Handles chkInFront.CheckedChanged
        My.Settings.Save()
    End Sub
    Private Sub Timer1_Tick_1(sender As Object, e As EventArgs) Handles TimerCheckScanFiles.Tick
        If CURRENT_SCAN_TABLE.Rows.Count <> ListView1.Items.Count Then
            'Dim result As MsgBoxResult
            'Dim msg = "Neue Dateien wurden gescann"
            'result = MessageBox.Show(msg, "Confirmation needed:", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            'If result = MsgBoxResult.No Then

            'End If
            Load_Files()
        End If

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PdfViewer1.DocumentFilePath = ""
        Dim del = "DELETE FROM TBPMO_FILES_USER where HANDLE_TYPE = 'SCAN' AND UPPER(USER_WORK) = UPPER('" & USER_USERNAME & "')"
        If MYDB_ECM.ExecuteNonQuery(del) Then
            ClassFolderWatcher.Check_Scan_Files()
            Load_Files()
        End If
    End Sub
End Class