Imports System.IO
Imports DigitalData.Modules.EDMI.API
Imports DigitalData.Modules.EDMI.API.Client
Imports DigitalData.Modules.EDMI.API.EDMIServiceReference
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.ZooFlow.Constants
Imports DigitalData.Modules.ZooFlow.State

Namespace DocumentResultList

    Public Class Loader
        Inherits Modules.ZooFlow.Base.BaseClass

        Private ReadOnly Client As Client
        Private ReadOnly Mode As OperationMode
        Private ReadOnly User As DigitalData.Modules.ZooFlow.State.UserState

        Public Sub New(pLogConfig As LogConfig, pMode As OperationMode, pClient As Client, pUser As DigitalData.Modules.ZooFlow.State.UserState)
            MyBase.New(pLogConfig)
            Client = pClient
            Mode = pMode
            User = pUser
        End Sub

        Public Function Load(pObjectId As Long, pFullPath As String) As Document
            Select Case Mode
                Case OperationMode.NoAppServer
                    Return Load_FromWindream(pObjectId, pFullPath)

                Case OperationMode.WithAppServer
                    Return Load_FromIDB(pObjectId)

                Case OperationMode.ZooFlow
                    Return Load_FromZooflow(pObjectId)

                Case Else
                    Return Nothing
            End Select
        End Function

        Private Function Load_FromWindream(pObjectId As Long, pFullPath As String) As Document
            Dim oFileInfo As New FileInfo(pFullPath)
            Dim oResultDocumentInfo = New Document(pObjectId) With {
        .Contents = Load_FromDisk(pFullPath),
        .AccessRight = Rights.AccessRight.FULL,
        .FullPath = pFullPath,
        .Extension = oFileInfo.Extension.Substring(1)
    }

            Return oResultDocumentInfo
        End Function

        Private Function Load_FromIDB(pObjectId As Long) As Document
            Try
                Dim oDocumentInfo As Client.DocumentInfo = Client.GetDocumentInfo(User.UserId, pObjectId)
                Dim oFileInfo As New FileInfo(oDocumentInfo.FullPath)
                Dim oResultDocumentInfo As New Document(pObjectId) With {
                    .Contents = Load_FromDisk(oDocumentInfo.FullPath),
                    .AccessRight = oDocumentInfo.AccessRight,
                    .FullPath = oDocumentInfo.FullPath,
                    .Extension = oFileInfo.Extension.Substring(1)
                }

                Return oResultDocumentInfo
            Catch ex As Exception
                Logger.Error(ex)
                Return Nothing
            End Try
        End Function

        Private Function Load_FromZooflow(pObjectId As Long) As Document
            Dim oFileObject As FileObject = Client.Zooflow_GetFileObject(pObjectId, pLoadFileContents:=True)

            If oFileObject Is Nothing Then
                Return Nothing
            End If

            Dim oResultDocumentInfo As New Document(pObjectId) With {
                .Contents = oFileObject._FileContents,
                .Extension = oFileObject._FileExtension,
                .AccessRight = Rights.AccessRight.FULL,
                .FileHash = oFileObject._FileHash,
                .FullPath = Nothing
            }

            Return oResultDocumentInfo
        End Function

        Private Function Load_FromDisk(pFullPath) As Byte()
            Try
                Logger.Debug("Loading file [{0}]", pFullPath)
                Using oStream = File.OpenRead(pFullPath)
                    Using oMemoryStream = New MemoryStream()
                        oStream.CopyTo(oMemoryStream)
                        Logger.Debug("Loaded file [{0}] successfully.", pFullPath)
                        Return oMemoryStream.ToArray()
                    End Using
                End Using
            Catch ex As Exception
                Logger.Warn("Loading file [{0}] failed.", pFullPath)
                Logger.Error(ex)
                Return Nothing
            End Try
        End Function
    End Class
End Namespace
