package io.keen.client.java;

import io.keen.client.java.exceptions.KeenQueryClientException;
import io.keen.client.java.http.HttpMethods;

import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static java.util.Collections.emptyMap;

abstract class CachedDatasetRequest extends PersistentAnalysis {

    private CachedDatasetRequest(String httpMethod, boolean needsMasterKey, String datasetName) {
        super(httpMethod, needsMasterKey, datasetName, null);
    }

    static KeenQueryRequest definitionRequest(String datasetName) {
        return new CachedDatasetRequest(HttpMethods.GET, false, datasetName) {

            @Override
            URL getRequestURL(RequestUrlBuilder urlBuilder, String projectId) throws KeenQueryClientException {
                return urlBuilder.getDatasetsUrl(projectId, getResourceName(), null, Collections.<String, Object>emptyMap());
            }
        };
    }

    static KeenQueryRequest resultsRequest(String datasetName, final String indexBy, final Timeframe timeframe, final Collection<String> groupByParams) {
        return new CachedDatasetRequest(HttpMethods.GET, false, datasetName) {
            @Override
            URL getRequestURL(RequestUrlBuilder urlBuilder, String projectId) throws KeenQueryClientException {
                return urlBuilder.getDatasetsUrl(projectId, getResourceName(), "/results", queryArgs());
            }

            @Override
            boolean retrievingResults() {
                return true;
            }

            @Override
            boolean groupedResponseExpected() {
                return groupByParams != null && !groupByParams.isEmpty();
            }

            @Override
            Collection<String> getGroupByParams() {
                return groupByParams;
            }

            private Map<String, Object> queryArgs() {
                if (indexBy == null) {
                    throw new IllegalArgumentException("index_by is required");
                }
                if (timeframe == null) {
                    throw new IllegalArgumentException("timeframe is required");
                }

                HashMap<String, Object> queryArgs = new HashMap<String, Object>();
                queryArgs.put(KeenQueryConstants.INDEX_BY, indexBy);
                queryArgs.putAll(timeframe.constructTimeframeArgs());
                return queryArgs;
            }
        };
    }

    @Override
    String getAnalysisType() {
        return KeenQueryConstants.DATASETS;
    }

    @Override
    Map<String, Object> constructRequestArgs() {
        return emptyMap();
    }

    @Override
    boolean intervalResponseExpected() {
        return true;
    }

}

@@ -23,16 +23,128 @@
 .java;%0A%0A
+import com.fasterxml.jackson.databind.ObjectMapper;%0Aimport com.fasterxml.jackson.databind.SerializationFeature;%0A
 import i
@@ -245,16 +245,73 @@
 thods;%0A%0A
+import java.io.IOException;%0Aimport java.io.StringWriter;%0A
 import j
@@ -1376,32 +1376,193 @@
  datasetName) %7B%0A
+            private final ObjectMapper objectMapper = new ObjectMapper()%0A                    .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);%0A%0A
             @Ove
@@ -1671,32 +1671,58 @@
 ientException %7B%0A
+                try %7B%0A    
                 
@@ -1802,32 +1802,151 @@
 , queryArgs());%0A
+                %7D catch (IOException e) %7B%0A                    throw new KeenQueryClientException(e);%0A                %7D%0A
             %7D%0A%0A 
@@ -2385,24 +2385,43 @@
  queryArgs()
+ throws IOException
  %7B%0A         
@@ -2681,32 +2681,144 @@
              %7D%0A%0A
+                Object timeframeAsArgs = timeframe.constructTimeframeArgs().get(KeenQueryConstants.TIMEFRAME);%0A%0A
                 
@@ -2986,32 +2986,72 @@
 .put
-All(timeframe.constructT
+(KeenQueryConstants.TIMEFRAME, objectMapper.writeValueAsString(t
 imef
@@ -3055,20 +3055,21 @@
 meframeA
+sA
 rgs
-(
 ));%0A    

Support for timeframes in CDS client