package io.searchbox.core.search.aggregation;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.util.LinkedList;
import java.util.List;

import static io.searchbox.core.search.aggregation.AggregationField.*;

/**
 * @author cfstout
 */

public class TermsAggregation extends BucketAggregation {

    public static final String TYPE = "terms";

    private Long docCountErrorUpperBound;
    private Long sumOtherDocCount;
    private List<Entry> buckets = new LinkedList<Entry>();

    public TermsAggregation(String name, JsonObject termAggregation) {
        super(name, termAggregation);
        docCountErrorUpperBound = termAggregation.get(String.valueOf(DOC_COUNT_ERROR_UPPER_BOUND)).getAsLong();
        sumOtherDocCount = termAggregation.get(String.valueOf(SUM_OTHER_DOC_COUNT)).getAsLong();

        if(termAggregation.has(String.valueOf(BUCKETS)) && termAggregation.get(String.valueOf(BUCKETS)).isJsonArray()) {
            parseBuckets(termAggregation.get(String.valueOf(BUCKETS)).getAsJsonArray());
        }
    }

    private void parseBuckets(JsonArray bucketsSource) {
        for(JsonElement bucketElement : bucketsSource) {
            JsonObject bucket = (JsonObject) bucketElement;
            Entry entry = new Entry(bucket, bucket.get(String.valueOf(KEY)).getAsString(), bucket.get(String.valueOf(DOC_COUNT)).getAsLong());
            buckets.add(entry);
        }
    }

    public Long getDocCountErrorUpperBound() {
        return docCountErrorUpperBound;
    }

    public Long getSumOtherDocCount() {
        return sumOtherDocCount;
    }

    public List<Entry> getBuckets() {
        return buckets;
    }

    public class Entry extends Bucket {
        private String key;

        public Entry(JsonObject bucket, String key, Long count) {
            super(bucket, count);
            this.key = key;
        }

        public String getKey() {
            return key;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (obj == this) {
                return true;
            }
            if (obj.getClass() != getClass()) {
                return false;
            }

            Entry rhs = (Entry) obj;
            return new EqualsBuilder()
                    .appendSuper(super.equals(obj))
                    .append(key, rhs.key)
                    .isEquals();
        }

        @Override
        public int hashCode() {
            return new HashCodeBuilder()
                    .append(getCount())
                    .append(getKey())
                    .toHashCode();
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != getClass()) {
            return false;
        }

        TermsAggregation rhs = (TermsAggregation) obj;
        return new EqualsBuilder()
                .appendSuper(super.equals(obj))
                .append(buckets, rhs.buckets)
                .append(docCountErrorUpperBound, rhs.docCountErrorUpperBound)
                .append(sumOtherDocCount, rhs.sumOtherDocCount)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder()
                .appendSuper(super.hashCode())
                .append(docCountErrorUpperBound)
                .append(sumOtherDocCount)
                .append(buckets)
                .toHashCode();
    }
}

@@ -772,16 +772,28 @@
         
+JsonElement 
 docCount
@@ -807,16 +807,23 @@
 perBound
+Element
  = termA
@@ -885,29 +885,176 @@
 ND))
-.getAsLong();%0A       
+;%0A        if (docCountErrorUpperBoundElement != null ) %7B%0A            docCountErrorUpperBound = docCountErrorUpperBoundElement.getAsLong();%0A        %7D%0A        JsonElement
  sum
@@ -1066,16 +1066,23 @@
 DocCount
+Element
  = termA
@@ -1124,32 +1124,135 @@
 THER_DOC_COUNT))
+;%0A        if (sumOtherDocCountElement != null) %7B%0A            sumOtherDocCount = sumOtherDocCountElement
 .getAsLong();%0A%0A 
@@ -1248,16 +1248,26 @@
 sLong();
+%0A        %7D
 %0A%0A      

Fix for NPE during TermsAggregation instantiation