package com.growthbeat.analytics.model;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.fasterxml.jackson.core.type.TypeReference;
import com.growthbeat.Context;
import com.growthbeat.analytics.query.segment.SegmentQuery;
import com.growthbeat.constants.Constants;
import com.growthbeat.model.Model;
import com.growthbeat.model.Order;
import com.growthbeat.utils.DateUtils;
import com.growthbeat.utils.JsonUtils;

public class Segment extends Model {

	private String id;
	private String name;
	private String description;
	private int size;
	private SegmentQuery query;
	private Date created;

	public static Segment findById(String id, Context context) {
		return get(context, "/1/segments/" + id, new HashMap<String, Object>(), Segment.class);
	}

	public static List<Segment> findByParentSegmentId(String parentSegmentId, Order order, Integer page, Integer limit, Boolean onlyLeaf,
			Context context) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("parentSegmentId", parentSegmentId);
		if (order != null)
			params.put("order", order.toString());
		if (page != null)
			params.put("page", page);
		if (limit != null)
			params.put("limit", limit);
		if (onlyLeaf != null)
			params.put("onlyLeaf", onlyLeaf);
		return get(context, "/1/segments", params, new TypeReference<List<Segment>>() {
		});
	}

	public static Set<String> findClientIdsBySegmentQuery(String applicationId, SegmentQuery segmentQuery,
			Date begin, Date end, boolean cacheable, Context context) {

		Map<String, Object> params = new HashMap<String, Object>();
		if (applicationId != null)
			params.put("applicationId", applicationId);
		if (segmentQuery != null)
			params.put("segmentQuery", JsonUtils.serialize(segmentQuery));
		if (begin != null)
			params.put("begin", DateUtils.dateTimeStringFromDateWithFormat(begin, Constants.ISO_8601_DATETIME_FORMAT));
		if (end != null)
			params.put("end", DateUtils.dateTimeStringFromDateWithFormat(end, Constants.ISO_8601_DATETIME_FORMAT));
		params.put("cacheable", cacheable);

		return get(context, "/1/segments/client_ids", params, new TypeReference<Set<String>>() {
		});
	}

	public static Segment update(String id, String name, String description, SegmentQuery query, Context context) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", name);
		params.put("description", description);
		if (query != null)
			params.put("query", JsonUtils.serialize(query));
		return put(context, "/1/segments/" + id, params, Segment.class);
	}

	public static Segment updateSize(String id, Context context) {
		Map<String, Object> params = new HashMap<String, Object>();
		return put(context, "/1/segments/" + id + "/size", params, Segment.class);
	}

	public static void deleteById(String id, Context context) {
		delete(context, "/1/segments/" + id, new HashMap<String, Object>(), Void.class);
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public SegmentQuery getQuery() {
		return query;
	}

	public void setQuery(SegmentQuery query) {
		this.query = query;
	}

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

}

@@ -2201,32 +2201,794 @@
 %3E() %7B%0A%09%09%7D);%0A%09%7D%0A%0A
+%09public static Set%3CString%3E findClientIdsByRealtimeSegmentQuery(String applicationId, SegmentQuery segmentQuery,%0A%09%09%09Date begin, Date end, Context context) %7B%0A%09%09%0A%09%09Map%3CString, Object%3E params = new HashMap%3CString, Object%3E();%0A%09%09if (applicationId != null)%0A%09%09%09params.put(%22applicationId%22, applicationId);%0A%09%09if (segmentQuery != null)%0A%09%09%09params.put(%22segmentQuery%22, JsonUtils.serialize(segmentQuery));%0A%09%09if (begin != null)%0A%09%09%09params.put(%22begin%22, DateUtils.dateTimeStringFromDateWithFormat(begin, Constants.ISO_8601_DATETIME_FORMAT));%0A%09%09if (end != null)%0A%09%09%09params.put(%22end%22, DateUtils.dateTimeStringFromDateWithFormat(end, Constants.ISO_8601_DATETIME_FORMAT));%0A%09%09%0A%09%09return get(context, %22/1/segments/realtime_client_ids%22, params, new TypeReference%3CSet%3CString%3E%3E() %7B%0A%09%09%7D);%0A%09%7D%0A%0A
 %09public static S

add realtime segemnt