Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Language
Imports System.IO

Namespace Methods.IDB.GetFileObject
    Public Class GetFileObjectMethod
        Inherits BaseMethod

        Public Sub New(pLogConfig As LogConfig, pDatabaseIDB As MSSQLServer, pDatabaseECM As MSSQLServer, pGlobalState As GlobalState)
            MyBase.New(pLogConfig, pDatabaseIDB, pDatabaseECM, pGlobalState)
        End Sub

        Public Function Run(pData As GetFileObjectRequest) As GetFileObjectResponse
            Try
                If Helpers.TestObjectIdExists(pData.ObjectId) = False Then
                    LogAndThrow("ObjectId does not exist!")
                End If

                Dim oSQL = $"SELECT * FROM VWIDB_FILE_OBJECT WHERE IDB_OBJ_ID = {pData.ObjectId}"
                Dim oTable = DatabaseIDB.GetDatatable(oSQL)

                If oTable Is Nothing OrElse oTable.Rows.Count = 0 Then
                    LogAndThrow("Error while getting FileObject data!")
                End If

                Dim oRow As DataRow = oTable.First()
                Dim oFileHash As String = oRow.ItemEx("FILE_HASH", "")
                Dim oFileSize As Long = oRow.ItemEx(Of Long)("FILE_SIZE", 0)
                Dim oFileExtension As String = oRow.ItemEx(Of String)("EXTENSION")
                Dim oFilePath = oRow.ItemEx("RELPATH", "")
                Dim oFileName = oRow.ItemEx("Filename", "")
                Dim oFullFileName = Path.Combine(oFilePath, oFileName)

                Dim oFileObject As New FileObject With {
                    .ObjectId = pData.ObjectId,
                    .FileHash = oFileHash,
                    .FileSize = oFileSize,
                    .FileExtension = oFileExtension,
                    .FilePath = oFullFileName
                }

                If pData.LoadFileContents = True Then
                    If File.Exists(oFullFileName) = False Then
                        Throw New FileNotFoundException("FileObject not Found!", oFullFileName)
                    End If

                    Dim oContents As Byte() = LoadFileContents(oFullFileName)

                    If oContents Is Nothing Then
                        Throw New FileNotFoundException("FileObject not Found!", oFullFileName)
                    End If

                    oFileObject.FileContents = oContents

                End If

                Return New GetFileObjectResponse(oFileObject)

            Catch ex As FileNotFoundException
                Logger.Error(ex)
                Return New GetFileObjectResponse(ex, ex.FileName)

            Catch ex As Exception
                Logger.Error(ex)
                Return New GetFileObjectResponse(ex)

            End Try
        End Function

        Private Function LoadFileContents(pFilePath As String) As Byte()
            Try
                Using oFileStream As New FileStream(pFilePath, FileMode.Open, FileAccess.Read)
                    Using oMemoryStream As New MemoryStream()
                        oFileStream.CopyTo(oMemoryStream)
                        Dim oContents = oMemoryStream.ToArray()

                        Return oContents
                    End Using
                End Using

            Catch ex As Exception
                Logger.Error(ex)
                Return Nothing

            End Try
        End Function
    End Class

End Namespace