package ch.elexis.base.solr.spotlight;

import java.io.IOException;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.MapSolrParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.elexis.core.eenv.IElexisEnvironmentService;
import ch.elexis.core.spotlight.ISpotlightResult;
import ch.elexis.core.spotlight.ISpotlightResultContributor;

public abstract class AbstractSpotlightResultContributor implements ISpotlightResultContributor {
	
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	private HttpSolrClient client;
	private final String CORE;
	
	public AbstractSpotlightResultContributor(String core){
		this.CORE = core;
	}
	
	public void activate(IElexisEnvironmentService elexisEnvironmentService){
		client = new HttpSolrClient.Builder("https://ee.elexisdemo.ch/solr/").build();
	}
	
	public void deactivate(){
		if (client != null) {
			try {
				client.close();
			} catch (IOException e) {
				logger.warn("Error closing client", e);
			}
		}
	}
	
	@Override
	public void computeResult(List<String> stringTerms, List<LocalDate> dateTerms,
		List<Number> numericTerms, ISpotlightResult spotlightResult,
		Map<String, String> contextParameters){
		
		try {
			final Map<String, String> queryParamMap = new HashMap<String, String>();
			String searchString = stringTerms.stream().reduce((u, t) -> u + " AND text:" + t).get();
			queryParamMap.put("q", "text:" + searchString);
			queryParamMap.put("sort", "lastupdate desc");
			queryParamMap.put("rows", "5");
			MapSolrParams queryParams = new MapSolrParams(queryParamMap);
			
			QueryResponse response = client.query(CORE, queryParams);
			handleResponse(spotlightResult, response);
			
		} catch (SolrServerException | IOException e) {
			logger.warn("Error in client.query", e);
		}
		
	}
	
	protected abstract void handleResponse(ISpotlightResult spotlightResult,
		QueryResponse response);
	
}

@@ -609,16 +609,67 @@
 ributor;
+%0Aimport ch.elexis.core.spotlight.ISpotlightService;
 %0A%0Apublic
@@ -1507,16 +1507,64 @@
 %09%09try %7B%0A
+%09%09%09StringBuilder qString = new StringBuilder();%0A
 %09%09%09final
@@ -1638,29 +1638,271 @@
 %0A%09%09%09
-String searchString =
+%0A%09%09%09if (contextParameters != null) %7B%0A%09%09%09%09String patientId =%0A%09%09%09%09%09contextParameters.get(ISpotlightService.CONTEXT_FILTER_PATIENT_ID);%0A%09%09%09%09if (patientId != null) %7B%0A%09%09%09%09%09qString.append(%22patient_id:%22 + patientId + %22 AND %22);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09qString.append(%0A%09%09%09%09%22text:%22 +
  str
@@ -1963,16 +1963,17 @@
 t).get()
+)
 ;%0A%09%09%09que
@@ -1996,30 +1996,26 @@
 q%22, 
-%22text:%22 + search
+qString.to
 String
+()
 );%0A%09
@@ -2097,16 +2097,62 @@
 , %225%22);%0A
+%09%09%09%0A%09%09%09System.out.println(queryParamMap);%0A%09%09%09%0A
 %09%09%09MapSo

Allow limit search to patient context