package example;

import java.io.IOException;
import java.io.InputStream;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.List;

import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.freedomoss.crowdcontrol.webharvest.plugin.s3.S3ResultItem;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;

import com.workfusion.odf.core.Intake;
import com.workfusion.rpa.core.plugin.PluginAdapterFactory;
import com.workfusion.rpa.core.plugin.s3.S3PluginAdapter;
import com.workfusion.rpa.core.plugin.s3.S3PluginWebharvestAdapter;
import com.workfusion.rpa.core.storage.S3Manager;

public class S3pluginsExample {
    private final Intake intake;
    private final Logger log;

    public S3pluginsExample(Intake intake ) {
        this.intake = intake;
        this.log = intake.getInstance(Logger.class);
    }

    public   void s3Example() throws IOException {
        final S3Manager s3Manager = intake.getInstance(S3Manager.class);

        final S3PluginAdapter s3PluginAdapter = PluginAdapterFactory.getInstance()
                .getPluginAdapter(PluginAdapterFactory.PluginsEnum.S3);

        S3ResultItem s3ResultItem = null;
        String path = null;
        try {


            s3PluginAdapter.getCommonS3Attributes().put(S3PluginWebharvestAdapter.ATTRIBUTE_BUCKET, "doc-upload");
            final String part1 = "part 1";
            final List<String> strings1 = s3PluginAdapter.listEntries(part1);

            if (!strings1.isEmpty()) {
                throw new IllegalStateException("There unwanted files");
            }

            final String testingKeyOne = part1 + "/part 2/Delete Transaction.xml";
            final InputStream resourceAsStream = this.getClass().getResourceAsStream("/configs/main/Delete Transaction.xml");
            final byte[] bytesOne = IOUtils.toByteArray(resourceAsStream);
            s3ResultItem = s3PluginAdapter.put(bytesOne,
                    testingKeyOne,
                    CannedAccessControlList.Private,
                    null,
                    null,
                    Long.valueOf(99999));

            final String testingKeyTwo = part1 + "/part 2/Mail Connector.xml";

            final String mailConnectorResource = "/configs/main/Mail Connector.xml";
            final InputStream resourceAsStream1 = this.getClass().getResourceAsStream(mailConnectorResource);
            final byte[] bytesTwo = IOUtils.toByteArray(resourceAsStream1);

            path = s3Manager.uploadFileToS3(bytesTwo, testingKeyTwo, S3Manager.AUTO_DETECT_CONTENT_TYPE);

            final ZonedDateTime from = ZonedDateTime.now().minusYears(1000);
            final List<String> strings = s3PluginAdapter.listEntries(".*", from, ZonedDateTime.now().plusYears(1000), part1);
            strings.forEach(log::info);

            if (strings.size() != 2) {
                throw new IllegalStateException();
            }

            final byte[] content = s3Manager.getContent(s3ResultItem.getFilename());

            if (!Arrays.equals(content, bytesOne)) {
                throw new IllegalStateException();
            }

            if (!Arrays.equals(s3PluginAdapter.get(path), bytesTwo)) {
                throw new IllegalStateException();
            }

            final String s3FileURLFromKey = s3Manager.getS3FileURL(s3ResultItem.getFilename());

            if (!s3FileURLFromKey.equals(s3ResultItem.getDirectUrl())) {
                throw new IllegalStateException();
            }

            final byte[] contentFromKeyUrl = s3Manager.getContent(s3FileURLFromKey);

            if (!Arrays.equals(contentFromKeyUrl, bytesOne)) {
                throw new IllegalStateException();
            }

        }
        finally {

            if(s3ResultItem!= null){
                s3Manager.deleteFile(s3ResultItem.getSignedUrl());
            }

            if(path!=null){
                s3PluginAdapter.delete(path);
            }
        }

    }
}
