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;
    }

    /**
     * QuickPlotResultSet
     * Add data from both collections.
     * 
     * DetailReport.java:1156:quickPlot:1247 -> ReportPack.java:278:ConnectQuickPlot:279 
     * -> WepsConnection.java:322:ConnectQuickPlot:324 -> calls this method
     * 
     * This method is given two collections of data for a left and right axis for plotting.
     * if the data is the same size we add both collections to the SQL table.
     * if the collections aren't the same size then one of them contains data and the other
     * doesn't. So we take the larger sized List and add it to the table.
     * 
     * Each DefaultCategoryDataset has data for a graph of plots. Row contains data name
     * Column is the dates of the data. the row and column are used to set or get the data.
     * 
     * See QuickPlotCustomizer.java for additional info on how quick plot is made.
     * 
     * @param collectionL - left collection of data (left Axis)
     * @param collectionR - right collection of data (right axis)
     * @throws SQLException 
     */
    public void add(ArrayList<DefaultCategoryDataset> collectionL,
            ArrayList<DefaultCategoryDataset> collectionR) throws SQLException {
        
        if(collectionR.size() < collectionL.size()) {
            for(int index = 0; index < collectionL.size(); index ++) {
                Object[] row = createNewRow(true);
                setRowValue(row, YEAR, index);
                
                try {
                    setRowValue(row, DATALEFT, collectionL.get(index));
                } catch(IndexOutOfBoundsException less) {
                    setRowValue(row, DATALEFT, new DefaultCategoryDataset());
                }
            }
        } else if(collectionL.size() < collectionR.size()) {
            for(int index = 0; index < collectionR.size(); index ++) {
                Object[] row = createNewRow(true);
                setRowValue(row, YEAR, index);
                
                try {
                    setRowValue(row, DATARIGHT, collectionR.get(index));
                } catch(IndexOutOfBoundsException less) {
                    setRowValue(row, DATARIGHT, new DefaultCategoryDataset());
                }
            }
        } else {
            int size = collectionR.size() >= collectionL.size() ? collectionR.size() : collectionL.size();

            for (int index = 0; index < size; 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 {
                    setRowValue(row, DATALEFT, collectionL.get(index));
                } catch (IndexOutOfBoundsException lessData) {
                    setRowValue(row, DATALEFT, new DefaultCategoryDataset());
                }
                try {
                    setRowValue(row, DATARIGHT, collectionR.get(index));
                } catch (IndexOutOfBoundsException lessData) {
                    setRowValue(row, DATARIGHT, new DefaultCategoryDataset());
                }
            }
        }
        filled = true;
    }
    
    @Override
    public String getName() {
        return NAME;
    }   
}