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

Namespace Modules.Globix
    Public Class ImportFile
        Inherits BaseMethod
        Private ReadOnly FileEx As Filesystem.File

        Public Sub New(pLogConfig As LogConfig, pChannel As IEDMIServiceChannel)
            MyBase.New(pLogConfig, pChannel)
            FileEx = New Filesystem.File(pLogConfig)
        End Sub

        Public Async Function RunAsync(pFilePath As String,
                                        pProfileId As Integer,
                                        pAttributeValues As List(Of UserAttributeValue),
                                        pObjectStoreName As String,
                                        pObjectKind As String,
                                        pBusinessEntity As String,
                                        pImportOptions As Options.ImportFileOptions) As Task(Of ImportFileResponse)
            Try
                ' Set default options
                If pImportOptions Is Nothing Then
                    pImportOptions = New Options.ImportFileOptions()
                End If

                ' Check if file exists
                If File.Exists(pFilePath) = False Then
                    Throw New FileNotFoundException("Path does not exist")
                End If

                Dim oFileInfo As New FileInfo(pFilePath)
                Dim oExtension As String = oFileInfo.Extension

                Dim oFileName As String = oFileInfo.Name
                Dim oFileCreatedAt As Date = oFileInfo?.CreationTime
                Dim oFileModifiedAt As Date = oFileInfo?.LastWriteTime
                Dim oFileHash As String = FileEx.GetChecksum(oFileInfo.FullName)

                ' Importing the file now
                Using oFileStream As New FileStream(pFilePath, FileMode.Open, FileAccess.Read)
                    Using oMemoryStream As New MemoryStream()
                        oFileStream.CopyTo(oMemoryStream)
                        Dim oContents = oMemoryStream.ToArray()

                        Dim oFileImportResponse = Await Channel.ImportFileAsync(New ImportFileRequest With {
                            .BusinessEntity = pBusinessEntity,
                            .File = New FileProperties With {
                                .FileName = oFileInfo.Name,
                                .FileCreatedAt = oFileCreatedAt,
                                .FileChangedAt = oFileModifiedAt,
                                .FileContents = oContents,
                                .FileImportedAt = pImportOptions.DateImported,
                                .FileChecksum = oFileHash,
                                .FileInfoRaw = oFileInfo
                            },
                            .KindType = pObjectKind,
                            .StoreName = pObjectStoreName,
                            .User = New UserState() With {
                                .UserName = pImportOptions.Username,
                                .Language = pImportOptions.Language
                            },
                            .ProfileId = pProfileId,
                            .AttributeValues = pAttributeValues.ToArray
                        })

                        Return oFileImportResponse
                    End Using
                End Using
            Catch ex As Exception
                Logger.Error(ex)
                Return Nothing
            End Try
        End Function
    End Class
End Namespace

