package ca.carleton.gcrc.couch.client.impl;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import ca.carleton.gcrc.couch.client.CouchQueryResults;

public class CouchQueryResultsImpl implements CouchQueryResults {

	private JSONObject top;
	private int total = 0;
	private int offset = 0;
	private List<JSONObject> rows = null;

	public CouchQueryResultsImpl(JSONObject top) throws Exception {
		this.top = top;
		
		if( top.containsKey("total") ) {
			total = top.getInt("total");
		}
		if( top.containsKey("offset") ) {
			offset = top.getInt("offset");
		}
		
		JSONArray rowArr = top.getJSONArray("rows");
		int size = rowArr.size();
		rows = new ArrayList<JSONObject>(size);
		for(int i=0; i<size; ++i) {
			rows.add( rowArr.getJSONObject(i) );
		}
	}
	
	@Override
	public JSONObject getFullResults() {
		return top;
	}
	
	@Override
	public int getTotal() {
		return total;
	}

	@Override
	public int getOffset() {
		return offset;
	}

	@Override
	public List<JSONObject> getRows() {
		return rows;
	}

	@Override
	public List<JSONObject> getValues() {
		List<JSONObject> result = new ArrayList<JSONObject>(rows.size());
		for(JSONObject row : rows) {
			JSONObject v = row.optJSONObject("value");
			if( null != v ) {
				result.add(v);
			}
		}
		return result;
	}
}
