package requestHandlers.djonDB;

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.djonDBUtil;
import djondb.BSONArrayObj;
import djondb.BSONObj;
import djondb.DjondbConnection;

public class getCards_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();
		JSONObject ret = new JSONObject();
		ret.put("ret", false);
		DjondbConnection c;
		JSONArray arr = new JSONArray();
		try
		{
			HttpSession session = req.getSession(false);
			c = djonDBUtil.openConn();
			BSONObj userDoc = djonDBUtil.getCurrentUser(session, c);
			if(userDoc != null)
			{
				BSONArrayObj cards = userDoc.getBSONArray(djonDBUtil.USER_CARDS_FIELDNAME);
				for(int i=0; i<cards.length(); ++i)
				{
					arr.put(cards.get(i).getString("number"));
				}
				ret.put("ret", true);
			}
		}
		catch (Throwable theException) 	    
		{
		     System.out.println(theException); 
		}
		finally
		{
			ret.put("cards", arr);
			out.print(ret);
			out.close();
		}		
	}

}
