package requestHandlers.djonDB;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

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

import org.json.JSONArray;

import requestHandlers.RequestHandler;
import Util.djonDBUtil;
import djondb.BSONArrayObj;
import djondb.BSONObj;
import djondb.DjondbConnection;

public class getStatePie_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();
		JSONArray ret = new JSONArray();
		DjondbConnection c;
		try
		{
			HttpSession session = req.getSession(false);
			c = djonDBUtil.openConn();
			//BSONArrayObj userArr = c.find(djonDBUtil.DBNAME, djonDBUtil.USERS_COLLECTION_NAME, "$'email' == 'djiang1@student.gsu.edu' and $'_id' == 'dddac36a-ac7b-4d85-beb5-bc5c94b3a888'");
			//BSONObj userDoc = userArr.get(0);
			BSONObj userDoc = djonDBUtil.getCurrentUser(session, c);
			if(userDoc != null)
			{
				String year = req.getParameter("year");
				int y = Calendar.getInstance().get(Calendar.YEAR);
				try
				{
					y = Integer.parseInt(year);
				}
				catch(NumberFormatException nfe)
				{
					y = -1;
					System.err.println(nfe);
				}
				//System.out.println("y = " + y);
				BSONArrayObj receipts = userDoc.getBSONArray(djonDBUtil.USER_RECEIPTS_FIELDNAME);
				Map<String, Double> map = new HashMap<String, Double>();
				if(y >= 1990)
				{
					for(int i=0; i<receipts.length(); ++i)
					{
						BSONObj receipt = receipts.get(i);
						String date = receipt.getString(djonDBUtil.USER_RECEIPT_PURCHASEDATE_FIELDNAME);
						int yy = djonDBUtil.getYear(date);
						//System.out.println("yy = " + yy);
						if(y == yy)
						{
							String merchantType = djonDBUtil.getMerchantType(c, receipt.getString(djonDBUtil.USER_RECEIPT_MERCHANT_FIELDNAME));
							if(!map.containsKey(merchantType))
							{
								map.put(merchantType, 0.0);
							}
							Double sum = map.get(merchantType);
							sum += receipt.getDouble(djonDBUtil.USER_RECEIPT_TOTAL_FIELDNAME);
							map.put(merchantType, sum);
						}
					}
				}
				else
				{
					// taken as "all"
					for(int i=0; i<receipts.length(); ++i)
					{
						BSONObj receipt = receipts.get(i);
						String date = receipt.getString(djonDBUtil.USER_RECEIPT_PURCHASEDATE_FIELDNAME);
						String merchantType = djonDBUtil.getMerchantType(c, receipt.getString(djonDBUtil.USER_RECEIPT_MERCHANT_FIELDNAME));
						if(!map.containsKey(merchantType))
						{
							map.put(merchantType, 0.0);
						}
						Double sum = map.get(merchantType);
						sum += receipt.getDouble(djonDBUtil.USER_RECEIPT_TOTAL_FIELDNAME);
						map.put(merchantType, sum);
					}
				}
				for(Map.Entry<String, Double> entry : map.entrySet())
				{
					JSONArray arr = new JSONArray();
					arr.put(entry.getKey());
					arr.put(entry.getValue());
					ret.put(arr);
				}
			}
		}
		catch (Throwable theException) 	    
		{
		     System.out.println(theException); 
		}
		finally
		{
			out.print(ret);
			out.close();
		}		
	}
}
