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

import Amplify
import Foundation

extension AWSS3StorageService {

    func multiPartUpload(
        serviceKey: String,
        uploadSource: UploadSource,
        contentType: String?,
        metadata: [String: String]?,
        accelerate: Bool?,
        onEvent: @escaping StorageServiceMultiPartUploadEventHandler
    ) {
        let fail: (Error) -> Void = { error in
            let storageError = StorageError(error: error)
            onEvent(.failed(storageError))
        }

        // Validate parameters
        guard try attempt(validateParameters(bucket: bucket, key: serviceKey, accelerationModeEnabled: false), fail: fail) else { return }

        let requestHeaders: [String: String] = [:]

        // Get file using upload source
        guard let uploadFile = try attempt(uploadSource.getFile(), fail: fail) else { return }

        let client = DefaultStorageMultipartUploadClient(
            serviceProxy: self,
            bucket: bucket,
            key: serviceKey,
            uploadFile: uploadFile,
            metadata: metadata
        )
        let multipartUploadSession = StorageMultipartUploadSession(
            client: client,
            bucket: bucket,
            key: serviceKey,
            contentType: contentType,
            requestHeaders: requestHeaders,
            onEvent: onEvent
        )

        register(multipartUploadSession: multipartUploadSession)

        // https://docs.amplify.aws/sdk/storage/transfer-utility/q/platform/ios
        multipartUploadSession.startUpload()
    }

}
