package requestHandlers.mongoDB;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;

import requestHandlers.RequestHandler;
import Util.MongoDBUtil;
import Util.djonDBUtil;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class addItem_mongo implements RequestHandler{

	@Override
	public void processRequest(Object user, HttpServletRequest req,
			HttpServletResponse res, Object[] services, Object log)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = res.getWriter();
		JSONObject ret = new JSONObject();
		ret.put("ret", false);
		MongoClient c;
		
		int rid = req.getParameter("rid") != null && !req.getParameter("rid").equals("") ? Integer.parseInt(req.getParameter("rid")) : -1;
		String itemName = req.getParameter("name");
		double itemPrice = req.getParameter("price") != null && !req.getParameter("price").equals("") ? Double.parseDouble(req.getParameter("price")) : -1.0;
		double qty = req.getParameter("qty") != null && !req.getParameter("qty").equals("") ? Double.parseDouble(req.getParameter("qty")) : -0.0;
		double taxRate = req.getParameter("taxRate") != null && !req.getParameter("taxRate").equals("") ? Double.parseDouble(req.getParameter("taxRate")) : -1.0;
		double subtotal = itemPrice * qty * (1 + taxRate);
		System.out.println("rid = " + rid);
		System.out.println("itemName = " + itemName);
		System.out.println("itemPrice = " + itemPrice);
		System.out.println("taxRate = " + taxRate);
		System.out.println("Qty = " + qty);
		if(itemPrice > 0 && rid>0 && qty>0 && taxRate>=0.0 && itemName != null && !itemName.equals(""))
		{
			try
			{
				HttpSession session = req.getSession(false);
				c = MongoDBUtil.openConn();
				DBObject userDoc = MongoDBUtil.getCurrentUser(session, c);
				if(userDoc != null)
				{
					BasicDBList receipts = (BasicDBList) userDoc.get(MongoDBUtil.USER_RECEIPTS_FIELDNAME);
					if(receipts != null && receipts.size() > 0)
					{
						for(int i=0; i<receipts.size(); ++i)
						{
							BasicDBObject receipt = (BasicDBObject)receipts.get(i);
							if(receipt.getInt(MongoDBUtil.RECEIPT_ID_FIELDNAME) == rid)
							{
								BasicDBList items = (BasicDBList)receipt.get(MongoDBUtil.RECEIPT_ITEMS_FIELDNAME);
								BasicDBObject item = new BasicDBObject();
								item.put(djonDBUtil.USER_ITEM_NAME_FIELDNAME, itemName);
								item.put(djonDBUtil.USER_ITEM_PRICE_FIELDNAME, itemPrice);
								item.put(djonDBUtil.USER_ITEM_QTY_FIELDNAME, qty);
								item.put(djonDBUtil.USER_ITEM_TAXRATE_FIELDNAME, taxRate);
								items.add(item);
								ret.put("ret", true);
								double total = subtotal + receipt.getDouble(MongoDBUtil.RECEIPT_TOTAL_FIELDNAME);
								receipt.put(MongoDBUtil.RECEIPT_TOTAL_FIELDNAME, total);
								break;
							}
						}
					}
				}
			}
			catch (Throwable theException) 	    
			{
			     System.out.println(theException); 
			}
			finally
			{
				out.print(ret);
				out.close();
			}
		}
		
	}

}
