package org.cru.godtools.api.translations;

import com.google.common.base.Strings;
import org.cru.godtools.api.utilities.ErrorResponse;
import org.cru.godtools.domain.GodToolsVersion;
import org.cru.godtools.domain.authentication.AuthorizationService;
import org.cru.godtools.domain.authentication.UnauthorizedException;
import org.jboss.logging.Logger;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * Created by ryancarlson on 7/28/14.
 */

@Path("/drafts")
public class DraftResource
{
	@Inject
	AuthorizationService authService;
	@Inject
	GodToolsTranslationRetrievalProcess translationRetrievalProcess;
	@Inject
	GodToolsTranslationService godToolsTranslationService;

	private Logger log = Logger.getLogger(DraftResource.class);

	@GET
	@Path("/{language}")
	@Produces({"application/zip", "application/xml"})
	public Response getTranslations(@PathParam("language") String languageCode,
									@QueryParam("interpreter") Integer minimumInterpreterVersionParam,
									@HeaderParam("interpreter") Integer minimumInterpreterVersionHeader,
									@QueryParam("compressed") String compressed,
									@HeaderParam("authorization") String authTokenHeader,
									@QueryParam("authorization") String authTokenParam) throws IOException
	{
		log.info("Requesting all drafts for language: " + languageCode);

		if(authService.canAccessOrCreateDrafts(authTokenParam, authTokenHeader))
		{
			return translationRetrievalProcess
					.setLanguageCode(languageCode)
					.setMinimumInterpreterVersion(minimumInterpreterVersionHeader == null ? minimumInterpreterVersionParam : minimumInterpreterVersionHeader)
					.setCompressed(Boolean.parseBoolean(compressed))
					.loadDrafts()
					.buildResponse();
		}
		else
		{
			throw new UnauthorizedException();
		}
	}

	@GET
	@Path("/{language}/{package}")
	@Produces({"application/zip", "application/xml"})
	public Response getTranslation(@PathParam("language") String languageCode,
								   @PathParam("package") String packageCode,
								   @QueryParam("interpreter") Integer minimumInterpreterVersionParam,
								   @HeaderParam("interpreter") Integer minimumInterpreterVersionHeader,
								   @QueryParam("compressed") String compressed,
								   @QueryParam("version") BigDecimal versionNumber,
								   @HeaderParam("Authorization") String authTokenHeader,
								   @QueryParam("Authorization") String authTokenParam) throws IOException
	{
		log.info("Requesting draft translation for package: " + packageCode + " and language: " + languageCode);

		if(authService.canAccessOrCreateDrafts(authTokenParam, authTokenHeader))
		{
			return translationRetrievalProcess
					.setLanguageCode(languageCode)
					.setPackageCode(packageCode)
					.setMinimumInterpreterVersion(minimumInterpreterVersionHeader == null ? minimumInterpreterVersionParam : minimumInterpreterVersionHeader)
					.setCompressed(Boolean.parseBoolean(compressed))
					.loadDrafts()
					.buildResponse();
		}
		else
		{
			throw new UnauthorizedException();
		}
	}
}

@@ -41,148 +41,8 @@
 s;%0A%0A
-import com.google.common.base.Strings;%0Aimport org.cru.godtools.api.utilities.ErrorResponse;%0Aimport org.cru.godtools.domain.GodToolsVersion;%0A
 impo
@@ -1222,25 +1222,25 @@
 eaderParam(%22
-a
+A
 uthorization
@@ -1288,17 +1288,17 @@
 yParam(%22
-a
+A
 uthoriza

update Authorization param name