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 Load_Files()
        Try
            Dim sql1 As String = String.Format("select * from TBPMO_FILES_USER where HANDLE_TYPE = 'SCAN' and WORKED = 0 AND UPPER(USER_WORK) = '{0}'", Environment.UserName)
            Dim DT As DataTable = ClassDatabase.Return_Datatable(sql1, "Load ScanFiles")
            If Not IsNothing(DT) Then
                CURRENT_SCAN_TABLE = DT
                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
            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
        Load_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
                    Panel1.Visible = True
                    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
    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. 
            ListView1.DoDragDrop(DragID, DragDropEffects.Copy)
            Timer1.Start()
            'Console.WriteLine("Dragging")
        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")
                ListView1.DoDragDrop("SCAN;" & DragID.ToString, DragDropEffects.Copy)
            End If
        End If

    End Sub

    Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged

    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
                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 ClassDatabase.Execute_non_Query(del, True) Then
                        Load_Files()
                    End If
                End If
            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()
    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 Timer1.Tick
        Load_Files()
    End Sub
End Class