/*
 * Licensed to Elastic Search and Shay Banon under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Elastic Search licenses this
 * file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.index.query.json;

import com.google.inject.Inject;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.TermRangeFilter;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.elasticsearch.index.AbstractIndexComponent;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.query.QueryParsingException;
import org.elasticsearch.index.settings.IndexSettings;
import org.elasticsearch.util.Booleans;
import org.elasticsearch.util.settings.Settings;

import java.io.IOException;

import static org.elasticsearch.index.query.support.QueryParsers.*;

/**
 * @author kimchy (Shay Banon)
 */
public class RangeJsonFilterParser extends AbstractIndexComponent implements JsonFilterParser {

    public static final String NAME = "range";

    @Inject public RangeJsonFilterParser(Index index, @IndexSettings Settings settings) {
        super(index, settings);
    }

    @Override public String[] names() {
        return new String[]{NAME};
    }

    @Override public Filter parse(JsonQueryParseContext parseContext) throws IOException, QueryParsingException {
        JsonParser jp = parseContext.jp();

        JsonToken token = jp.nextToken();
        assert token == JsonToken.FIELD_NAME;
        String fieldName = jp.getCurrentName();

        // now, we move after the field name, which starts the object
        token = jp.nextToken();
        assert token == JsonToken.START_OBJECT;

        String from = null;
        String to = null;
        boolean includeLower = true;
        boolean includeUpper = true;

        String currentFieldName = null;
        while ((token = jp.nextToken()) != JsonToken.END_OBJECT) {
            if (token == JsonToken.FIELD_NAME) {
                currentFieldName = jp.getCurrentName();
            } else {
                if ("from".equals(currentFieldName)) {
                    if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
                        from = null;
                    } else {
                        from = jp.getText();
                    }
                } else if ("to".equals(currentFieldName)) {
                    if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
                        to = null;
                    } else {
                        to = jp.getText();
                    }
                } else if ("include_lower".equals(currentFieldName)) {
                    if (token == JsonToken.VALUE_NUMBER_INT) {
                        includeLower = jp.getIntValue() != 0;
                    } else if (token == JsonToken.VALUE_STRING) {
                        includeLower = Booleans.parseBoolean(jp.getText(), includeLower);
                    } else {
                        includeLower = token == JsonToken.VALUE_TRUE;
                    }
                } else if ("include_upper".equals(currentFieldName)) {
                    if (token == JsonToken.VALUE_NUMBER_INT) {
                        includeUpper = jp.getIntValue() != 0;
                    } else if (token == JsonToken.VALUE_STRING) {
                        includeUpper = Booleans.parseBoolean(jp.getText(), includeUpper);
                    } else {
                        includeUpper = token == JsonToken.VALUE_TRUE;
                    }
                } else if ("gte".equals(currentFieldName)) {
                    if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
                        from = null;
                    } else {
                        from = jp.getText();
                    }
                    includeLower = true;
                } else if ("lt".equals(currentFieldName)) {
                    if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
                        to = null;
                    } else {
                        to = jp.getText();
                    }
                    includeUpper = false;
                } else if ("lte".equals(currentFieldName)) {
                    if (jp.getCurrentToken() == JsonToken.VALUE_NULL) {
                        to = null;
                    } else {
                        to = jp.getText();
                    }
                    includeUpper = true;
                }
            }
        }

        // move to the next end object, to close the field name
        token = jp.nextToken();
        assert token == JsonToken.END_OBJECT;

        Filter filter = null;
        MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName);
        if (smartNameFieldMappers != null) {
            if (smartNameFieldMappers.hasMapper()) {
                filter = smartNameFieldMappers.mapper().rangeFilter(from, to, includeLower, includeUpper);
            }
        }
        if (filter == null) {
            filter = new TermRangeFilter(fieldName, from, to, includeLower, includeUpper);
        }
        return wrapSmartNameFilter(filter, smartNameFieldMappers, parseContext.indexCache());
    }
}
@@ -4159,32 +4159,339 @@
               %7D%0A
+                %7D else if (%22gt%22.equals(currentFieldName)) %7B%0A                    if (jp.getCurrentToken() == JsonToken.VALUE_NULL) %7B%0A                        from = null;%0A                    %7D else %7B%0A                        from = jp.getText();%0A                    %7D%0A                    includeLower = false;%0A
                 

Range filter gt is broken, closes #119.