package io.keen.client.java;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class DatasetQuery {

    private String projectId;

    private String analysisType;

    private String eventCollection;

    private String timezone;

    private String interval;

    private String timeframe;

    private List<String> groupBy;

    private List<Filter> filters;

    @SuppressWarnings("unchecked")
    static DatasetQuery fromMap(Map<String, Object> properties) {
        DatasetQuery query = new DatasetQuery();
        query.projectId = (String) properties.get("project_id");
        query.analysisType = (String) properties.get("analysis_type");
        query.eventCollection = (String) properties.get("event_collection");
        query.timezone = (String) properties.get("timezone");
        query.interval = (String) properties.get("interval");
        query.timeframe = (String) properties.get("timeframe");
        query.groupBy = (List<String>) properties.get("group_by");

        if (properties.get("filters") != null) {
            query.filters = new LinkedList<Filter>();
            for (Map<String, Object> filter : (List<Map<String, Object>>) properties.get("filters")) {
                query.filters.add(new Filter(
                        (String) filter.get("property_name"),
                        FilterOperator.fromString((String) filter.get("operator")),
                        filter.get("property_value")
                ));
            }
        }
        return query;
    }

    public String getProjectId() {
        return projectId;
    }

    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    public String getAnalysisType() {
        return analysisType;
    }

    public void setAnalysisType(String analysisType) {
        this.analysisType = analysisType;
    }

    public String getEventCollection() {
        return eventCollection;
    }

    public void setEventCollection(String eventCollection) {
        this.eventCollection = eventCollection;
    }

    public String getTimezone() {
        return timezone;
    }

    public void setTimezone(String timezone) {
        this.timezone = timezone;
    }

    public String getInterval() {
        return interval;
    }

    public void setInterval(String interval) {
        this.interval = interval;
    }

    public String getTimeframe() {
        return timeframe;
    }

    public void setTimeframe(String timeframe) {
        this.timeframe = timeframe;
    }

    public List<String> getGroupBy() {
        return groupBy;
    }

    public void setGroupBy(List<String> groupBy) {
        this.groupBy = groupBy;
    }

    public List<Filter> getFilters() {
        return filters;
    }

    public void setFilters(List<Filter> filters) {
        this.filters = filters;
    }

    Map<String, Object> asMap() {
//        private List<String> groupBy;
//        private List<Filter> filters;
        Map<String, Object> result = new HashMap<String, Object>();
        if (analysisType != null) {
            result.put(KeenQueryConstants.ANALYSIS_TYPE, analysisType);
        }
        if (eventCollection != null) {
            result.put(KeenQueryConstants.EVENT_COLLECTION, eventCollection);
        }
        if (timeframe != null) {
            result.put(KeenQueryConstants.TIMEFRAME, timeframe);
        }
        if (timezone != null) {
            result.put(KeenQueryConstants.TIMEZONE, timezone);
        }
        if (interval != null) {
            result.put(KeenQueryConstants.INTERVAL, interval);
        }
        return result;
    }

    @Override
    public String toString() {
        return "DatasetQuery{" +
                "projectId='" + projectId + '\'' +
                ", analysisType='" + analysisType + '\'' +
                ", eventCollection='" + eventCollection + '\'' +
                ", timezone='" + timezone + '\'' +
                ", interval='" + interval + '\'' +
                ", timeframe='" + timeframe + '\'' +
                ", groupBy=" + groupBy +
                ", filters=" + filters +
                '}';
    }
}

@@ -124,16 +124,73 @@
 l.Map;%0A%0A
+import static io.keen.client.java.KeenQueryConstants.*;%0A%0A
 public c
@@ -271,24 +271,60 @@
 lysisType;%0A%0A
+    private String targetProperty;%0A%0A
     private 
@@ -1982,32 +1982,222 @@
 sisType;%0A    %7D%0A%0A
+    public String getTargetProperty() %7B%0A        return targetProperty;%0A    %7D%0A%0A    public void setTargetProperty(String targetProperty) %7B%0A        this.targetProperty = targetProperty;%0A    %7D%0A%0A
     public Strin
@@ -3405,35 +3405,16 @@
 ult.put(
-KeenQueryConstants.
 ANALYSIS
@@ -3431,32 +3431,33 @@
 lysisType);%0A
+%0A
         
 %7D%0A        if
@@ -3448,37 +3448,256 @@
     
-%7D%0A        if (eventCollection
+    if (!COUNT.equals(analysisType) && targetProperty == null) %7B%0A                throw new IllegalStateException(%22target_property must not be null when analysis_type does not equal to '%22 + COUNT + %22'%22);%0A            %7D%0A%0A            if (targetProperty
  != 
@@ -3708,32 +3708,36 @@
 ) %7B%0A            
+    
 result.put(KeenQ
@@ -3731,35 +3731,136 @@
 ult.put(
-KeenQueryConstants.
+TARGET_PROPERTY, targetProperty);%0A            %7D%0A        %7D%0A        if (eventCollection != null) %7B%0A            result.put(
 EVENT_CO
@@ -3953,35 +3953,16 @@
 ult.put(
-KeenQueryConstants.
 TIMEFRAM
@@ -4041,35 +4041,16 @@
 ult.put(
-KeenQueryConstants.
 TIMEZONE
@@ -4127,35 +4127,16 @@
 ult.put(
-KeenQueryConstants.
 INTERVAL
@@ -4368,32 +4368,95 @@
 isType + '%5C'' +%0A
+                %22, targetProperty='%22 + targetProperty + '%5C'' +%0A
                 

Support for other that "count" analysis types.