package uk.ac.ebi.biosamples.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.hateoas.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriUtils;
import uk.ac.ebi.biosamples.model.facets.FacetFactory;
import uk.ac.ebi.biosamples.model.facets.LabelCountEntry;
import uk.ac.ebi.biosamples.model.facets.StringListFacet;
import uk.ac.ebi.biosamples.solr.service.SolrSampleService;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FacetService {

	private Logger log = LoggerFactory.getLogger(getClass());
	
	private final SolrSampleService solrSampleService;

	public FacetService(SolrSampleService solrSampleService) {
		this.solrSampleService = solrSampleService;
	}


	public List<Resource<StringListFacet>> getFacets(String text, MultiValueMap<String,String> filters, int noOfFacets, int noOfFacetValues) {
		Pageable facetPageable = new PageRequest(0,noOfFacets);
		Pageable facetValuePageable = new PageRequest(0,noOfFacetValues);
		//TODO if a facet is enabled as a filter, then that value will be the only filter displayed
		//TODO allow update date range
		List<StringListFacet> solrFacets = solrSampleService.getFacets(text, filters, null, null, facetPageable, facetValuePageable);


		List<String> tempFilters = new ArrayList<>();
		for(String key: filters.keySet()) {
			filters.get(key).forEach(value -> tempFilters.add(key + ":" + value));
		}
		String[] filtersArray = tempFilters.toArray(new String[tempFilters.size()]);


		// Need to return a resource for each facet created from solr
		List<Resource<StringListFacet>> resourceFacets = new ArrayList<>();
		for(StringListFacet facet: solrFacets) {
			FacetResourceAssembler.LabelFilterResourceAssembler resourceAssembler =
					new FacetResourceAssembler.LabelFilterResourceAssembler(text, null, null, filtersArray, facet);
			FacetResourceAssembler.StringListFacetResourceAssembler facetResourceAssembler =
					new FacetResourceAssembler.StringListFacetResourceAssembler(text, null, null, filtersArray, facet);
			List<LabelCountEntry> content = (List<LabelCountEntry>) facet.getContent();
			List<Resource<LabelCountEntry>> resourceContent =
					content.stream().map(resourceAssembler::toResource).collect(Collectors.toList());
			StringListFacet newFacet = FacetFactory.buildStringList(facet.getType(), facet.getLabel(), facet.getCount(), resourceContent);
			resourceFacets.add(facetResourceAssembler.toResource(newFacet));
		}
		return resourceFacets;
	}

	private String encodeParam(String queryParam) {
		try {
			return UriUtils.encodeQueryParam(queryParam, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
	}
}

@@ -1566,34 +1566,40 @@
  ArrayList%3C%3E();%0A
-%09%09
+        
 for(String key: 
@@ -1618,19 +1618,28 @@
 et()) %7B%0A
-%09%09%09
+            
 filters.
@@ -1668,47 +1668,212 @@
  -%3E 
-tempFilters.add(key + %22:%22 + value));%0A%09%09
+%7B%0A                if (value != null) %7B%0A                    tempFilters.add(key + %22:%22 + value);%0A                %7D else %7B%0A                    tempFilters.add(key);%0A                %7D%0A            %7D);%0A        
 %7D%0A%09%09

Update the way the general facet filter is serialized