package usda.weru.weps.reports.query;

import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 * The purpose of this class is to allow looping of the data needed for dynamic
 * allocation for the quickplot
 * @author Jonathan
 */
public class QuickPlotResultSet extends WepsResultSet
{

    public static final String NAME = "quickplot";
    
    public static final String DATALEFT = "leftdata";
    
    public static final String DATARIGHT = "rightdata";
    
    public static final String YEAR = "year";
    
    private final WepsConnection wepscon;
    
    private boolean filled = false;
    
    public QuickPlotResultSet(WepsConnection w_con)
    {
        wepscon = w_con;
        addColumn(YEAR, Types.INTEGER, 10, 0);
        addColumn(DATALEFT, Types.JAVA_OBJECT, 10, 0);
        addColumn(DATARIGHT, Types.JAVA_OBJECT, 10, 0);
    }
    
    @Override
    public void fill() throws SQLException 
    {
        if(filled) return;
        Object[] row = createNewRow(true);
        setRowValue(row, YEAR, 0);
        setRowValue(row, DATALEFT, new DefaultCategoryDataset());
        setRowValue(row, DATARIGHT, new DefaultCategoryDataset());
        filled = true;
    }
    
    public void add(ArrayList<DefaultCategoryDataset> col0, 
            ArrayList<DefaultCategoryDataset> col1) throws SQLException
    {
        int greater = col1.size() > col0.size() ? col1.size() : col0.size();
        for(int index = 0; index < greater; index ++)
        {            
            Object[] row = createNewRow(true);
            setRowValue(row, YEAR, index);
            //If one of the datasets is smaller than the other, we need to take
            //that into account:  those datasets will be empty.
            try { System.out.println(col0.get(index)); setRowValue(row, DATALEFT, col0.get(index)); }
            catch(IndexOutOfBoundsException lessData) {setRowValue(row, DATALEFT, new DefaultCategoryDataset()); }
            try { System.out.println(col1.get(index)); setRowValue(row, DATARIGHT, col1.get(index)); }
            catch(IndexOutOfBoundsException lessData) {setRowValue(row, DATARIGHT, new DefaultCategoryDataset()); }
        }
        filled = true;
    }

    @Override
    public String getName() 
    {
        return NAME;
    }
    
}
