package gr.uoa.di.webui.search;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.log4j.Logger;


public class DateCriterion extends FieldCriterion{

	public Logger logger = Logger.getLogger(DateCriterion.class);
	
	public static long ONE_DAY_MILLIS = 24 * 60 * 60 * 1000;
	
	private int fromMonth = -1;
	private int fromYear = -1;
	private int toMonth = -1;
	private int toYear = -1;
	
	private int monthsOld = -1;
	private long[] ranges;
	
	public DateCriterion(String name, String display, String value, long[] ranges) 
		throws ParseException {
		
		super(name, display, value, Qualifier.DATE);

		if( value != null ){
			this.ranges = ranges;
			this.setMonthsOld();
			if( this.getMonthsOld() == 0 ){
				this.setDetailedDate();
			}
		}

	}
	
	private void setMonthsOld() throws ParseException{
	
		if(ranges == null){
			return;
		}
	
		Date today = new Date();
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 24);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		
		calendar.setTime(today);
		int currentYear = calendar.get(Calendar.YEAR);
		int currentMonth = calendar.get(Calendar.MONTH)+1;
		int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
		
		String range = super.getValue();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    	String[] split = range.split(" ");
    	Date to = sf.parse(split[1]);
    	
    	calendar.setTime(to);
    	int selectedYear = calendar.get(Calendar.YEAR);
    	int selectedMonth = calendar.get(Calendar.MONTH)+1;
    	int selectetDay = calendar.get(Calendar.DAY_OF_MONTH);

    	if(selectedYear == currentYear && selectedMonth == currentMonth
    			&& selectetDay == currentDay){
    		long toInMillis = calendar.getTimeInMillis()/ONE_DAY_MILLIS;
   		
    		Date from = sf.parse(split[0]);
    		calendar.setTime(from);
    		
    		long fromInMillis = calendar.getTimeInMillis()/ONE_DAY_MILLIS;
    	
    		long currentRange = (toInMillis - fromInMillis)/  30;
    		logger.debug("current range " + currentRange);
    		
    		for ( int i = 0; i < ranges.length; i++ ) {
				if( ranges[i] == currentRange){
					monthsOld = (int)ranges[i];
				}
			}
    		
    	} else {
    		monthsOld = 0;
    		
    	}
    	
    	logger.debug("months old " + monthsOld);
	}

	private void setDetailedDate() throws ParseException {
		String range = super.getValue();
		
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    	String[] split = range.split(" ");
    	Date from = sf.parse(split[0]);
    	Date to = sf.parse(split[1]);
    	
    	Calendar calendar = GregorianCalendar.getInstance();
    	calendar.set(Calendar.HOUR_OF_DAY, 24);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
    	
    	calendar.setTime(from);
    	this.fromMonth = calendar.get(Calendar.MONTH) + 1;
        this.fromYear = calendar.get(Calendar.YEAR);
        
    	calendar.setTime(to);
    	this.toMonth = calendar.get(Calendar.MONTH) + 1;
        this.toYear = calendar.get(Calendar.YEAR);
    	
        this.monthsOld = 0;
	}
	
	
	public int getFromMonth() {
		return fromMonth;
	}

	public int getFromYear() {
		return fromYear;
	}

	public int getToMonth() {
		return toMonth;
	}

	public int getToYear() {
		return toYear;
	}

	public int getMonthsOld() {
		return monthsOld;
	}
	
	@Override
	public String toString() {
		// used for debugging!
		return "monthsOld : " + monthsOld +
			", fromMonth:" + fromMonth + ", fromYear: " + fromYear +
			", toMonth: " + toMonth + ", toYear: " + toYear;
	}
	
}
