package com.afi.estimator.common
{
    public class DateUtil
    {
        public static const YEAR:String        = "fullYear";
        public static const MONTH:String    = "month";
        public static const WEEK:String        = "week";
        public static const DAY_OF_MONTH:String    = "date";
        public static const HOURS:String    = "hours";
        public static const MINUTES:String    = "minutes";
        public static const SECONDS:String    = "seconds";
        public static const MILLISECONDS:String    = "milliseconds";
        public static const DAY_OF_WEEK:String    = "day";
        
        public function DateUtil()
        {
        }
        
        /**
         * Adds the specified number of "date parts" to a date, e.g. 6 days
         * 
         * @param datePart    The part of the date that will be added
         * @param number    The total number of "dateParts" to add to the date
         * @param date        The date on which to add
         * 
         * @return            The new date
         */
        public static function dateAdd( datePart:String, number:Number, date:Date ):Date {
            var _returnDate:Date = new Date( date );
            
            switch ( datePart ) {
                case DateUtil.YEAR:
                case DateUtil.MONTH:
                case DateUtil.DAY_OF_MONTH:
                case DateUtil.HOURS:
                case DateUtil.MINUTES:
                case DateUtil.SECONDS:
                case DateUtil.MILLISECONDS:
                    _returnDate[ datePart ] += number;
                    break;
                case DateUtil.WEEK:
                    _returnDate[ DateUtil.DAY_OF_MONTH ] += number * 7;
                    break;
                default:
                    /* Unknown date part, do nothing. */
                    break;
            }
            return _returnDate;
        }
        
        public static function julianDay(date:Date):int{
            var dobYear:Number = date.getFullYear();
            var dateString:String = "01/01/"+dobYear;
            var beginningDate:Date = new Date(dateString);
            var diffOfDatesInDays:int = countDays(beginningDate, date);
            return (diffOfDatesInDays + 1);
            
        }
        
        public static function countDays( startDate:Date, endDate:Date ):int 
        { 
            var oneDay:int = 24*60*60*1000; // hours*minutes*seconds*milliseconds 
            var diffDays:int = Math.round(Math.abs((startDate.getTime() - endDate.getTime())/(oneDay))); 
            return diffDays; 
        } 
        
        public static function calculateAge(dobDate:Date):int{
            var originalAge:int;
            var todaysDate:Date = new Date();
            
            //calculate the year difference => age.
            originalAge = todaysDate.getFullYear() - dobDate.getFullYear(); 
            
            //get the corresponding julian day for the dob selected.
            var julianDay:int = julianDay(dobDate);
            trace("Ju " + julianDay);
            var temp:int = julianDay + 182;
            
            if(!isFutureDate(temp)){
                originalAge += 1;
            }
            
            return originalAge;
        }
        
        public static function isFutureDate(julianDay:int):Boolean{
            var todaysDate:Date = new Date();
            var dateString:String = "01/01/" + todaysDate.getFullYear();
            trace("*Todays date ***" + todaysDate);
            var janDate:Date = new Date(dateString);
            var timeinsec:Number=  janDate.time + DateUtil.convertDaysToMilliseconds(julianDay);
            var dateWRTJulian:Date = new Date(timeinsec);
            trace("*Julian Date**" + dateWRTJulian);
            var dif:int = countDays(todaysDate, dateWRTJulian);
            trace(" difference " + dif);
            if(dif >= 0)
                return true;
            
            return false;
        }
        
        public static function convertDaysToMilliseconds(numDays:int):Number {
            return 1000 * 60 * 60 * 24 * numDays;
        }
    }
}