Imports System.ComponentModel
Imports DevExpress.XtraEditors.Controls

Public Class frmIndexFileList

    Private Sub frmIndexFileList_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Me.TBGI_FILES_USERTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBGI_FILES_USERTableAdapter.Fill(Me.MyDataset1.TBGI_FILES_USER, Environment.UserName)

            'If MyDataset1.TBGI_FILES_USER.Rows.Count > 0 Then
            '    CheckedListBoxControl1.DataSource = MyDataset1.TBGI_FILES_USER
            '    CheckedListBoxControl1.DisplayMember = MyDataset1.TBGI_FILES_USER.Columns("FILENAME_ONLY").ColumnName
            '    CheckedListBoxControl1.ValueMember = MyDataset1.TBGI_FILES_USER.Columns(0).ColumnName
            'End If

            CheckedListBoxControl1.DisplayMember = "FileName"
            CheckedListBoxControl1.ValueMember = "FileName"
            CheckedListBoxControl1.Items.Clear()

            For Each oFile As DataRow In MyDataset1.TBGI_FILES_USER.Rows
                Dim oFilename = oFile.Item("FILENAME_ONLY")
                Dim oPath = oFile.Item("FILENAME2WORK")
                Dim oHash = oFile.Item("FILE_HASH")
                Dim oHandleType = oFile.Item("HANDLE_TYPE")

                Dim oImportDate = ClassIndexFunctions.FileExistsinDropTable(oPath, oHandleType)
                Dim oFileExists As Boolean = True

                If oImportDate = DateTime.MinValue Then
                    oFileExists = False
                End If

                CheckedListBoxControl1.Items.Add(New SplitFile With {
                    .FileName = oFilename,
                    .Exists = oFileExists,
                    .[Date] = oImportDate
                })
            Next

            CheckedListBoxControl1.CheckAll()

        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox("Error in Load Form: " & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Public Class SplitFile
        Public FileName As String
        Public Exists As Boolean
        Public [Date] As Date

        Public Overrides Function ToString() As String
            If Exists Then
                Return $"{FileName} (Die Datei wurde bereits am [{[Date]}] verarbeitet)"
            Else
                Return FileName
            End If
        End Function
    End Class

    Private Sub btnstartIndex_Click(sender As Object, e As EventArgs) Handles btnstartIndex.Click
        Try
            LOGGER.Debug("Starting indexing of {0} files", CheckedListBoxControl1.Items.Count - CheckedListBoxControl1.CheckedItems.Count)

            For index = 0 To CheckedListBoxControl1.ItemCount - 1
                Dim oRow = MyDataset1.TBGI_FILES_USER.Item(index)
                Dim oChecked = CheckedListBoxControl1.GetItemChecked(index)

                Dim oFilePath = oRow.Item("FILENAME2WORK")
                Dim oHandletype As String = oRow.Item("HANDLE_TYPE")
                Dim oGuid = oRow.Item("GUID")

                If oChecked = False Then
                    If oHandletype = "|MSGONLY|" Or oHandletype = "|ATTMNTEXTRACTED|" Then
                        Try
                            IO.File.Delete(oFilePath)
                        Catch ex As Exception
                            LOGGER.Error(ex)
                        End Try
                    End If

                    LOGGER.Debug("Removing file from user files: [{0}]", oFilePath)
                    DATABASE_ECM.ExecuteNonQuery("DELETE FROM TBGI_FILES_USER WHERE GUID = " & oGuid)
                End If
            Next

            DialogResult = DialogResult.OK
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox("Unexpected Error in Clear Multiple Documents: " & ex.Message, MsgBoxStyle.Critical)
        End Try

        Me.Close()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        CheckedListBoxControl1.CheckAll()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        CheckedListBoxControl1.UnCheckAll()
    End Sub

    Private Sub frmIndexFileList_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Me.BringToFront()
        CheckedListBoxControl1.Focus()
    End Sub

    Private Sub CheckedListBoxControl1_DrawItem(sender As Object, e As DevExpress.XtraEditors.ListBoxDrawItemEventArgs) Handles CheckedListBoxControl1.DrawItem
        If e.State = (DrawItemState.Selected Or DrawItemState.Checked) Or e.State = DrawItemState.Checked Then
            e.Appearance.BackColor = Color.LightGreen
        Else
            e.Appearance.BackColor = Color.Transparent
        End If
    End Sub

    Private Sub frmIndexFileList_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        If DialogResult = DialogResult.Cancel Then
            Try
                DATABASE_ECM.ExecuteNonQuery("DELETE FROM TBGI_FILES_USER WHERE UPPER(USER@WORK) = UPPER('" & Environment.UserName & "')")
            Catch ex As Exception
                MsgBox("Error while deleting User Files: " & ex.Message, MsgBoxStyle.Critical, Text)
                LOGGER.Error(ex)
            End Try
        End If
    End Sub
End Class