Imports System.IO
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.EDMI.API

Public Class frmFileTest
    Private _fileOp As Document

    Private Sub frmFileTest_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            _fileOp = New Document(My.LogConfig, My.Settings.IDBNetworkServiceAddress)
            _fileOp.Connect()
        Catch ex As Exception
            Logger.Warn($"Unexpected error in frmFileTest_Load: {ex.Message}")
        End Try
    End Sub

    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim oDialog = New OpenFileDialog() With {
            .Multiselect = True
        }

        If oDialog.ShowDialog() <> DialogResult.OK Then
            Exit Sub
        End If

        For Each oFileName In oDialog.FileNames
            Try
                Dim oSW As New Stopwatch()
                oSW.Start()

                Dim oResult = Await _fileOp.ImportFile2(oFileName)

                oSW.Stop()

                If oResult.OK = False Then
                    listboxLog.Items.Add($"Document upload failed! Reason: '{oResult.ErrorMessage}'")
                    Exit Sub
                End If

                listboxLog.Items.Add($"Document uploaded!")
                listboxLog.Items.Add($"Filename: {oResult.Document.FileName}")
                listboxLog.Items.Add($"Time in milliseconds: {oSW.ElapsedMilliseconds}")
                listboxLog.Items.Add($"----------------------------------------------------------")
            Catch ex As Exception
                listboxLog.Items.Add($"Document upload failed! Reason: '{ex.Message}'")
                Logger.Error(ex)
            End Try
        Next

    End Sub

    'Private Sub btnDocByDocId_Click(sender As Object, e As EventArgs)
    '    Try
    '        Dim oDocId As Int64 = Int64.Parse(TextBox1.Text)
    '        Dim oResult = _fileOp.GetDocumentByDocumentId(oDocId)

    '        If Not oResult.OK Then
    '            MsgBox(oResult.ErrorMessage)
    '            Exit Sub
    '        End If

    '        Dim oDocObject = oResult.Document

    '        listboxLog.Items.Add($"Document fetched!")
    '        listboxLog.Items.Add($"DocId: {oDocObject.DocumentId}")
    '        listboxLog.Items.Add($"ContainerId: {oDocObject.ContainerId}")
    '        listboxLog.Items.Add($"Filename: {oDocObject.FileName}")
    '        listboxLog.Items.Add($"----------------------------------------------------------")
    '    Catch ex As Exception
    '        ShowErrorMessage(ex)
    '    End Try
    'End Sub

    'Private Sub btnDocByContainerId_Click(sender As Object, e As EventArgs)
    '    Try
    '        Dim oContainerId As Int64 = Int64.Parse(TextBox2.Text)
    '        Dim oResult = _fileOp.GetDocumentByContainerId(oContainerId)

    '        If Not oResult.OK Then
    '            MsgBox(oResult.ErrorMessage)
    '            Exit Sub
    '        End If

    '        Dim oDocObject = oResult.Document

    '        listboxLog.Items.Add($"Document fetched!")
    '        listboxLog.Items.Add($"DocId: {oDocObject.DocumentId}")
    '        listboxLog.Items.Add($"ContainerId: {oDocObject.ContainerId}")
    '        listboxLog.Items.Add($"Filename: {oDocObject.FileName}")
    '        listboxLog.Items.Add($"----------------------------------------------------------")
    '    Catch ex As Exception
    '        ShowErrorMessage(ex)
    '    End Try
    'End Sub
End Class