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

import uk.ac.ebi.quickgo.annotation.common.document.AnnotationFields;
import uk.ac.ebi.quickgo.annotation.validation.AnnotationValidation;
import uk.ac.ebi.quickgo.rest.search.query.FilterProvider;
import uk.ac.ebi.quickgo.rest.search.query.PrototypeFilter;
import uk.ac.ebi.quickgo.rest.search.query.Validator;

import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.stream.Stream;
import javax.validation.constraints.Pattern;
import org.springframework.validation.Errors;

import static com.google.common.base.Strings.isNullOrEmpty;

/**
 * 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 (PrototypeFilter) 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 AnnotationFilter implements FilterProvider {

    public static final int DEFAULT_ENTRIES_PER_PAGE = 25;
    private static final int DEFAULT_PAGE_NUMBER = 1;

    //Non-data parameters
    private int  limit = DEFAULT_ENTRIES_PER_PAGE;
    private int page = DEFAULT_PAGE_NUMBER;

    private final List<PrototypeFilter> prototypeFilters = new ArrayList<>();

    @Pattern(regexp = "^[A-Za-z][A-Za-z-_]+(,[A-Za-z][A-Za-z-_]+[,]*)*")
    private String assignedBy;

    /**
     *  E.g. ASPGD,Agbase,..
     *  In the format assignedBy=ASPGD,Agbase
     */
    public void setAssignedBy(String assignedBy) {

        this.assignedBy = assignedBy;

        if (!isNullOrEmpty(assignedBy)) {

            final Validator<String> validator = (String s) -> {};   //todo needs to be removed.

            final PrototypeFilter pFilter = PrototypeFilter.create(AnnotationFields.ASSIGNED_BY, assignedBy, validator);
            prototypeFilters.add(pFilter);
        }

    }



    public void setPage(int page) {
        Preconditions.checkArgument(page>-1, "The value for page cannot be negative");
        this.page = page;
    }

    public void setLimit(int limit) {
        Preconditions.checkArgument(limit>-1, "The value for limit cannot be negative");
        this.limit = limit;
    }

    public int getLimit() {
        return limit;
    }

    public int getPage() {
        return page;
    }

    /**
     * Provide a stream of the prototype filters.
     * @return
     */
    public Stream<PrototypeFilter> stream() {
        return prototypeFilters.stream();
    }

}

@@ -112,77 +112,8 @@
 ds;%0A
-import uk.ac.ebi.quickgo.annotation.validation.AnnotationValidation;%0A
 impo
@@ -286,53 +286,8 @@
 r;%0A%0A
-import com.google.common.base.Preconditions;%0A
 impo
@@ -337,40 +337,8 @@
 st;%0A
-import java.util.regex.Matcher;%0A
 impo
@@ -401,22 +401,18 @@
 traints.
-Patter
+Mi
 n;%0Aimpor
@@ -417,45 +417,44 @@
 ort 
-org.springframework.validation.Errors
+javax.validation.constraints.Pattern
 ;%0A%0Ai
@@ -1077,16 +1077,28 @@
 ameters%0A
+    @Min(0)%0A
     priv
@@ -1139,16 +1139,29 @@
 ER_PAGE;
+%0A%0A    @Min(1)
 %0A    pri
@@ -1887,17 +1887,16 @@
     %7D%0A%0A%0A
-%0A
     publ
@@ -1935,246 +1935,70 @@
     
-Preconditions.checkArgument(page%3E-1, %22The value for page cannot be negative%22);%0A        this.page = page;%0A    %7D%0A%0A    public void setLimit(int limit) %7B%0A        Preconditions.checkArgument(limit%3E-1, %22The value for limit cannot be negative%22);
+this.page = page;%0A    %7D%0A%0A    public void setLimit(int limit) %7B
 %0A   

Improve validation for page and limit.