package io.core9.commerce.cart;

import io.core9.module.auth.AuthenticationPlugin;
import io.core9.module.auth.Session;
import io.core9.plugin.server.handler.Middleware;
import io.core9.plugin.server.request.Request;
import io.core9.plugin.server.vertx.VertxServer;

import java.util.ArrayList;
import java.util.Map;

import net.xeoh.plugins.base.annotations.PluginImplementation;
import net.xeoh.plugins.base.annotations.events.PluginLoaded;
import net.xeoh.plugins.base.annotations.injections.InjectPlugin;

@PluginImplementation
public class CartActionsImpl implements CartActions {
	
	@InjectPlugin
	private AuthenticationPlugin auth;
	
	@PluginLoaded
	public void onServerLoaded(VertxServer server) {
		server.use("/core/cart(/:itemid)*", new Middleware() {
			@Override
			public void handle(Request request) {
				Session session = auth.getUser(request).getSession();
				Cart cart = (Cart) session.getAttribute("cart");
				if(cart == null) {
					cart = new Cart();
				}
				switch(request.getMethod()) {
				case POST:
					if(request.getParams().get("delete") != null) {
						deleteItemFromCart(cart, (String) request.getParams().get("itemid"));
					} else if (request.getParams().get("update") != null) {
						updateItemInCart(cart, request.getBodyAsMap());
					} else {
						addItemToCart(cart, (String) request.getParams().get("itemid"), request.getBodyAsMap());
					}
					break;
				case DELETE:
					deleteItemFromCart(cart, (String) request.getParams().get("itemid"));
					break;
				case PUT:
					updateItemInCart(cart, request.getBodyAsMap());
					break;
				default:
					break;
				}
				session.setAttribute("cart", cart);
				if(request.getBodyAsMap().get("redirect") != null) {
					request.getResponse().sendRedirect(301, (String) request.getBodyAsMap().get("redirect"));
				} else {
					request.getResponse().sendJsonArray(new ArrayList<LineItem>(cart.getItems().values()));
				}
			}
		});
	}

	private void deleteItemFromCart(Cart cart, String itemId) {
		if(itemId != null) {
			cart.getItems().remove(itemId);
		}
	}
	
	private void addItemToCart(Cart cart, String itemId, Map<String, Object> bodyAsMap) {
		cart.addItem(
				itemId, 
				Integer.parseInt((String) bodyAsMap.get("quantity")), 
				Integer.parseInt((String) bodyAsMap.get("price")), 
				(String) bodyAsMap.get("description"));
	}
	
	private void updateItemInCart(Cart cart, Map<String, Object> bodyAsMap) {
		LineItem item = cart.getItems().get(bodyAsMap.get("id"));
		if(bodyAsMap.containsKey("quantity")) {
			item.setQuantity(Integer.parseInt((String) bodyAsMap.get("quantity")));
		}
	}
}

@@ -284,24 +284,50 @@
 .ArrayList;%0A
+import java.util.HashMap;%0A
 import java.
@@ -660,16 +660,79 @@
 auth;%0A%09%0A
+%09@InjectPlugin%0A%09private CommerceEncryptionPlugin encryption;%0A%09%0A
 %09@Plugin
@@ -2238,39 +2238,23 @@
 %7B%0A%09%09
-cart.addItem(%0A%09%09%09%09itemId, %0A%09%09%09%09
+int quantity = 
 Inte
@@ -2301,16 +2301,103 @@
 ntity%22))
+;%0A%09%09if(quantity %3E 0 && isValid(bodyAsMap)) %7B%0A%09%09%09cart.addItem(%0A%09%09%09%09itemId, %0A%09%09%09%09quantity
 , %0A%09%09%09%09I
@@ -2491,16 +2491,20 @@
 ion%22));%0A
+%09%09%7D%0A
 %09%7D%0A%09%0A%09pr
@@ -2685,24 +2685,22 @@
 %09%09%09i
-tem.setQ
+nt q
 uantity
-(
+ = 
 Inte
@@ -2747,20 +2747,610 @@
 ntity%22))
-);%0A%09%09%7D
+;%0A%09%09%09if(quantity %3E 0) %7B%0A%09%09%09%09item.setQuantity(quantity);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09%0A%09private boolean isValid(Map%3CString,Object%3E body) %7B%0A%09%09String%5B%5D fields = getHashFields(body);%0A%09%09Map%3CString,Object%3E validation = new HashMap%3CString,Object%3E();%0A%09%09for(String field : fields) %7B%0A%09%09%09validation.put(field, body.get(field));%0A%09%09%7D%0A%09%09return body.get(%22hash%22).equals(encryption.encrypt(validation));%0A%09%7D%0A%09%0A%09private String%5B%5D getHashFields(Map%3CString,Object%3E body) %7B%0A%09%09String fields = (String) body.get(%22hashfields%22);%0A%09%09fields = fields.replaceAll(%22%5C%5C%5B%22, %22%22).replaceAll(%22%5C%5C%5D%22, %22%22).replaceAll(%22 %22, %22%22);%0A%09%09return fields.split(%22,%22);
 %0A%09%7D%0A%7D%0A

use hashfields for validation