package org.mbari.seepsearch.io;

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

import org.mbari.io.DelimitedFile;
import org.mbari.seepsearch.core.RovFix;
import org.mbari.seepsearch.util.IRovNavigationIterator;

public class VarsQueryFile extends DelimitedFile implements
    IRovNavigationIterator {

    public VarsQueryFile(String filename) throws IOException {
        this(new File(filename));

    }

    public VarsQueryFile(File file) throws IOException {
        super(file);
        setDelimiter(" ");
        setHasColumnNames(true);
    }

    public void read() throws IOException {
        super.read();
        List columnList = Arrays.asList(getColumnNames());
        latColumn = columnList.indexOf("Lat");
        lonColumn = columnList.indexOf("Lon");

        if ((latColumn == -1) || (lonColumn == -1)) {
            throw new IOException("Not an ROV navigation file...skipping");
        }
    }


    public boolean hasNext() {
        boolean ok = false;
        if (currentRow < getNumOfRows() - 1) {
            ok = true;
        }
        return ok;
    }

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

    public RovFix getNextRovFix() {
        currentRow++;
        double latitude = data[currentRow][latColumn];
        double longitude = data[currentRow][lonColumn];
        double depth = Double.NaN;
        double altitude = Double.NaN;
        return new RovFix(latitude, longitude, depth, altitude);
    }
    
    public  void reset() {
        currentRow = 0;   
    }

    /**
     * Does nothing
     */
    public void remove() {

    }

    int currentRow;
    int latColumn;
    int lonColumn;

}