/**
 * 
 */
package com.browseengine.bobo.geosearch.query;

import java.io.IOException;
import java.util.List;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Weight;

import com.browseengine.bobo.geosearch.IDeletedDocs;
import com.browseengine.bobo.geosearch.impl.IndexReaderDeletedDocs;
import com.browseengine.bobo.geosearch.index.impl.GeoIndexReader;
import com.browseengine.bobo.geosearch.index.impl.GeoSegmentReader;

/**
 * @author Shane Detsch
 * @author Ken McCracken
 *
 */
public class GeoWeight extends Weight {
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    private final GeoQuery geoQuery;

    public GeoWeight(GeoQuery geoQuery) {
        this.geoQuery = geoQuery;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Explanation explain(IndexReader reader, int doc) throws IOException {
        // TODO: improve this to provide the actual distance component of the score, 
        // and explain how we take smoothed 1/distance^2.
        return new Explanation(doc, geoQuery.toString());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Query getQuery() {
        return geoQuery;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public float getValue() {
        // TODO: tune
        return 1;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void normalize(float norm) {
        // TODO: tune
        // no-op
    }

    /**
     * {@inheritDoc}
     * 
     * The GeoScorer.nextDoc() should give you the capability
     *  to go through all the documents that are within 'rangeInMiles'
     *   of the centroid by increasing order of document id.
     */
    @Override
    public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder,
            boolean topScorer) throws IOException {
        if (!(reader instanceof GeoIndexReader)) {
            throw new RuntimeException("attempt to create a "
                    +GeoScorer.class+" with a reader that was not a "
                    +GeoIndexReader.class);
        }
        GeoIndexReader geoIndexReader = (GeoIndexReader) reader;
        List<GeoSegmentReader> segmentsInOrder = geoIndexReader.getGeoSegmentReaders();
        IDeletedDocs wholeIndexDeletedDocs = new IndexReaderDeletedDocs(reader);

        double centroidLongitudeDegrees = geoQuery.getCentroidLongitude();
        double centroidLatitudeDegrees = geoQuery.getCentroidLatitude();
        float rangeInMiles = geoQuery.getRangeInMiles();
        return new GeoScorer(this, segmentsInOrder, wholeIndexDeletedDocs, 
                centroidLongitudeDegrees, centroidLatitudeDegrees, rangeInMiles);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public float sumOfSquaredWeights() throws IOException {
        return 1;
    }
    
    
}

@@ -793,16 +793,101 @@
 eoQuery;
+%0A    private float queryWeight;%0A    private float queryNorm;%0A    private float value;
 %0A%0A    pu
@@ -1294,16 +1294,42 @@
 String()
++%22, queryNorm: %22+queryNorm
 );%0A    %7D
@@ -1451,273 +1451,8 @@
    %0A
-    /**%0A     * %7B@inheritDoc%7D%0A     */%0A    @Override%0A    public float getValue() %7B%0A        // TODO: tune%0A        return 1;%0A    %7D%0A%0A    /**%0A     * %7B@inheritDoc%7D%0A     */%0A    @Override%0A    public void normalize(float norm) %7B%0A        // TODO: tune%0A        // no-op%0A    %7D%0A%0A
     
@@ -2655,32 +2655,461 @@
 eInMiles);%0A    %7D
+%0A    %0A    /**%0A     * %7B@inheritDoc%7D%0A     */%0A    @Override%0A    public float getValue() %7B%0A        return value;%0A    %7D%0A%0A    /**%0A     * %7B@inheritDoc%7D%0A     */%0A    @Override%0A    public void normalize(float queryNorm) %7B%0A        this.queryNorm = queryNorm;%0A        queryWeight *= queryNorm;                   // normalize query weight%0A        // idf is effectively 1%0A        value = queryWeight;                  // idf for document%0A    %7D
 %0A%0A    /**%0A     *
@@ -3217,16 +3217,115 @@
     
-return 1
+// idf is effectively 1%0A        queryWeight = geoQuery.getBoost();%0A        return queryWeight * queryWeight
 ;%0A  

fix boosts, sumOfSquaredWeights, normalize, value.