package mx.nic.rdap.server.result;

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

import mx.nic.rdap.core.catalog.RemarkType;
import mx.nic.rdap.core.db.Entity;
import mx.nic.rdap.core.db.Remark;
import mx.nic.rdap.db.struct.SearchResultStruct;
import mx.nic.rdap.renderer.object.SearchResponse;

/**
 * A result from an Entity search request.
 */
public class EntitySearchResult extends RdapSearchResult {

	private List<Entity> entities;
	// The max number of results allowed for the user
	private Integer maxNumberOfResultsForUser;
	// Indicate is the search has more results than the answered to the user
	private Boolean resultSetWasLimitedByUserConfiguration;
	
	public EntitySearchResult(String header, String contextPath, SearchResultStruct<Entity> result, String userName) {
		notices = new ArrayList<Remark>();
		this.entities = new ArrayList<Entity>();
		this.setMaxNumberOfResultsForUser(result.getSearchResultsLimitForUser());
		this.resultSetWasLimitedByUserConfiguration = result.getResultSetWasLimitedByUserConfiguration();
		for (Entity entity : result.getResults()) {
			EntityResult.addSelfLinks(header, contextPath, entity);
			this.entities.add(entity);
		}
		setRdapObjects(entities);
		validateResponse();
		fillNotices();
		
		setResultType(ResultType.ENTITIES);
		SearchResponse<Entity> searchResponse = new SearchResponse<>();
		searchResponse.setNotices(getNotices());
		searchResponse.setRdapObjects(getEntities());
		searchResponse.setRdapConformance(new ArrayList<>());
		searchResponse.getRdapConformance().add("rdap_level_0");
		
		setEntities(entities);
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see mx.nic.rdap.server.RdapResult#fillNotices()
	 */
	@Override
	public void fillNotices() {
		// At the moment, we only add the privacy notice
		if (entities.size() < maxNumberOfResultsForUser && resultSetWasLimitedByUserConfiguration) {
			notices.add(new Remark(RemarkType.RESULT_SET_UNEXPLAINABLE));
		} else if (entities.size() == maxNumberOfResultsForUser) {
			notices.add(new Remark(RemarkType.RESULT_SET_AUTHORIZATION));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see mx.nic.rdap.server.RdapResult#validateResponse()
	 */
	@Override
	public void validateResponse() {
		// Nothing to validate
	}

	public List<Entity> getEntities() {
		return entities;
	}

	public void setEntities(List<Entity> entities) {
		this.entities = entities;
	}

	public Integer getMaxNumberOfResultsForUser() {
		return maxNumberOfResultsForUser;
	}

	public void setMaxNumberOfResultsForUser(Integer maxNumberOfResultsForUser) {
		this.maxNumberOfResultsForUser = maxNumberOfResultsForUser;
	}

	public Boolean getResultSetWasLimitedByUserConfiguration() {
		return resultSetWasLimitedByUserConfiguration;
	}

	public void setResultSetWasLimitedByUserConfiguration(Boolean resultSetWasLimitedByUserConfiguration) {
		this.resultSetWasLimitedByUserConfiguration = resultSetWasLimitedByUserConfiguration;
	}
	
}

@@ -1184,36 +1184,8 @@
 %7D%0A%09%09
-setRdapObjects(entities);%0A%09%09
 vali
@@ -1212,20 +1212,70 @@
 llNo
+tices();%0A%09%09setRdapObjects(entities);%0A%09%09setEntities(enti
 ti
-c
 es
-(
 );%0A
-%09%09%0A
 %09%09se
@@ -1308,16 +1308,17 @@
 ITIES);%0A
+%0A
 %09%09Search
@@ -1585,35 +1585,47 @@
 %22);%0A
-%09%09
 %0A%09%09set
-Entities(entities);
+RdapResponse(searchResponse);%0A%09%09%0A
 %0A%09%7D%0A

Fix #73