<%

var coffedata = {
	"ADDITIONS" : [{
		"name" : "Milk",
		"cost" : "$1"
	}, {
		"name" : "Peppermint",
		"cost" : "$1.5"
	}, {
		"name" : "Caramel",
		"cost" : "$1.6"
	}, {
		"name" : "Whip Cream",
		"cost" : "$2.1"
	}],
	"DRINKS" : [{
		"name" : "Espresso",
		"cost" : "$5"
	}, {
		"name" : "Percolator",
		"cost" : "$4"
	}, {
		"name" : "Colombian Coffee",
		"cost" : "$7"
	}, {
		"name" : "Hawaiian Kona",
		"cost" : "$6"
	}, {
		"name" : "COCO",
		"cost" : "$6"
	}]
};

function addOrder(orderName) {
	if(session.get("count") == null) {
		session.put("count", 0);
	}
	var orderIdx = session.get("count");

	var orderO = session.get("orders");
	if(orderO != null) {
		//orderIdx = orderO.length;
		var orderCount = session.get("count");
		orderCount++;
		session.put("count", orderCount);
		orderIdx = orderCount;
	}

	var order = request.getParameter('order');
	var price = 0;
	//menuID.info(coffedata);
	for(var key in coffedata.DRINKS) {
		if(coffedata.DRINKS[key].name == orderName) {
			price = coffedata.DRINKS[key].cost;
		}
	}
	if(price != 0) {
		var orders;
		if(orderO != null) {
			var orderJson = session.get("orders");
			orderJson.push(orderIdx);
			session.put("orders", orderJson);
		} else {
			session.put("orders", [0]);
		}
		var object = {
			"ORDER_ID" : orderIdx,
			"ADDITION" : "",
			"DRINK" : order,
			"COST" : price,
			"STATUS" : "In Progress",
			"PAY" : false
		};
		session.put("o" + orderIdx, object);
		//order is been taken
		var result = session.get("o" + orderIdx);
		print(result);
	} else {
		print({
			"Infor" : "Sorry, Coffee is not in shop"
		});
	}
}

function listAllOrders() {
	var orderO = session.get("orders");
	var listOrder = [];
	if(orderO != null) {
		var order
		for(var key in orderO) {
			order = session.get("o" + orderO[key]);
			listOrder.push(order);
		}
		print({
			"orders" : listOrder
		});
	} else {

		print({
			"Infor" : "Sorry, Orders are not exsit"
		});
	}
}

function updateStatus(orderid, status) {
	//get order from session
	var order = session.get("o" + orderid);
	//checking order is there
	if(order.length != 0) {
		//Order is existing
		order.STATUS = status;
		session.put("o" + orderid, order);
		print(order);
	} else {
		print({
			"Infor" : "Sorry, Order is not exsit"
		});
	}

}

function addAddition(orderid, addition) {

	//adding addition
	var order = session.get("o" + orderid);
	//checking order is there
	if(order != null) {
		//checking for status
		if(order.STATUS == "In Progress") {
			//Order is existing
			//processing new price
			var price = 0;
			for(var key in coffedata.ADDITIONS) {
				if(coffedata.ADDITIONS[key].name == addition) {
					price = coffedata.ADDITIONS[key].cost;
				}

			}
			if(price != 0) {
				var newPrice = parseFloat(price.substring(1)) + parseFloat(order.COST.substring(1));
				order.ADDITION = addition;
				order.COST = "$" + newPrice;
				order.STATUS = "Ready";
				session.put("o" + orderid, order);
				print(order);
			} else {
				print({
					"Infor" : "Sorry, Addition is not in shop"
				});
			}
		} else {
			if(order.STATUS == "Ready") {
				print({
					"Infor" : "Sorry, Order can be added addition only one time"
				});
			} else if(order.STATUS == "Complete") {
				print({
					"Infor" : "Sorry, Order has been completed"
				});
			} else {
				print({
					"Infor" : "Sorry, Order has be send to prepared"
				});
			}

		}
	} else {

		print({
			"Infor" : "Sorry, Order is not exsit"
		});
	}

}

function viewOrder(orderid) {
	var order = session.get("o" + orderid);
	if(order != null) {
		print(order);
	} else {

		print({
			"Infor" : "Sorry, Order is not exsit"
		});
	}

}

function deleteOrder(orderid) {
	//db processing
	var order = session.get("o" + orderid);
	var orderO = session.get("orders");

	//checking order is there
	if(order != null && orderO != null) {
		session.remove("o" + orderid);
		for(var key in orderO) {
			if(orderO[key] == orderid) {
				orderO.splice(key, 1);
				session.put("orders", orderO);
			}
		}
		print({
			"Infor" : "Order id " + orderid + " is removed"
		});
	} else {
		print({
			"Infor" : "Sorry, Order id " + orderid + " is not exist to remove"
		});
	}

}

%>