package com.usai.apex;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class SearchResult implements Serializable
{
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 8847830656864696392L;
	int total_count = -1;
	// int direction = 1;
	int offset = 0;
	// int count = 0;
	ArrayList<HashMap<String, String>> records = new ArrayList<HashMap<String, String>>();

	public int get_recordscount() {
		return records.size();
	}

	public HashMap<String, String> get_record(int i) {
		return records.get(i);
	}

	public void add_records(String jsonstr) {
		try {
			records.clear();
			JSONObject objrecords = new JSONObject(jsonstr);
			for (int i = 0; i < objrecords.length(); i++) {
				// offset++;
				JSONObject rec = objrecords.getJSONObject("record" + i);
				Iterator<?> it = rec.keys();
				HashMap<String, String> record = new HashMap<String, String>();
				while (it.hasNext()) // loop for each function
				{
					String field_name = (String) it.next();
					String val = rec.getString(field_name)
							.replace("\n", "");
					record.put(field_name, val);
				}
				records.add(record);

			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String TAG = "init_fields@ResultActivity.SearchResult";
		Log.d(TAG, jsonstr);
	}

	public int get_totalcount() {
		return total_count;
	}

	public void put_totalcount(int c) {
		total_count = c;
	}

	public int get_offset() {
		return offset;
	}


	public void set_offset(int i) {
		offset = i;

	}
}
