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 getItems_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();
		JSONArray temp = 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
		{
			HttpSession session = req.getSession(false);
			c = MongoDBUtil.openConn();
			DBObject userDoc = MongoDBUtil.getCurrentUser(session, c);
			if(userDoc != null)
			{
				int rid = -1;
				int page = 1;
				int limit = 5;
				try
				{
					rid = req.getParameter("rid") == null || req.getParameter("rid").equals("") ? -1 : Integer.parseInt(req.getParameter("rid"));
					page = req.getParameter("page") == null || req.getParameter("page").equals("") ? 1 : Integer.parseInt(req.getParameter("page")); // get the requested page
					limit = req.getParameter("rows") == null || req.getParameter("rows").equals("") ? 5 : Integer.parseInt(req.getParameter("rows"));
				}catch(NumberFormatException nfe)
				{
					System.err.println(nfe);
				}
				
				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");

				System.out.println("page = " + req.getParameter("page"));
				System.out.println("limit = " + req.getParameter("rows"));
				System.out.println("sidx = " + req.getParameter("sidx"));
				System.out.println("sord = " + req.getParameter("sord"));
				
				// search request
				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");
				
				BasicDBList receipts = (BasicDBList) userDoc.get(MongoDBUtil.USER_RECEIPTS_FIELDNAME);
				if(receipts != null && receipts.size() > 0)
				{
					for(int i=0; i<receipts.size(); ++i)
					{
						BasicDBObject receipt = (BasicDBObject)receipts.get(i);
						if(rid == receipt.getInt(MongoDBUtil.RECEIPT_ID_FIELDNAME));
						{
							BasicDBList items = (BasicDBList) receipt.get(MongoDBUtil.RECEIPT_ITEMS_FIELDNAME);
							int count = items.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 > items.size())
								end = items.size();
							for(int j=start; j<end; ++j)
							{
								BasicDBObject item = (BasicDBObject)items.get(j);
								JSONArray record = new JSONArray();
								double p = item.getDouble(MongoDBUtil.ITEM_PRICE_FIELDNAME);
								int q = item.getInt(MongoDBUtil.ITEM_QTY_FIELDNAME);
								double t = item.getDouble(MongoDBUtil.ITEM_TAXRATE_FIELDNAME);
								record.put(item.getString(MongoDBUtil.ITEM_NAME_FIELDNAME));
								record.put(p);
								record.put(q);
								record.put(t);
								record.put(q*p*(1+t));
								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();
		}
	}

}
