package ws;

import org.json.JSONObject;

import djondb.BSONArrayObj;
import djondb.BSONObj;
import djondb.DjondbConnection;

import Util.djonDBUtil;

public class MyReceiptsWS {
	public String addReceipt(final String User, final String Card, final String Merchant, final String PurchaseDate)
	{
		JSONObject ret = new JSONObject();
		DjondbConnection c;
		try
		{
			c = djonDBUtil.openConn();
			StringBuilder sb = new StringBuilder();
			sb.append("$'email' == '").append(User).append("'");
			BSONArrayObj userArr = c.find(djonDBUtil.DBNAME, djonDBUtil.USERS_COLLECTION_NAME, sb.toString());
			BSONObj userDoc = null;
			if(userArr != null)
			{
				userDoc = userArr.get(0);
			}
			if(userDoc != null)
			{
				BSONArrayObj receipts = userDoc.getBSONArray(djonDBUtil.USER_RECEIPTS_FIELDNAME);
				BSONObj receipt = new BSONObj();
				int id = receipts.length();
				System.err.println("rid = " + id);
				receipt.add(djonDBUtil.USER_RECEIPT_ID_FIELDNAME, id);
				receipt.add(djonDBUtil.USER_RECEIPT_MERCHANT_FIELDNAME, Merchant);
				receipt.add(djonDBUtil.USER_RECEIPT_PURCHASEDATE_FIELDNAME, PurchaseDate);
				receipt.add(djonDBUtil.USER_RECEIPT_CARD_FIELDNAME, Card);
				receipt.add(djonDBUtil.USER_RECEIPT_ITEMS_FIELDNAME, new BSONArrayObj());
				receipt.add(djonDBUtil.USER_RECEIPT_IMG_FIELDNAME, "./receiptsImgs/"+id+".jpg");
				receipt.add(djonDBUtil.USER_RECEIPT_TOTAL_FIELDNAME, 0.0);		
				receipts.add(receipt);
				c.update(djonDBUtil.DBNAME, djonDBUtil.USERS_COLLECTION_NAME, userDoc);
				ret.put("ret", true);
			}
		}
		catch (Throwable theException) 	    
		{
		     System.out.println(theException); 
		     ret.put("msg", theException.getMessage());
		}
		return ret.toString();
	}
	
	public String addItem(final int ReceiptID, final String User, final String itemName, final double itemPrice, final int qty, final double taxRate)
	{
		JSONObject ret = new JSONObject();
		ret.put("ret", false);
		DjondbConnection c;
		try
		{
			c = djonDBUtil.openConn();

			if(itemPrice > 0 && ReceiptID>0 && qty>0 && taxRate>=0.0 && itemName != null && !itemName.equals(""))
			{
				double subtotal = itemPrice * qty * (1 + taxRate);
				StringBuilder sb = new StringBuilder();
				sb.append("$'email' == '").append(User).append("'");
				BSONArrayObj userArr = c.find(djonDBUtil.DBNAME, djonDBUtil.USERS_COLLECTION_NAME, sb.toString());
				BSONObj userDoc = null;
				if(userArr != null)
				{
					userDoc = userArr.get(0);
				}
				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) == ReceiptID)
						{
							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);
							receipt.add(djonDBUtil.USER_RECEIPT_TOTAL_FIELDNAME, total);
							c.update(djonDBUtil.DBNAME, djonDBUtil.USERS_COLLECTION_NAME, userDoc);
							ret.put("ret", true);
							break;
						}
					}
					ret.put("msg", "no such receipt");
				}
			}
		}
		catch (Throwable theException) 	    
		{
		     System.out.println(theException); 
		     ret.put("msg", theException.getMessage());
		}
		return ret.toString();
	}
}
