package requestHandlers.mongoDB;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Iterator;

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

import org.json.JSONArray;
import org.json.JSONObject;

import requestHandlers.RequestHandler;
import Util.MongoDBUtil;

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

public class getSpendingCurve_mongo implements RequestHandler{
	private final int NUM_OF_MONTH = 12;
	@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();
		JSONArray series = new JSONArray();
		MongoClient c;
		try
		{
			HttpSession session = req.getSession(false);
			c = MongoDBUtil.openConn();
			DBObject userDoc = MongoDBUtil.getCurrentUser(session, c);
			if(userDoc != null)
			{
				int year = Calendar.getInstance().get(Calendar.YEAR);
				try
				{
					year = Integer.parseInt(req.getParameter("year"));
				}catch(NumberFormatException nfe)
				{
					System.err.println(nfe);
					year = Calendar.getInstance().get(Calendar.YEAR);
				}
				
				BasicDBList receipts = (BasicDBList) userDoc.get(MongoDBUtil.USER_RECEIPTS_FIELDNAME);
				JSONObject json = new JSONObject();
				JSONArray record = new JSONArray();
				for(int i=1; i<=NUM_OF_MONTH; ++i)
				{
					record.put(0.0);
				}
				json.put("Total", record);
				if(receipts != null && receipts.size() > 0)
				{
					for(int i=0; i<receipts.size(); ++i)
					{
						BasicDBObject receipt = (BasicDBObject)receipts.get(i);
						String date = receipt.getString(MongoDBUtil.RECEIPT_PURCHASEDATE_FIELDNAME);
						int y = MongoDBUtil.getYear(date);
						int m = MongoDBUtil.getMonth(date);
						String merchantType = MongoDBUtil.getMerchantType(c, receipt.getString(MongoDBUtil.RECEIPT_MERCHANT_FIELDNAME));
						if(y == year)
						{
							JSONArray arr = json.getJSONArray("Total");
							arr.put(m-1, (Double)arr.get(m-1) + receipt.getDouble(MongoDBUtil.RECEIPT_TOTAL_FIELDNAME));
							if(!json.has(merchantType))
							{
								record = new JSONArray();
							
								for(int j=1; j<=NUM_OF_MONTH; ++j)
								{
									record.put(0.0);
								}
								json.put(merchantType, record);
							}
							arr = json.getJSONArray(merchantType);
							arr.put(m-1, (Double)arr.get(m-1) + receipt.getDouble(MongoDBUtil.RECEIPT_TOTAL_FIELDNAME));
						}
					}
				}
				Iterator iter = json.keys();
				while(iter.hasNext())
				{
					JSONObject obj = new JSONObject();
					String k = iter.next().toString();
					obj.put("name", k);
					obj.put("data", json.get(k));
					series.put(obj);
				}
			}
		}
		catch (Throwable theException) 	    
		{
		     System.out.println(theException); 
		}
		finally
		{
			out.print(series);
			out.close();
		}	
	}

}
