package usda.weru.weps.reports.query;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import org.openide.util.Exceptions;
import static usda.weru.util.diff.DifferFrame.LOGGER;

/**
 *
 * @author alexblum
 */
public class DateSetandCalc {

    private final Set_CI_DataStruct CI_DS;

    public DateSetandCalc() {
        CI_DS = new Set_CI_DataStruct();
    }

    //Method to determine location in season.out where biomass for specific interval is indexed
    //This function is used in relation to multiharvested crops
    public int getDateRange(
            DateFormat terminateDateFormat,
            String[] season_line_parts,
            String end) throws SQLException {
        int index = 0;
        while (index < season_line_parts.length) {
            //find location in season.out to pull date from 
            //if multiple harvest crop we are in trouble for now
            if (CI_DS.isValidDate(season_line_parts[index])) {
                String season_year = season_line_parts[index].trim();
                try {
                    Date sDate = terminateDateFormat.parse(season_year);
                    Date hDate = terminateDateFormat.parse(end);
                    //as soon as data for season.out is found, s_out returns index
                    //System.out.println("[getDateRange] comparing: " +season_year + " " + end);
                    if (sDate.equals(hDate)) {
                        //System.out.println("date match");
                        break;
                    }
                } catch (ParseException ex) {
                     LOGGER.error("Error parsing locating date in [DateSetandCalc] .getDateRange().");
                }
            }
            index++;
        }
        return index;
    }

}
