package uk.ac.ebi.quickgo.ontology.service.search;

import uk.ac.ebi.quickgo.ontology.common.OntologyRepoConfig;
import uk.ac.ebi.quickgo.ontology.model.OBOTerm;
import uk.ac.ebi.quickgo.ontology.service.converter.ECODocConverter;
import uk.ac.ebi.quickgo.ontology.service.converter.GODocConverter;
import uk.ac.ebi.quickgo.rest.search.RequestRetrieval;
import uk.ac.ebi.quickgo.rest.search.SearchService;
import uk.ac.ebi.quickgo.rest.search.query.QueryRequestConverter;
import uk.ac.ebi.quickgo.rest.search.solr.SolrQueryConverter;
import uk.ac.ebi.quickgo.rest.search.solr.SolrRequestRetrieval;
import uk.ac.ebi.quickgo.rest.search.solr.SolrRetrievalConfig;
import uk.ac.ebi.quickgo.rest.service.ServiceRetrievalConfig;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.solr.core.SolrTemplate;

import static uk.ac.ebi.quickgo.rest.search.solr.SolrRetrievalConfigHelper.DEFAULT_HIGHLIGHT_DELIMS;
import static uk.ac.ebi.quickgo.rest.search.solr.SolrRetrievalConfigHelper.HIGHLIGHT_END_DELIM_INDEX;
import static uk.ac.ebi.quickgo.rest.search.solr.SolrRetrievalConfigHelper.HIGHLIGHT_START_DELIM_INDEX;
import static uk.ac.ebi.quickgo.rest.search.solr.SolrRetrievalConfigHelper.convertHighlightDelims;
import static uk.ac.ebi.quickgo.rest.service.ServiceRetrievalConfigHelper.extractFieldMappings;

@Configuration
@Import({OntologyRepoConfig.class})
@ComponentScan({"uk.ac.ebi.quickgo.ontology.service.search"})
@PropertySource("classpath:search.properties")
public class SearchServiceConfig {
    public static final String SOLR_ONTOLOGY_QUERY_REQUEST_HANDLER = "/search";

    private static final Logger LOGGER = LoggerFactory.getLogger(SearchServiceConfig.class);
    private static final String COMMA = ",";
    private static final String DEFAULT_ONTOLOGY_SEARCH_RETURN_FIELDS = "id,name,ontologyType";

    @Bean
    public SearchService<OBOTerm> ontologySearchService(RequestRetrieval<OBOTerm> ontologySolrRequestRetrieval) {
        return new SearchServiceImpl(ontologySolrRequestRetrieval);
    }

    @Bean
    public RequestRetrieval<OBOTerm> ontologySolrRequestRetrieval(
            SolrTemplate ontologyTemplate,
            QueryRequestConverter<SolrQuery> solrSelectQueryRequestConverter,
            OntologyCompositeRetrievalConfig ontologyRetrievalConfig) {

        SolrQueryResultConverter resultConverter = new SolrQueryResultConverter(
                new DocumentObjectBinder(),
                new GODocConverter(),
                new ECODocConverter(),
                ontologyRetrievalConfig.repo2DomainFieldMap()
        );

        return new SolrRequestRetrieval<>(
                ontologyTemplate.getSolrClient(),
                solrSelectQueryRequestConverter,
                resultConverter,
                ontologyRetrievalConfig);
    }

    @Bean
    public QueryRequestConverter<SolrQuery> ontologySolrQueryRequestConverter() {
        return new SolrQueryConverter(SOLR_ONTOLOGY_QUERY_REQUEST_HANDLER);
    }

    @Bean
    public OntologyCompositeRetrievalConfig ontologyRetrievalConfig(
            @Value("${search.return.fields:" + DEFAULT_ONTOLOGY_SEARCH_RETURN_FIELDS + "}") String ontologySearchSolrReturnedFields,
            @Value("${search.field.repo2domain.map:}") String ontologySearchRepo2DomainFieldMap,
            @Value("${search.highlight.delims:" + DEFAULT_HIGHLIGHT_DELIMS + "}") String highlightDelims) {
        String[] highlightDelimsArr = convertHighlightDelims(highlightDelims, COMMA);

        return new OntologyCompositeRetrievalConfig() {

            @Override public Map<String, String> repo2DomainFieldMap() {
                return extractFieldMappings(ontologySearchRepo2DomainFieldMap, COMMA);
            }

            @Override public List<String> getSearchReturnedFields() {
                return Arrays.asList(ontologySearchSolrReturnedFields.split(COMMA));
            }

            @Override public String getHighlightStartDelim() {
                return highlightDelimsArr[HIGHLIGHT_START_DELIM_INDEX];
            }

            @Override public String getHighlightEndDelim() {
                return highlightDelimsArr[HIGHLIGHT_END_DELIM_INDEX];
            }
        };
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    public interface OntologyCompositeRetrievalConfig extends SolrRetrievalConfig, ServiceRetrievalConfig {}
}
@@ -734,22 +734,17 @@
 va.util.
-Arrays
+*
 ;%0Aimport
@@ -754,20 +754,33 @@
 va.util.
-Li
 st
+ream.Collectors
 ;%0Aimport
@@ -790,19 +790,29 @@
 va.util.
-Map
+stream.Stream
 ;%0Aimport
@@ -920,65 +920,9 @@
 er;%0A
-import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A
+
 impo
@@ -1946,101 +1946,8 @@
 %22;%0A%0A
-    private static final Logger LOGGER = LoggerFactory.getLogger(SearchServiceConfig.class);%0A
     
@@ -2080,24 +2080,131 @@
 logyType%22;%0A%0A
+    @Value(%22$%7Bsearch.wildcard.compatible.fields:%7D%22)%0A    private String fieldsThatCanBeSearchedByWildCard;%0A%0A
     @Bean%0A  
@@ -3162,36 +3162,32 @@
     %7D%0A%0A    @Bean
-%0A   
  public QueryReq
@@ -3261,37 +3261,199 @@
     
-return new SolrQueryConverter
+Set%3CString%3E wildCardFields =%0A                Stream.of(fieldsThatCanBeSearchedByWildCard.split(COMMA)).collect(Collectors.toSet());%0A        return SolrQueryConverter.createWithWildCardSupport
 (SOL
@@ -3484,16 +3484,32 @@
 _HANDLER
+, wildCardFields
 );%0A    %7D
@@ -5010,9 +5010,10 @@
 nfig %7B%7D%0A
-
 %7D
+%0A

Add wildcard searches.