package org.mbari.seepsearch.io;

import org.mbari.seepsearch.util.IRovNavigationIterator;
import org.mbari.seepsearch.core.RovFix;
import org.mbari.seepsearch.db.*;
import java.sql.*;

public class VarsNavigationQuery extends SimpleVarsQuery implements
    IRovNavigationIterator {

    public VarsNavigationQuery(String keywork) throws ClassNotFoundException,
     SQLException, IllegalAccessException, InstantiationException {
        super();
        resultSet = execute(keywork);
    }

    public RovFix getNextRovFix() {
        RovFix rovFix = null;
        try {
            double latitude = resultSet.getDouble(3);
            double longitude = resultSet.getDouble(4);
            double depth = resultSet.getDouble(5);
            rovFix = new RovFix(latitude, longitude, depth, Double.NaN);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            System.out.println("*****Failure*****");
            showResultSet();
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("*****Failure*****");
            showResultSet();
            e.printStackTrace();

        }
        return rovFix;
    }

    private void showResultSet() {
        for (int i = 1; i <= 8; i++) {
            try {
                System.out.print(resultSet.getString(i) + ",");
            } catch (SQLException e2) {
                e2.printStackTrace();
            }
        }
        System.out.println();
    }

    public boolean hasNext() {
        boolean ok = false;
        try {
            ok = resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ok;
    }

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

    public void remove() {
        /**@todo: Implement this java.util.Iterator method*/
        throw new java.lang.UnsupportedOperationException("Method remove() not yet implemented.");
    }

    private ResultSet getResultSet() {
        return resultSet;
    }


    public static void main(String[] args) {
        try {
            VarsNavigationQuery q = new VarsNavigationQuery("nanomia");
            ResultSet r = q.getResultSet();
            while(r.next()) {
                for (int i = 1; i <= 8; i++) {
                    System.out.print(r.getString(i) + ",");
                }
                System.out.println();

            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    private ResultSet resultSet;

}