package io.yawp.repository.shields;

import static io.yawp.repository.query.condition.Condition.c;
import io.yawp.commons.http.annotation.GET;
import io.yawp.commons.http.annotation.PUT;
import io.yawp.repository.IdRef;
import io.yawp.repository.models.basic.ShieldedObject;
import io.yawp.repository.models.basic.facades.ShieldedObjectFacades.AmyFacade;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;

public class ObjectShield extends Shield<ShieldedObject> {

	@Override
	public void always() {
		allow(isJim());
		allow(isAmy()).facade(AmyFacade.class);
	}

	@Override
	public void index(IdRef<?> parentId) {
		allow(isKurt()).where(c("stringValue", "=", "ok"));
	}

	@Override
	public void show(IdRef<ShieldedObject> id) {
		allow(isRobert());
		allow(isId100(id));
		allow(isKurt()).where(c("stringValue", "=", "ok"));
	}

	@Override
	public void create(List<ShieldedObject> objects) {
		allow(isRequestWithValidObjects(objects));
		allow(isKurt()).where(c("stringValue", "=", "ok"));
		allow(isJanis()).where(c("stringValue", "=", "ok-for-janis"));
	}

	@Override
	public void update(IdRef<ShieldedObject> id, ShieldedObject object) {
		allow(isRobert());
		allow(isId100(id));
		allow(isRequestWithValidObject(object));
		allow(isKurt()).where(c("stringValue", "=", "ok"));
		allow(isJanis()).where(c("stringValue", "=", "ok-for-janis"));
	}

	@Override
	public void destroy(IdRef<ShieldedObject> id) {
		allow(isId100(id));
		allow(isJanis()).where(c("stringValue", "=", "ok-for-janis"));
	}

	@Override
	public void custom() {
		allow(isRobert());
	}

	@PUT("something")
	public void something(IdRef<ShieldedObject> id) {
		allow(isJanis()).where(c("stringValue", "=", "ok-for-janis"));
	}

	@PUT("anotherthing")
	public void anotherthing(IdRef<ShieldedObject> id, Map<String, String> params) {
		allow(isId100(id));
		allow(params.containsKey("x") && params.get("x").equals("ok"));
	}

	@GET("collection")
	public void collection() {
		allow();
	}

	private boolean isRobert() {
		return is("robert@rock.com");
	}

	private boolean isJim() {
		return is("jim@rock.com");
	}

	private boolean isKurt() {
		return is("kurt@rock.com");
	}

	private boolean isJanis() {
		return is("janis@rock.com");
	}

	private boolean isAmy() {
		return is("amy@rock.com");
	}

	private boolean is(String email) {
		User currentUser = UserServiceFactory.getUserService().getCurrentUser();
		return currentUser != null && currentUser.getEmail().equals(email);
	}

	private boolean isId100(IdRef<ShieldedObject> id) {
		return id.asLong().equals(100l);
	}

	private boolean isRequestWithValidObject(ShieldedObject object) {
		if (object == null) {
			return false;
		}
		return isRequestWithValidObjects(Arrays.asList(object));
	}

	private boolean isRequestWithValidObjects(List<ShieldedObject> objects) {
		if (!requestHasAnyObject()) {
			return false;
		}

		for (ShieldedObject objectInList : objects) {
			if (!isValidObject(objectInList)) {
				return false;
			}
		}

		return true;
	}

	private boolean isValidObject(ShieldedObject object) {
		if (object.getStringValue() == null) {
			return false;
		}
		return object.getStringValue().equals("valid object");
	}
}

@@ -34,70 +34,8 @@
 s;%0A%0A
-import static io.yawp.repository.query.condition.Condition.c;%0A
 impo
@@ -74,16 +74,16 @@
 on.GET;%0A
+
 import i
@@ -687,34 +687,32 @@
 isKurt()).where(
-c(
 %22stringValue%22, %22
@@ -712,33 +712,32 @@
 lue%22, %22=%22, %22ok%22)
-)
 ;%0A%09%7D%0A%0A%09@Override
@@ -842,34 +842,32 @@
 isKurt()).where(
-c(
 %22stringValue%22, %22
@@ -871,25 +871,24 @@
 , %22=%22, %22ok%22)
-)
 ;%0A%09%7D%0A%0A%09@Over
@@ -1005,34 +1005,32 @@
 isKurt()).where(
-c(
 %22stringValue%22, %22
@@ -1030,33 +1030,32 @@
 lue%22, %22=%22, %22ok%22)
-)
 ;%0A%09%09allow(isJani
@@ -1057,34 +1057,32 @@
 sJanis()).where(
-c(
 %22stringValue%22, %22
@@ -1092,33 +1092,32 @@
  %22ok-for-janis%22)
-)
 ;%0A%09%7D%0A%0A%09@Override
@@ -1290,34 +1290,32 @@
 isKurt()).where(
-c(
 %22stringValue%22, %22
@@ -1323,17 +1323,16 @@
 %22, %22ok%22)
-)
 ;%0A%09%09allo
@@ -1342,34 +1342,32 @@
 sJanis()).where(
-c(
 %22stringValue%22, %22
@@ -1377,33 +1377,32 @@
  %22ok-for-janis%22)
-)
 ;%0A%09%7D%0A%0A%09@Override
@@ -1490,34 +1490,32 @@
 sJanis()).where(
-c(
 %22stringValue%22, %22
@@ -1525,33 +1525,32 @@
  %22ok-for-janis%22)
-)
 ;%0A%09%7D%0A%0A%09@Override
@@ -1698,10 +1698,8 @@
 ere(
-c(
 %22str
@@ -1729,17 +1729,16 @@
 -janis%22)
-)
 ;%0A%09%7D%0A%0A%09@

remove c from example