package org.cru.godtools.api.v2;

import com.google.common.base.Optional;
import org.ccci.util.time.Clock;
import org.cru.godtools.api.v2.functions.DraftTranslation;
import org.cru.godtools.domain.authentication.AuthorizationRecord;
import org.cru.godtools.domain.authentication.AuthorizationService;
import org.cru.godtools.domain.languages.Language;
import org.cru.godtools.domain.languages.LanguageService;
import org.cru.godtools.domain.packages.Package;
import org.cru.godtools.domain.packages.PackageService;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.UUID;

@Path("/v2/languages")
public class LanguageResource
{

	@Inject
	AuthorizationService authorizationService;
	@Inject
	LanguageService languageService;
	@Inject
	PackageService packageService;

	@Inject
	DraftTranslation draftTranslation;

	@Inject
	Clock clock;

	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	public Response addLanguage(Language language, @HeaderParam("Authorization") String authorization)
	{
		Optional<AuthorizationRecord> authorizationRecord = authorizationService.getAuthorizationRecord(authorization, null);

		AuthorizationRecord.checkAdminAccess(authorizationRecord, clock.currentDateTime());

		if(languageService.languageExists(language))
		{
			return Response
					.status(400)
					.build();
		}

		language.setId(UUID.randomUUID());

		languageService.insert(language);

		for(Package gtPackage : packageService.selectAllPackages())
		{
			draftTranslation.create(language.getCode(), gtPackage.getCode());
		}

		authorizationService.updateAdminRecordExpiration(authorizationRecord.get(), 4);

		return Response
				.status(201)
				.build();
	}
}

@@ -1154,16 +1154,161 @@
 ion)%0A%09%7B%0A
+%09%09if(language == null)%0A%09%09%7B%0A%09%09%09return Response%0A%09%09%09%09%09.status(Response.Status.BAD_REQUEST)%0A%09%09%09%09%09.entity(%22Language was missing%22)%0A%09%09%09%09%09.build();%0A%09%09%7D%0A%0A
 %09%09Option

Return 400 if language is not passed on create language