package uk.ac.ebi.quickgo.annotation.search;

import uk.ac.ebi.quickgo.annotation.common.document.AnnotationFields;
import uk.ac.ebi.quickgo.annotation.model.AnnotationFilter;
import uk.ac.ebi.quickgo.rest.search.SearchQueryRequestBuilder;
import uk.ac.ebi.quickgo.rest.search.query.QueryRequest;
import uk.ac.ebi.quickgo.rest.search.query.QuickGOQuery;

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

/**
 *
 * Records common configuration details required to create a {@link QueryRequest} instance,
 * and provides a {@link SearchQueryRequestBuilder} instance that can be used to build
 * specialised forms of the query.
 * <p>
 * The purpose of this class is to encapsulate and simplify the creation of search queries.
 * <p>
 * More specifically, this class wraps {@link uk.ac.ebi.quickgo.rest.search.query.QueryRequest.Builder}
 * and performs boiler-plate configuration tasks which all callers
 * of a search require. These tasks include, setting up field projections,
 * highlighting fields, validating facets and filters, etc.
 *
 * Based on {@link uk.ac.ebi.quickgo.rest.search.DefaultSearchQueryTemplate}
 * @author Tony Wardell
 * Date: 26/04/2016
 * Time: 10:05
 * Created with IntelliJ IDEA.
 */
public class AnnotationSearchQueryTemplate {

    public static final int DEFAULT_PAGE_SIZE = 25;
    public static final int DEFAULT_PAGE_NUMBER = 1;
    private final Iterable<String> returnedFields;


    public AnnotationSearchQueryTemplate(
            Iterable<String> returnedFields) {
        this.returnedFields = returnedFields;

    }

    public Builder newBuilder() {
        return new Builder(returnedFields);
    }


    public static class Builder implements SearchQueryRequestBuilder {
        private final Iterable<String> returnedFields;
        private AnnotationFilter annotationFilter;
        private int page = DEFAULT_PAGE_NUMBER;
        private int pageSize = DEFAULT_PAGE_SIZE;
        private List<String> filterQueries;

        private Builder(Iterable<String> returnedFields) {
            this.returnedFields = returnedFields;
            this.filterQueries = new ArrayList<>();
        }


        /**
         *
         * @param annotationFilter
         * @return
         */
        public AnnotationSearchQueryTemplate.Builder addAnnotationFilter(AnnotationFilter annotationFilter) {
            this.annotationFilter = annotationFilter;
            return this;
        }



        /**
         * Specify the number of results to be returned per page, i.e., page size.
         *
         * @param pageSize the page size.
         * @return this {@link AnnotationSearchQueryTemplate.Builder} instance
         */
        public AnnotationSearchQueryTemplate.Builder setPageSize(int pageSize) {
            this.pageSize = pageSize;
            return this;
        }

        /**
         * Specify which page of results to return.
         *
         * @param page the page of results to return.
         * @return this {@link AnnotationSearchQueryTemplate.Builder} instance
         */
        public AnnotationSearchQueryTemplate.Builder setPage(int page) {
            this.page = page;
            return this;
        }

        /**
         * Create QueryRequest which is an aggregation of the complete query to sent to Solr
         * Will not use query, only filterQueries,
         * @return
         */
        @Override public QueryRequest build() {
            QueryRequest.Builder builder = new QueryRequest.Builder(QuickGOQuery.createEmptyQuery());
            builder.setPageParameters(page, pageSize);

            //Add all filters to builder here.. todo others to follow
            addFilterToBuilder(builder, annotationFilter.getAssignedby(),  AnnotationFields.ASSIGNED_BY);
            addFilterToBuilder(builder, annotationFilter.getQualifier(),  AnnotationFields.QUALIFIER);

            returnedFields
                    .forEach(builder::addProjectedField);

            return builder.build();
        }

        private void addFilterToBuilder(QueryRequest.Builder builder, List<String> filterArgs, String solrField) {
            if(filterArgs==null){
                return;
            }
            QuickGOQuery assignedByQuery = filterArgs
                .parallelStream()
                .reduce(null, (q, arg) -> QuickGOQuery.createQuery( solrField, arg), (q1,q2) -> q1.or(q2)) ;

            if(assignedByQuery!=null) {
                builder.addQueryFilter(assignedByQuery);
            }
        }
    }

}

@@ -1255,114 +1255,8 @@
 te %7B
-%0A%0A    public static final int DEFAULT_PAGE_SIZE = 25;%0A    public static final int DEFAULT_PAGE_NUMBER = 1;
 %0A   
@@ -1714,150 +1714,8 @@
 ter;
-%0A        private int page = DEFAULT_PAGE_NUMBER;%0A        private int pageSize = DEFAULT_PAGE_SIZE;%0A        private List%3CString%3E filterQueries;
 %0A%0A  
@@ -1825,60 +1825,8 @@
 ds;%0A
-            this.filterQueries = new ArrayList%3C%3E();%0A
     
@@ -2123,17 +2123,18 @@
     %7D%0A%0A%0A
-%0A
+//
         
@@ -2129,32 +2129,34 @@
 %0A%0A//        /**%0A
+//
          * Speci
@@ -2214,32 +2214,34 @@
 .e., page size.%0A
+//
          *%0A     
@@ -2227,32 +2227,34 @@
 e.%0A//         *%0A
+//
          * @para
@@ -2279,16 +2279,18 @@
 e size.%0A
+//
         
@@ -2352,32 +2352,34 @@
 ilder%7D instance%0A
+//
          */%0A    
@@ -2366,32 +2366,34 @@
 e%0A//         */%0A
+//
         public A
@@ -2453,24 +2453,26 @@
 pageSize) %7B%0A
+//
             
@@ -2497,16 +2497,18 @@
 geSize;%0A
+//
         
@@ -2516,32 +2516,34 @@
    return this;%0A
+//
         %7D%0A%0A     
@@ -2528,33 +2528,37 @@
 is;%0A//        %7D%0A
-%0A
+//%0A//
         /**%0A    
@@ -2545,32 +2545,34 @@
 /%0A//        /**%0A
+//
          * Speci
@@ -2599,32 +2599,34 @@
 ults to return.%0A
+//
          *%0A     
@@ -2612,32 +2612,34 @@
 n.%0A//         *%0A
+//
          * @para
@@ -2676,16 +2676,18 @@
 return.%0A
+//
         
@@ -2757,16 +2757,18 @@
 nstance%0A
+//
         
@@ -2763,32 +2763,34 @@
 e%0A//         */%0A
+//
         public A
@@ -2842,24 +2842,26 @@
 int page) %7B%0A
+//
             
@@ -2878,16 +2878,18 @@
 = page;%0A
+//
         
@@ -2897,32 +2897,34 @@
    return this;%0A
+//
         %7D%0A%0A     
@@ -3297,22 +3297,118 @@
 ers(
-page, pageSize
+Integer.valueOf(annotationFilter.getPage()), Integer.valueOf(annotationFilter%0A                    .getLimit())
 );%0A%0A

Fix operation of page number and page size.