package requestHandlers.djonDB;

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.djonDBUtil;
import djondb.BSONArrayObj;
import djondb.BSONObj;
import djondb.DjondbConnection;

public class addItem_djon 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);
		DjondbConnection c;
		
		int rid = req.getParameter("rid") != null && !req.getParameter("rid").equals("") ? Integer.parseInt(req.getParameter("rid")) : -1;
		String itemName = req.getParameter("item");
		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);
		System.out.println("subtotal = " + subtotal);
		if(itemPrice > 0 && rid>=0 && qty>0 && taxRate>=0.0 && itemName != null && !itemName.equals(""))
		{
			try
			{
				HttpSession session = req.getSession(false);
				c = djonDBUtil.openConn();
				BSONObj userDoc = djonDBUtil.getCurrentUser(session, c);
				if(userDoc != null)
				{
					BSONArrayObj receipts = userDoc.getBSONArray(djonDBUtil.USER_RECEIPTS_FIELDNAME);
					for(int i=0; i<receipts.length(); ++i)
					{
						BSONObj receipt = receipts.get(i);
						if(receipt.getInt(djonDBUtil.USER_RECEIPT_ID_FIELDNAME) == rid)
						{
							BSONArrayObj items = receipt.getBSONArray(djonDBUtil.USER_RECEIPT_ITEMS_FIELDNAME);
							BSONObj item = new BSONObj();
							item.add(djonDBUtil.USER_ITEM_NAME_FIELDNAME, itemName);
							item.add(djonDBUtil.USER_ITEM_PRICE_FIELDNAME, itemPrice);
							item.add(djonDBUtil.USER_ITEM_QTY_FIELDNAME, qty);
							item.add(djonDBUtil.USER_ITEM_TAXRATE_FIELDNAME, taxRate);
							items.add(item);
							double total = subtotal + receipt.getDouble(djonDBUtil.USER_RECEIPT_TOTAL_FIELDNAME);
							System.out.println("total = " + total);
							receipt.add(djonDBUtil.USER_RECEIPT_TOTAL_FIELDNAME, total);
							c.update(djonDBUtil.DBNAME, djonDBUtil.USERS_COLLECTION_NAME, userDoc);
							ret.put("ret", true);
							break;
						}
					}
				}
			}
			catch (Throwable theException) 	    
			{
			     System.out.println(theException); 
			}
		}
		out.print(ret);
		out.close();
	}

}
