package org.cru.godtools.s3;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import org.cru.godtools.api.meta.MetaResults;
import org.jboss.logging.Logger;

import javax.inject.Inject;
import javax.ws.rs.core.MediaType;
import java.io.InputStream;

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

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

	private static final String GODTOOLS_BUCKET = "cru-godtools";

	public S3Object getMetaFile(MediaType mediaType)
	{
		String metaKey = AmazonS3GodToolsConfig.getMetaKeyV2(mediaType);

		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)
	{
		String languagesKey = AmazonS3GodToolsConfig.getTranslationsKeyV2(languageCode);

		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)
	{
		String packagesKey = AmazonS3GodToolsConfig.getPackagesKeyV2(languageCode);

		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();

		PutObjectRequest putObjectRequest = new PutObjectRequest(GODTOOLS_BUCKET, packagesKey, compressedTranslation, metadata)
				.withCannedAcl(CannedAccessControlList.PublicRead); // God Tools packages are meant to be downloaded w/o authz

		s3Client.putObject(putObjectRequest);
	}

	/**
	 * Pushes an XML and a JSON serialized version of the meta file
	 */
	public void pushMetaFile(MetaResults metaResults)
	{
		log.info("Pushing meta files");

		s3Client.putObject(buildMetaPutRequest(MediaType.APPLICATION_XML_TYPE, metaResults.asXmlStream()));

		s3Client.putObject(buildMetaPutRequest(MediaType.APPLICATION_JSON_TYPE, metaResults.asJsonStream()));
	}

	private PutObjectRequest buildMetaPutRequest(MediaType mediaType, InputStream metaResultsStream)
	{
		return new PutObjectRequest(GODTOOLS_BUCKET,
				AmazonS3GodToolsConfig.getMetaKeyV2(mediaType),
				metaResultsStream,
				new ObjectMetadata())
				.withCannedAcl(CannedAccessControlList.PublicRead);
	}

	public void pushTranslationsZippedFile(String languageCode, String packageCode, InputStream languageFile)
	{
		String translationKey = AmazonS3GodToolsConfig.getTranslationsAndPackageKeyV2(languageCode, packageCode);

		log.info(String.format("pushing %s (text only) file for language %s", packageCode, languageCode));

		ObjectMetadata metadata = new ObjectMetadata();

		PutObjectRequest putObjectRequest = new PutObjectRequest(GODTOOLS_BUCKET, translationKey, languageFile, metadata)
				.withCannedAcl(CannedAccessControlList.PublicRead);

		s3Client.putObject(putObjectRequest);
	}
}

@@ -115,249 +115,9 @@
 del.
-CannedAccessControlList;%0Aimport com.amazonaws.services.s3.model.GetObjectRequest;%0Aimport com.amazonaws.services.s3.model.ObjectMetadata;%0Aimport com.amazonaws.services.s3.model.PutObjectRequest;%0Aimport com.amazonaws.services.s3.model.S3Object
+*
 ;%0Aim
@@ -222,16 +222,54 @@
 Inject;%0A
+import javax.ws.rs.NotFoundException;%0A
 import j
@@ -746,32 +746,43 @@
 s%22, metaKey));%0A%0A
+%09%09try%0A%09%09%7B%0A%09
 %09%09return s3Clien
@@ -853,23 +853,197 @@
 AME,
-%0A%09%09%09%09metaKey));
+ metaKey));%0A%09%09%7D%0A%09%09catch(AmazonS3Exception amazonException)%0A%09%09%7B%0A%09%09%09if(amazonException.getStatusCode() == 404)%0A%09%09%09%7B%0A%09%09%09%09throw new NotFoundException();%0A%09%09%09%7D%0A%0A%09%09%09else throw amazonException;%0A%09%09%7D
 %0A%09%7D%0A
@@ -1355,21 +1355,17 @@
 ET_NAME,
-%0A%09%09%09%09
+ 
 language
@@ -1680,21 +1680,17 @@
 ET_NAME,
-%0A%09%09%09%09
+ 
 packages

Return 404 response if meta file not found