package de.tblsoft.search.pipeline.filter.solr;

import com.google.common.base.Strings;
import de.tblsoft.search.response.Document;
import de.tblsoft.search.response.Facet;
import de.tblsoft.search.response.FacetValue;
import de.tblsoft.search.response.SearchResult;
import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;

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

/**
 * Created by tblsoft on 11.11.17.
 */
public class Solr2SearchResultMappingTransformer implements SearchResultTransformerIF {

    private Map<String, String> fieldMapping = new HashMap<>();
    private Map<String, String> facetMapping = new HashMap<>();
    private Map<String, String> facetNameMapping = new HashMap<>();

    public Solr2SearchResultMappingTransformer() {


    }

    public SearchResult transform(QueryResponse queryResponse) {
        SearchResult result = new SearchResult();
        result.setTotal(queryResponse.getResults().getNumFound());
        result.setMaxScore(queryResponse.getResults().getMaxScore());
        Iterator<SolrDocument> solrDocumentIterator = queryResponse.getResults().iterator();
        while(solrDocumentIterator.hasNext()) {
            SolrDocument solrDocument = solrDocumentIterator.next();
            Document document = transformDocument(solrDocument);
            result.getDocuments().add(document);
        }

        mapFacets(queryResponse, result);


        return result;
    }

    protected void mapFacets(QueryResponse queryResponse, SearchResult searchResult) {
        if(queryResponse.getFacetFields() == null) {
            return;
        }
        searchResult.setFacetCount(queryResponse.getFacetFields().size());
        for(FacetField facetField : queryResponse.getFacetFields()) {
            Facet facet = new Facet();

            String id = facetMapping.get(facetField.getName());
            facet.setId(id);

            String name = facetNameMapping.get(id);
            facet.setName(name);

            facet.setCount(Long.valueOf(facetField.getValues().size()));
            Long facetReseultCount = 0L;
            for(FacetField.Count count: facetField.getValues()) {
                FacetValue facetValue = new FacetValue(count.getName(), count.getCount());
                facetReseultCount = facetReseultCount + facetValue.getCount();

                // TODO encode the value
                facetValue.setFilter(id + "=" + count.getName());
                facet.getValues().add(facetValue);
            }
            facet.setResultCount(facetReseultCount);
            searchResult.getFacets().add(facet);
        }
    }

    public Document transformDocument(SolrDocument solrDocument) {
        Document document = new Document();
        for(String fieldNames: solrDocument.getFieldNames()) {
            transformField(document, fieldNames,solrDocument.get(fieldNames));


        }
        return document;

    }

    public void transformField(Document document, String name, Object value) {
        String mappedName = fieldMapping.get(name);
        if(!Strings.isNullOrEmpty(mappedName)) {
            document.getDocument().put(mappedName, value);
        }
    }

    public void setFieldMapping(Map<String, String> fieldMapping) {
        this.fieldMapping = fieldMapping;
    }

    public void setFacetMapping(Map<String, String> facetMapping) {
        this.facetMapping = facetMapping;
    }

    public void setFacetNameMapping(Map<String, String> facetNameMapping) {
        this.facetNameMapping = facetNameMapping;
    }

    public void addFieldMapping(String from, String to) {
        fieldMapping.put(from, to);
    }
    public void addFacetMapping(String from, String to) {
        facetMapping.put(from, to);
    }

    public void addFacetNameMapping(String from, String to) {
        facetNameMapping.put(from, to);
    }
}

@@ -443,64 +443,9 @@
 til.
-HashMap;%0Aimport java.util.Iterator;%0Aimport java.util.Map
+*
 ;%0A%0A/
@@ -591,39 +591,45 @@
 ate Map%3CString, 
+List%3C
 String%3E
+%3E
  fieldMapping = 
@@ -3061,22 +3061,28 @@
         
+List%3C
 String
+%3E
  mappedN
@@ -3084,16 +3084,17 @@
 ppedName
+s
  = field
@@ -3122,16 +3122,132 @@
       if
+(mappedNames == null) %7B%0A            return;%0A        %7D%0A        for(String mappedName : mappedNames) %7B%0A            if 
 (!String
@@ -3273,24 +3273,28 @@
 pedName)) %7B%0A
+    
             
@@ -3336,24 +3336,38 @@
 me, value);%0A
+            %7D%0A
         %7D%0A  
@@ -3415,23 +3415,29 @@
 String, 
+List%3C
 String%3E
+%3E
  fieldMa
@@ -3794,32 +3794,194 @@
 m, String to) %7B%0A
+        List%3CString%3E mapping = fieldMapping.get(from);%0A        if(mapping == null) %7B%0A            mapping = new ArrayList%3C%3E();%0A        %7D%0A        mapping.add(to);%0A%0A
         fieldMap
@@ -3987,34 +3987,39 @@
 pping.put(from, 
-to
+mapping
 );%0A    %7D%0A    pub

allow mapping of muliple values