package org.hisp.dhis.android.core.trackedentity;

import android.support.annotation.Nullable;

import java.util.HashSet;
import java.util.Set;

public class TeiQuery {

    private final int page;
    private final int pageSize;
    private final boolean paging;
    private final String orgUnit;
    private final int pageLimit;

    @Nullable
    private final Set<String> uIds;


    public TeiQuery(boolean paging, int page, int pageSize,
                    String orgUnit, int pageLimit) {
        this.paging = paging;
        this.page = page;
        this.pageSize = pageSize;
        this.orgUnit = orgUnit;
        this.pageLimit = pageLimit;
        uIds = null;
    }

    public TeiQuery(boolean paging, int page, int pageSize,
                    String orgUnit, @Nullable Set<String> uIds, int pageLimit) {
        this.paging = paging;
        this.page = page;
        this.pageSize = pageSize;
        this.orgUnit = orgUnit;
        this.uIds = uIds;
        this.pageLimit = pageLimit;
    }

    @Nullable
    public Set<String> getUIds() {
        return uIds;
    }

    public int getPage() {
        return page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public boolean isPaging() {
        return paging;
    }

    public String getOrgUnit() {
        return orgUnit;
    }

    public int getPageLimit() {
        return pageLimit;
    }

    public static class Builder {
        private int page = 1;
        private int pageSize = 50;
        private boolean paging;
        private String orgUnit;
        int pageLimit;

        private Set<String> uIds = new HashSet<>();

        private Builder() {
        }

        public static TeiQuery.Builder create() {
            return new TeiQuery.Builder();
        }

        public TeiQuery.Builder withPaging(boolean paging) {
            this.paging = paging;
            return this;
        }

        public TeiQuery.Builder withPage(int page) {
            this.page = page;
            return this;
        }

        public TeiQuery.Builder withPageSize(int pageSize) {
            this.pageSize = pageSize;
            return this;
        }

        public TeiQuery.Builder withOrgUnit(String orgUnit) {
            this.orgUnit = orgUnit;
            return this;
        }

        public TeiQuery.Builder withUIds(Set<String> uIds) {
            this.uIds = uIds;
            return this;
        }

        public TeiQuery.Builder withPageLimit(int pageLimit) {
            this.pageLimit = pageLimit;
            return this;
        }

        public TeiQuery build() {
            if (pageLimit > pageSize) {
                throw new IllegalArgumentException(
                        "pageLimit can not be more greater than pageSize");
            }

            return new TeiQuery(paging, page, pageSize,
                    orgUnit, uIds, pageLimit);
        }
    }
}

@@ -115,16 +115,39 @@
 ashSet;%0A
+import java.util.List;%0A
 import j
@@ -160,16 +160,16 @@
 il.Set;%0A
-
 %0Apublic 
@@ -298,30 +298,35 @@
 e final 
+Set%3C
 String
+%3E
  orgUnit
 ;%0A    pr
@@ -313,24 +313,25 @@
 ing%3E orgUnit
+s
 ;%0A    privat
@@ -486,30 +486,35 @@
         
+Set%3C
 String
+%3E
  orgUnit
 , int pa
@@ -505,16 +505,17 @@
  orgUnit
+s
 , int pa
@@ -627,32 +627,33 @@
     this.orgUnit
+s
  = orgUnit;%0A    
@@ -638,32 +638,33 @@
 gUnits = orgUnit
+s
 ;%0A        this.p
@@ -793,30 +793,35 @@
         
+Set%3C
 String
+%3E
  orgUnit
 , @Nulla
@@ -812,16 +812,17 @@
  orgUnit
+s
 , @Nulla
@@ -962,32 +962,33 @@
     this.orgUnit
+s
  = orgUnit;%0A    
@@ -973,32 +973,33 @@
 gUnits = orgUnit
+s
 ;%0A        this.u
@@ -1321,22 +1321,27 @@
  public 
+Set%3C
 String
+%3E
  getOrgU
@@ -1343,16 +1343,17 @@
 tOrgUnit
+s
 () %7B%0A   
@@ -1367,24 +1367,25 @@
 turn orgUnit
+s
 ;%0A    %7D%0A%0A   
@@ -1593,30 +1593,35 @@
 private 
+Set%3C
 String
+%3E
  orgUnit
 ;%0A      
@@ -1604,32 +1604,33 @@
 %3CString%3E orgUnit
+s
 ;%0A        int pa
@@ -1632,24 +1632,29 @@
 nt pageLimit
+ = 50
 ;%0A%0A        p
@@ -2272,23 +2272,29 @@
 Unit
-(
+s(Set%3C
 String
+%3E
  orgUnit
 ) %7B%0A
@@ -2289,16 +2289,17 @@
  orgUnit
+s
 ) %7B%0A    
@@ -2318,16 +2318,17 @@
 .orgUnit
+s
  = orgUn
@@ -2329,16 +2329,17 @@
  orgUnit
+s
 ;%0A      
@@ -2899,32 +2899,32 @@
 page, pageSize,%0A
-
                 
@@ -2934,16 +2934,17 @@
  orgUnit
+s
 , uIds, 

add an orgUnit set property on TeiQuery