/*
 * QueryResultsAdapter.java
 *
 * Created on February 7, 2006, 9:56 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.mbari.seepsearch.io;

import java.util.List;

import org.mbari.seepsearch.core.RovFix;
import org.mbari.seepsearch.util.IRovNavigationIterator;
import org.mbari.sql.QueryResults;

/**
 *
 * @author brian
 */
public class QueryResultsAdapter implements IRovNavigationIterator {
    
    private final QueryResults queryResults;
    private final List latitudes;
    private final List longitudes;
    private final List depths;
    private int currentRow = 0;
    
    /** Creates a new instance of QueryResultsAdapter */
    public QueryResultsAdapter(QueryResults queryResults) {
        this.queryResults = queryResults;
        this.latitudes = queryResults.getResults("Latitude");
        this.longitudes = queryResults.getResults("Longitude");
        this.depths = queryResults.getResults("Depth");
        // TODO verify that the result set has Latidute, Longitude and Depth columns
    }

    public RovFix getNextRovFix() {
        double latitude = getDoubleValue(latitudes.get(currentRow));
        double longitude = getDoubleValue(longitudes.get(currentRow)); 
        double depth =  getDoubleValue(depths.get(currentRow));
        currentRow++;
        return new RovFix(latitude, longitude, depth, Double.NaN);
        
    }
    
    private double getDoubleValue(Object obj) {
        return (obj == null) ? Double.NaN : ((Number) obj).doubleValue();
    }

    public boolean hasNext() {
        return currentRow < depths.size();
    }

    public Object next() {
        return getNextRovFix();
    }

    public void remove() {
        // Do nothing
    }
    
}
