package com.swiftype.api.easy.helper;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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


public class SearchResult {
	private final Info info;
	private final List<Record> results;

	private SearchResult(final Info info, final List<Record> results) {
		this.info = info;
		this.results = results;
	}

	public Info getInfo() {
		return info;
	}

	public List<Record> getResults() {
		return results;
	}

	public static Map<String, SearchResult> fromJson(final JSONObject json) {
		final Map<String, SearchResult> searchResultsPerDocumentType = new HashMap<String, SearchResult>();
		try {
			final JSONObject infoJson = json.getJSONObject("info");
			final JSONObject recordsJson = json.getJSONObject("records");

			for (@SuppressWarnings("unchecked") final Iterator<String> names = infoJson.keys(); names.hasNext();){
				final String documentTypeName = names.next();
				final List<Record> records = Record.fromJsonArray(recordsJson.getJSONArray(documentTypeName));
				final SearchResult result = new SearchResult(Info.fromJson(infoJson.getJSONObject(documentTypeName)), records);
				searchResultsPerDocumentType.put(documentTypeName, result);
			}
		} catch (JSONException e) {
			return null;
		}
		return searchResultsPerDocumentType;
	}

	public static class Info {
		public final String query;
		public final int totalResultCount;
		public final int pageCount;
		public final int page;
		public final int perPage;

		private Info(final String query,
					final int totalResultCount,
					final int pageCount,
					final int page,
					final int perPage) {
			this.query = query;
			this.totalResultCount = totalResultCount;
			this.pageCount = pageCount;
			this.page = page;
			this.perPage = perPage;
		}

		public static Info fromJson(final JSONObject json) {
			final String query = json.optString("query");
			final int totalResultCount = json.optInt("total_result_count");
			final int pageCount = json.optInt("num_pages");
			final int page = json.optInt("current_page");
			final int perPage = json.optInt("per_page");
			return new Info(query, totalResultCount, pageCount, page, perPage);
		}
	}
}

@@ -1524,16 +1524,50 @@
 perPage;
+%0A%09%09public final JSONObject facets;
 %0A%0A%09%09priv
@@ -1697,16 +1697,46 @@
  perPage
+,%0A%09%09%09%09%09final JSONObject facets
 ) %7B%0A%09%09%09t
@@ -1878,16 +1878,41 @@
 erPage;%0A
+%09%09%09this.facets = facets;%0A
 %09%09%7D%0A%0A%09%09p
@@ -2227,16 +2227,75 @@
 page%22);%0A
+%09%09%09final JSONObject facets = json.optJSONObject(%22facets%22);%0A
 %09%09%09retur
@@ -2354,16 +2354,24 @@
  perPage
+, facets
 );%0A%09%09%7D%0A%09

add facets to info in search results