package Util;

import djondb.BSONArrayObj;
import djondb.BSONObj;
import djondb.DjondbConnection;
import djondb.DjondbConnectionManager;
import djondb.LibraryException;
import djondb.Loader;

public class djsonTest {
	public static void main(String[] arg) {
	      try {
	         Loader.initialize();
	         DjondbConnection c = DjondbConnectionManager.getConnection("localhost");

	         if(c.open())
	         {

	         /*BSONObj obj = new BSONObj();
	         obj.add("fname", "Dan");
	         obj.add("lname", "Jiang");
	         obj.add("email", "djiang1@student.gsu.edu");
	         obj.add("password", "djiang");

	         c.insert(djonDBUtil.DBNAME, djonDBUtil.USERS_COLLECTION_NAME, obj);*/

	         BSONArrayObj res = c.find(djonDBUtil.DBNAME, djonDBUtil.USERS_COLLECTION_NAME, "$'email' == 'djiang1@student.gsu.edu' and $'_id' == 'dddac36a-ac7b-4d85-beb5-bc5c94b3a888'");

	         for  (int x = 0; x < res.length(); x++) {
	            BSONObj bhero = res.get(x);
	            System.out.println("User fname: " + bhero.getString("_id"));
	         }
	         BSONObj obj = res.get(0);
	         BSONArrayObj receipts = obj.getBSONArray(djonDBUtil.USER_RECEIPTS_FIELDNAME);
	         for(int i=0; i<receipts.length(); ++i)
	         {
	        	 BSONObj receipt = receipts.get(i);
	        	 System.out.println(receipt.toChar());
	         }
	         BSONObj receipt = new BSONObj();
				receipt.add(djonDBUtil.USER_RECEIPT_ID_FIELDNAME, receipts.length());
				receipt.add(djonDBUtil.USER_RECEIPT_MERCHANT_FIELDNAME, "Walmart");
				receipt.add(djonDBUtil.USER_RECEIPT_PURCHASEDATE_FIELDNAME, "2012-01-05");
				receipt.add(djonDBUtil.USER_RECEIPT_CARD_FIELDNAME, "20001001003");
				receipt.add(djonDBUtil.USER_RECEIPT_ITEMS_FIELDNAME, new BSONArrayObj());
				receipt.add(djonDBUtil.USER_RECEIPT_IMG_FIELDNAME, "./receiptsImgs/no_photo.jpg");
				receipt.add(djonDBUtil.USER_RECEIPT_TOTAL_FIELDNAME, 0.0);		
				receipts.add(receipt);
		         c.update(djonDBUtil.DBNAME, djonDBUtil.USERS_COLLECTION_NAME, obj);
		         
		         System.out.println("-------------------------------------------------");		         
		         obj = res.get(0);
		         receipts = obj.getBSONArray(djonDBUtil.USER_RECEIPTS_FIELDNAME);
		         for(int i=0; i<receipts.length(); ++i)
		         {
		        	 receipt = receipts.get(i);
		        	 System.out.println(receipt.toChar());
		         }
	         //obj.add("cards", new BSONArrayObj());
	         /*BSONArrayObj arr = obj.getBSONArray("cards");
	         BSONObj o1 = new BSONObj();
	         o1.add("number", "1234-4321-5674-9087");
	         arr.add(o1);
	         BSONObj o2 = new BSONObj();
	         o2.add("number", "3453-4444-2346-5622-9754");
	         arr.add(o2);*/
	         //obj.add("cards", arr);
	         //BSONArrayObj arr = obj.getBSONArray("cards");
	         //System.out.println(arr.get(0).getString("_id"));
	        /* System.out.println(obj.getString("_id"));
	         c.update(djonDBUtil.DBNAME, djonDBUtil.USERS_COLLECTION_NAME, obj);
	         for  (int x = 0; x < res.length(); x++) {
		            BSONObj bhero = res.get(x);
		            System.out.println("cards :" + bhero.getBSONArray("cards").toChar());
		     }*/
	         
	         //c.dropNamespace(djonDBUtil.DBNAME, djonDBUtil.USERS_COLLECTION_NAME);
	         //BSONArrayObj receipts = new BSONArrayObj();
	        /* BSONArrayObj items = new BSONArrayObj();
	         
	         BSONObj item = new BSONObj();
	         item.add(djonDBUtil.USER_ITEM_NAME_FIELDNAME, "Orange");
	         item.add(djonDBUtil.USER_ITEM_QTY_FIELDNAME, 2.3);
	         item.add(djonDBUtil.USER_ITEM_PRICE_FIELDNAME, 1.79);
	         item.add(djonDBUtil.USER_ITEM_TAXRATE_FIELDNAME, "0.02");
	         items.add(item);
	         
	         item = new BSONObj();
	         item.add(djonDBUtil.USER_ITEM_NAME_FIELDNAME, "Apple");
	         item.add(djonDBUtil.USER_ITEM_QTY_FIELDNAME, 4.1);
	         item.add(djonDBUtil.USER_ITEM_PRICE_FIELDNAME, 1.99);
	         item.add(djonDBUtil.USER_ITEM_TAXRATE_FIELDNAME, "0.02");
	         items.add(item);
	         
	         BSONObj receipt = new BSONObj();
	         receipt.add(djonDBUtil.USER_RECEIPT_ID_FIELDNAME, 0);
	         receipt.add(djonDBUtil.USER_RECEIPT_CARD_FIELDNAME, "1234-4321-5674-9087");
	         receipt.add(djonDBUtil.USER_RECEIPT_ITEMS_FIELDNAME, items);
	         receipt.add(djonDBUtil.USER_RECEIPT_MERCHANT_FIELDNAME, "Walmart");
	         receipt.add(djonDBUtil.USER_RECEIPT_PURCHASEDATE_FIELDNAME, "2013-02-23");
	         receipt.add(djonDBUtil.USER_RECEIPT_IMG_FIELDNAME, GlobalInfo.defaultImg);
	         receipt.add(djonDBUtil.USER_RECEIPT_TOTAL_FIELDNAME, 12.52);
	         
	         receipts.add(receipt);*/
	         
	         /*obj.add(djonDBUtil.USER_RECEIPTS_FIELDNAME, djonDBUtil.getReceipts("song.x.guo@oracle.com"));
	         c.update(djonDBUtil.DBNAME, djonDBUtil.USERS_COLLECTION_NAME, obj);
	         res = c.find(djonDBUtil.DBNAME, djonDBUtil.USERS_COLLECTION_NAME, "$'fname' == 'Dan'");
	         obj = res.get(0);*/
	         //System.out.println(obj.toChar());
	         /*BSONArrayObj merchants = djonDBUtil.getMerchants();
	         for(int i=0; i<merchants.length(); ++i)
	         {
	        	 BSONObj merchant = merchants.get(i);
	        	 c.insert(djonDBUtil.DBNAME, djonDBUtil.MERCHANTS_COLLECTION_NAME, merchant);
	         }*/
	         /*BSONArrayObj merchants = c.find(djonDBUtil.DBNAME, djonDBUtil.MERCHANTS_COLLECTION_NAME);
	         System.out.println(merchants.toChar());*/
	         DjondbConnectionManager.releaseConnection(c);
	         }
	      } catch (LibraryException e) {
	         e.printStackTrace();
	      }
	   }
}
