public/LogFiles/Import-AuditLogs.ps1

using namespace System.Management.Automation
using module '..\..\modules\Enums.psm1'
using module '..\..\modules\Helper\DateTimeHelper.psm1'
using module '..\..\modules\Session.psd1'
using module '..\..\modules\Downloader\DownloaderFactory.psm1'

function Import-AuditLogs {
    [Alias('impal')]
    param(
        [Parameter(Mandatory=$true)]
        # Specifies the operator from where this match should be downloaded.
        [OPERATOR] $operator,
        [Parameter(Mandatory=$true)]
        # Specifies the environment from where the match logs will be downloaded.
        [ENVIRONMENT] $environment,
        # Specifies the matchId from the Control Center
        [string] $matchId,
        # Defines the source log files to be downloaded. By default it will downlada
        # all configured logs for this Operator and Environment.
        [SOURCE[]] $source,
        # Defines the source from where the matchID should be located.
        # Typically used when there is external (provider) ID of the match.
        [ValidateSet([ValidExternalKeys])]
        [string] $externalKey,
        # Referent message time taken as baseline from where the messages will be start analyzing.
        [string] $referentTime,
        # Timeframe in which the messages will be analyzed after the referent time.
        [string] $timeSpan,
        # run this cmdlet as background job
        [switch] $AsJob,
        # created new session and preserve previous
        [switch] $newSession
    )

    #region Init

    $convert = $false
    $saveConverted = $false

    if ($newSession -or -not [Session]::getCurrent()){
        $currentSession = [Session]::NewSession($operator, $environment, $convert, $saveConverted)
        Write-Host ($LocalizedData.NewSessionCreatedMessage -f $currentSession.sessionId)
    }
    else {
        if (-not [Session]::getCurrent().canImport()) { Write-Error $LocalizedData.BackgroundJobsStillActiveMessage; return }

        [Session]::getCurrent().Reset($operator, $environment, $convert, $saveConverted)
        $currentSession = [Session]::getCurrent()
    }

    if ($null -eq $currentSession.config) {
        Write-Error ('There is no configured endpoints for operator {0}, environment {1}' -f @($operator, $environment))
        return
    }

    if ($externalKey){
        Write-Host $LocalizedData.ExternalMatchIdentificationMessage -NoNewLine
        $matchId = [Session]::getCurrent().getMatchIdByExternal($externalKey, $matchId)

        if ($matchId){
            Write-Host $LocalizedData.ExternalMatchFoundMessage -f $_defMatchId
        }
        else{
            Write-Host ''
            Write-Error $LocalizedData.ExernalMatchIdentificationError
            return
        }
    }

    $currentSession.setAnalyzedScope($source, $matchId, $referentTime, $timeSpan)

    if ($currentSession.config.queryType -eq [QUERY_TYPE]::datetimeInterval){
        Write-Error $LocalizedData.UnsupportedDownloaderQueryMessage
        return
    }

    #endregion

    if ($currentSession.config.ApproveDownload()) {
        $currentSession.RegisterDownloader($AsJob)
        if (-not $AsJob) { $currentSession.RegisterLogFiles($AsJob, $true) }
    }
    else {
        $currentSession.RegisterLogFiles($AsJob, $false)
        Write-Host ''
        Write-Host ($LocalizedData.TotalProcessingTimeMessage -f [DateTimeHelper]::FormatMilliseconds($currentSession.auditLogFile.getProcessingTime((Get-Date)).TotalMilliseconds))
    }

    if (-not $AsJob -or -not [Session]::getCurrent()) {
        [Session]::currentSessionId = $currentSession.sessionId
    }
}