package org.cru.godtools.s3;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3Object;
import org.jboss.logging.Logger;

import javax.inject.Inject;
import java.io.InputStream;

/**
 * Created by ryancarlson on 7/11/15.
 */
public class GodToolsS3Client
{
	@Inject
	AmazonS3Client s3Client;

	private Logger log = Logger.getLogger(this.getClass());

	public S3Object getMetaFile()
	{
		return getMetaFile(null, null);
	}

	public S3Object getMetaFile(String languageCode)
	{
		return getMetaFile(languageCode, null);
	}

	public S3Object getMetaFile(String languageCode, String packageCode)
	{
		String metaKey = AmazonS3GodToolsConfig.getMetaKey(languageCode, packageCode);

		log.info(String.format("Getting meta info file w/ key %s", metaKey));

		return s3Client.getObject(new GetObjectRequest(AmazonS3GodToolsConfig.BUCKET_NAME,
				metaKey));
	}

	public S3Object getLanguagesZippedFolder(String languageCode)
	{
		return getLanguagesZippedFolder(languageCode, null);
	}

	public S3Object getLanguagesZippedFolder(String languageCode, String packageCode)
	{
		String languagesKey = AmazonS3GodToolsConfig.getLanguagesKey(languageCode, packageCode);

		log.info(String.format("Getting languages file w/ key %s", languagesKey));

		return s3Client.getObject(new GetObjectRequest(AmazonS3GodToolsConfig.BUCKET_NAME,
				languagesKey));
	}

	public S3Object getPackagesZippedFolder(String languageCode)
	{
		return getPackagesZippedFolder(languageCode, null);
	}

	public S3Object getPackagesZippedFolder(String languageCode, String packageCode)
	{
		String packagesKey = AmazonS3GodToolsConfig.getPackagesKey(languageCode, packageCode);

		log.info(String.format("Getting packages file w/ key %s", packagesKey));

		return s3Client.getObject(new GetObjectRequest(AmazonS3GodToolsConfig.BUCKET_NAME,
				packagesKey));
	}

	public void pushPackagesZippedFolder(String languageCode, InputStream compressedTranslation)
	{
		String packagesKey = AmazonS3GodToolsConfig.getPackagesKeyV2(languageCode);

		log.info(String.format("Pushing packages file w/ key %s", packagesKey));

		ObjectMetadata metadata = new ObjectMetadata();

		s3Client.putObject("cru-godtools",packagesKey,compressedTranslation, metadata);
	}
}

@@ -72,16 +72,80 @@
 Client;%0A
+import com.amazonaws.services.s3.model.CannedAccessControlList;%0A
 import c
@@ -248,16 +248,73 @@
 tadata;%0A
+import com.amazonaws.services.s3.model.PutObjectRequest;%0A
 import c
@@ -2401,26 +2401,57 @@
 %0A%0A%09%09
-s3Client.p
+PutObjectRequest putObjectRequest = new P
 utObject
 (%22cr
@@ -2446,16 +2446,23 @@
 utObject
+Request
 (%22cru-go
@@ -2469,16 +2469,17 @@
 dtools%22,
+ 
 packages
@@ -2482,16 +2482,17 @@
 agesKey,
+ 
 compress
@@ -2515,15 +2515,170 @@
 etadata)
+%0A%09%09%09%09.withCannedAcl(CannedAccessControlList.PublicRead); // God Tools packages are meant to be downloaded w/o authz%0A%0A%09%09s3Client.putObject(putObjectRequest)
 ;%0A%09%7D%0A%7D%0A

Make updated zip folders pushed to s3 be publicly accessible so the content can be downloaded by iOS/Android