//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Amplify
import AWSPluginsCore
import Foundation

// The recovery process must load active tasks with URLSession and match them using taskIdentifier to a persisted task.
// Once the task can be "hydrated" the Event Handler can handle events from URLSession delegate methods. Tasks which
// are not matched can be deleted.

// swiftlint:disable line_length

/// Database protocol which supports the recovery process.
protocol StorageTransferDatabase {
    func insertTransferRequest(task: StorageTransferTask)

    func updateTransferRequest(task: StorageTransferTask)

    func removeTransferRequest(task: StorageTransferTask)

    func prepareForBackground(completion: (() -> Void)?)

    func defaultTransferType(persistableTransferTask: StoragePersistableTransferTask) -> StorageTransferType?

    func recover(urlSession: StorageURLSession, completionHandler: @escaping (Result<StorageTransferTaskPairs, Error>) -> Void)

    func attachEventHandlers(
        onUpload: AWSS3StorageServiceBehavior.StorageServiceUploadEventHandler?,
        onDownload: AWSS3StorageServiceBehavior.StorageServiceDownloadEventHandler?,
        onMultipartUpload: AWSS3StorageServiceBehavior.StorageServiceMultiPartUploadEventHandler?
    )
}

extension StorageTransferDatabase {
    func prepareForBackground() {
        prepareForBackground(completion: nil)
    }

    func attachEventHandlers(
        onUpload: AWSS3StorageServiceBehavior.StorageServiceUploadEventHandler? = nil,
        onDownload: AWSS3StorageServiceBehavior.StorageServiceDownloadEventHandler? = nil,
        onMultipartUpload: AWSS3StorageServiceBehavior.StorageServiceMultiPartUploadEventHandler? = nil
    ) {
        attachEventHandlers(onUpload: onUpload, onDownload: onDownload, onMultipartUpload: onMultipartUpload)
    }

}
