package com.workfusion.odf.impl.s3;

import java.io.IOException;

import jcifs.smb.SmbFile;
import org.slf4j.Logger;

import com.workfusion.odf.impl.converter.extractor.DataWrapper;
import com.workfusion.rpa.core.storage.S3KeyBuilder;
import com.workfusion.rpa.core.storage.S3Manager;

import static java.util.Objects.requireNonNull;

public final class S3Uploader {

    private final S3Manager s3Manager;
    private final Logger logger;
    private final String s3FolderName;

    public S3Uploader(S3Manager s3Manager, Logger logger, String s3FolderName) {
        this.s3Manager = requireNonNull(s3Manager);
        this.logger = requireNonNull(logger);
        this.s3FolderName = s3FolderName;
    }

    public String upload(DataWrapper wrapper) {
        String originalName = wrapper.getName();
        String fileKey = new S3KeyBuilder()
                .folder(s3FolderName)
                .originalFileName(originalName)
                .generate();

        logger.info("Uploading '{}' file to S3. Original file name is '{}'", fileKey, originalName);
        String fileLink = s3Manager.uploadFileToS3(wrapper.getData(), fileKey, S3Manager.AUTO_DETECT_CONTENT_TYPE);
        logger.info("File '{}' has successfully uploaded to '{}'", fileKey, fileLink);

        return fileLink;
    }

    public String upload(SmbFile file) throws IOException {
        String originalName = file.getName();
        String fileKey = new S3KeyBuilder()
                .folder(s3FolderName)
                .originalFileName(originalName)
                .generate();

        logger.info("Uploading '{}' file to S3. Original file name is '{}'", fileKey, originalName);
        String fileLink = s3Manager.putFile(fileKey, file.getInputStream(), S3Manager.AUTO_DETECT_CONTENT_TYPE);
        logger.info("File '{}' has successfully uploaded to '{}'", fileKey, fileLink);

        return fileLink;
    }

}
