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.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 getReceipts2_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();
		JSONArray rows = new JSONArray();
		ret.put("ret", false);
		ret.put("page", 1);
		ret.put("total", 0);
		ret.put("records", 0);
		ret.put("rows", rows);
		MongoClient c;
		try
		{
			int page = req.getParameter("page") == null || req.getParameter("page").equals("") ? 1 : Integer.parseInt(req.getParameter("page")); // get the requested page
			int limit = req.getParameter("rows") == null || req.getParameter("rows").equals("") ? 5 : Integer.parseInt(req.getParameter("rows"));
			String sidx = req.getParameter("sidx") == null || req.getParameter("sidx").equals("") ? "date" : req.getParameter("sidx");
			String sord = req.getParameter("sord") == null || req.getParameter("sord").equals("") ? "asc" : req.getParameter("sord");
			
			boolean _search = req.getParameter("_search") == null || req.getParameter("_search").equals("") ? false : Boolean.parseBoolean(req.getParameter("_search"));
			String field = req.getParameter("searchField");
			String oper = req.getParameter("searchOper");
			String str = req.getParameter("searchString");
			
			HttpSession session = req.getSession(false);
			c = MongoDBUtil.openConn();
			DBObject userDoc = MongoDBUtil.getCurrentUser(session, c);
			/*
			DB db = c.getDB(MongoDBUtil.DBNAME);
			DBCollection users = db.getCollection(MongoDBUtil.USERS_COLLECTION_NAME);
			BasicDBObject searchQuery = new BasicDBObject();
			searchQuery.put(MongoDBUtil.USER_EMAIL_FIELDNAME, "djiang1@student.gsu.edu");
			DBCursor cursor = users.find(searchQuery);
			DBObject userDoc = cursor.next();
			*/
			//System.err.println("-----" + userDoc);
			if(userDoc != null)
			{
				//System.err.println(userDoc.get(MongoDBUtil.USER_EMAIL_FIELDNAME));
				BasicDBList receipts = (BasicDBList) userDoc.get(MongoDBUtil.USER_RECEIPTS_FIELDNAME);
				if(receipts != null && receipts.size() > 0)
				{
					int count = receipts.size();
					int total_pages = 1;
					if(count > 0 && limit > 0)
					{
						total_pages = count / limit;
						if(count % limit > 0)
							total_pages++;
					}
					if(page > total_pages)
					{
						page = total_pages;
					}
					int start = limit * page - limit;
					if(start < 0)
					{
						start = 0;
					}
					int end = start + limit;
					if(end > receipts.size())
						end = receipts.size();
					System.out.println("count = " + count);
					System.out.println("total_pages = " + total_pages);
					System.out.println("page = " + page);
					System.out.println("start = " + start);
					
					for(int i=start; i<end; ++i)
					{
						BasicDBObject receipt = (BasicDBObject)receipts.get(i);
						JSONArray record = new JSONArray();
						record.put(receipt.getInt(MongoDBUtil.RECEIPT_ID_FIELDNAME));
						record.put(receipt.getString(MongoDBUtil.RECEIPT_MERCHANT_FIELDNAME));
						record.put(receipt.getDouble(MongoDBUtil.RECEIPT_TOTAL_FIELDNAME));
						record.put(receipt.getString(MongoDBUtil.RECEIPT_CARD_FIELDNAME));
						record.put(receipt.getString(MongoDBUtil.RECEIPT_PURCHASEDATE_FIELDNAME));
						rows.put(record);
					}

					ret.put("page", page);
					ret.put("total", total_pages);
					ret.put("records", count);
					ret.put("rows", rows);
					ret.put("ret", true);
				}
			}
		}
		catch (Throwable theException) 	    
		{
		     System.out.println(theException); 
		}
		finally
		{
			out.print(ret);
			out.close();
		}		
	}

}
