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

import uk.ac.ebi.quickgo.annotation.common.document.AnnotationFields;
import uk.ac.ebi.quickgo.common.validator.GeneProductIDList;
import uk.ac.ebi.quickgo.rest.search.filter.RequestFilter;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Pattern;

/**
 * A data structure for the annotation filtering parameters passed in from the client.
 *
 * Once the comma separated values have been set, then turn then into an object (SimpleFilter) that
 * encapsulates the list and solr field name to use for that argument.
 *
 * @author Tony Wardell
 * Date: 25/04/2016
 * Time: 11:23
 * Created with IntelliJ IDEA.
 */
public class AnnotationRequest {
    public static final int DEFAULT_ENTRIES_PER_PAGE = 25;
    public static final int MAX_ENTRIES_PER_PAGE = 100;

    private static final String COMMA = ",";
    private static final int DEFAULT_PAGE_NUMBER = 1;

    //Non-data parameters
    @Min(0)
    @Max(MAX_ENTRIES_PER_PAGE)
    private int limit = DEFAULT_ENTRIES_PER_PAGE;

    @Min(1)
    private int page = DEFAULT_PAGE_NUMBER;

    private final Map<String, String> filters = new HashMap<>();

    /**
     *  E.g. ASPGD,Agbase,..
     *  In the format assignedBy=ASPGD,Agbase
     */
    public void setAssignedBy(String assignedBy) {
        filters.put(AnnotationFields.ASSIGNED_BY, assignedBy);
    }

    @Pattern(regexp = "^[A-Za-z][A-Za-z\\-_]+(,[A-Za-z][A-Za-z\\-_]+)*")
    public String getAssignedBy() {
        return filters.get(AnnotationFields.ASSIGNED_BY);
    }

    //TODO:change the way the field is referenced
    private static final String ASPECT_FIELD = "aspect";
    private static final String GENE_PRODUCT_IDS_FIELD = "gpID";

    public void setAspect(String aspect) {
        if(aspect != null) {
            filters.put(ASPECT_FIELD, aspect.toLowerCase());
        }
    }

    @Pattern(regexp = "(?i)biological_process|molecular_function|cellular_component")
    public String getAspect() {
        return filters.get(ASPECT_FIELD);
    }

    /**
     * Gene Product IDs, in CSV format.
     */
    @GeneProductIDList
    public void setGpID(String listOfGeneProductIDs){
        if(listOfGeneProductIDs != null) {
            filters.put(GENE_PRODUCT_IDS_FIELD, listOfGeneProductIDs.toLowerCase());
        }
    }

    public String getGpID(){
        return filters.get(GENE_PRODUCT_IDS_FIELD);
    }



    public void setPage(int page) {
        this.page = page;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public int getLimit() {
        return limit;
    }

    public int getPage() {
        return page;
    }

    public Stream<RequestFilter> convertToFilters() {
        return filters.entrySet().stream().map(filter -> new RequestFilter(filter.getKey(),
                splitFilterValues(filter.getValue())));
    }

    private String[] splitFilterValues(String values) {
        return values.split(COMMA);
    }
}

@@ -112,69 +112,8 @@
 ds;%0A
-import uk.ac.ebi.quickgo.common.validator.GeneProductIDList;%0A
 impo
@@ -1727,74 +1727,8 @@
 t%22;%0A
-    private static final String GENE_PRODUCT_IDS_FIELD = %22gpID%22;%0A%0A
     
@@ -2100,16 +2100,18 @@
  */%0A    
+//
 @GenePro
@@ -2141,19 +2141,28 @@
 oid setG
-pID
+eneProductId
 (String 
@@ -2251,16 +2251,33 @@
 ers.put(
+AnnotationFields.
 GENE_PRO
@@ -2283,23 +2283,16 @@
 ODUCT_ID
-S_FIELD
 , listOf
@@ -2365,11 +2365,20 @@
 getG
-pID
+eneProductId
 ()%7B%0A
@@ -2404,16 +2404,33 @@
 ers.get(
+AnnotationFields.
 GENE_PRO
@@ -2440,15 +2440,8 @@
 T_ID
-S_FIELD
 );%0A 

Change the name of the setter for GeneProduct ids.