/**
 *
 */
package com.thinkbiganalytics.metadata.sla.api.core;

import com.thinkbiganalytics.calendar.HolidayCalendarService;
import com.thinkbiganalytics.jobrepo.query.model.ExecutedFeed;
import com.thinkbiganalytics.jobrepo.repository.FeedRepository;
import com.thinkbiganalytics.metadata.sla.api.AssessmentResult;
import com.thinkbiganalytics.metadata.sla.api.Metric;
import com.thinkbiganalytics.metadata.sla.spi.MetricAssessmentBuilder;
import com.thinkbiganalytics.metadata.sla.spi.MetricAssessor;
import com.thinkbiganalytics.scheduler.util.CronExpressionUtil;

import org.joda.time.DateTime;
import org.quartz.Calendar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.Date;

import javax.inject.Inject;

/**
 * @author Sean Felten
 */
public class FeedOnTimeArrivalMetricAssessor implements MetricAssessor<FeedOnTimeArrivalMetric, Serializable> {
    private static final Logger LOG = LoggerFactory.getLogger(FeedOnTimeArrivalMetricAssessor.class);

    @Inject
    private FeedRepository feedRepository;

    @Inject
    private HolidayCalendarService calendarService;


    /* (non-Javadoc)
     * @see com.thinkbiganalytics.metadata.sla.spi.MetricAssessor#accepts(com.thinkbiganalytics.metadata.sla.api.Metric)
     */
    @Override
    public boolean accepts(Metric metric) {
        return metric instanceof FeedOnTimeArrivalMetric;
    }

    /* (non-Javadoc)
     * @see com.thinkbiganalytics.metadata.sla.spi.MetricAssessor#assess(com.thinkbiganalytics.metadata.sla.api.Metric, com.thinkbiganalytics.metadata.sla.spi.MetricAssessmentBuilder)
     */
    @Override
    @SuppressWarnings("unchecked")
    public void assess(FeedOnTimeArrivalMetric metric, MetricAssessmentBuilder builder) {
        LOG.debug("Assessing metric: ", metric);

        builder.metric(metric);


        String feedName = metric.getFeedName();
        ExecutedFeed feed = this.feedRepository.findLastCompletedFeed(feedName);


        DateTime lastFeedTime = null;
        if (feed != null) {
            lastFeedTime = feed.getEndTime();
        }
        Date expectedDate = CronExpressionUtil.getPreviousFireTime(metric.getExpectedExpression());
        DateTime expectedTime = new DateTime(expectedDate);
        DateTime lateTime = expectedTime.plus(metric.getLatePeriod());
        //Calendar calendar = getCalendar(metric);
        //  DateTime asOfTime = expectedTime.minus(metric.getAsOfPeriod());
        // boolean isHoliday = !calendar.isTimeIncluded(asOfTime.getMillis());
        boolean isHoliday = false;
        builder.compareWith(expectedDate, feedName);



        if (isHoliday) {
            LOG.debug("No data expected for feed {} due to a holiday", feedName);
            builder.message("No data expected for feed " + feedName + " due to a holiday")
                    .result(AssessmentResult.SUCCESS);
        } else if (lastFeedTime == null ) {
            LOG.debug("Feed with the specified name {} not found", feedName);
            builder.message("Feed with the specified name "+feedName+" not found ")
                .result(AssessmentResult.WARNING);
        }  else if (lastFeedTime.isAfter(expectedTime) && lastFeedTime.isBefore(lateTime)) {
            LOG.debug("Data for feed {} arrived on {}, which was before late time: ", feedName, lastFeedTime, lateTime);

            builder.message("Data for feed " + feedName + " arrived on " + lastFeedTime + ", which was before late time: " + lateTime)
                    .result(AssessmentResult.SUCCESS);
        }
        else if(DateTime.now().isBefore(lateTime)) {
            return;
        }
        else {
            LOG.debug("Data for feed {} has not arrived before the late time: ", feedName, lateTime);

            builder.message("Data for feed " + feedName + " has not arrived before the late time: " + lateTime + "\n The last successful feed was on " + lastFeedTime)
                    .result(AssessmentResult.FAILURE);
        }
    }

    private Calendar getCalendar(FeedOnTimeArrivalMetric metric) {
        return this.calendarService.getCalendar(metric.getCalendarName());
    }

}

@@ -2331,18 +2331,16 @@
         
-//
 Calendar
@@ -2380,18 +2380,99 @@
         
-//
+boolean isHoliday = false;%0A        if (metric.getAsOfPeriod() != null) %7B%0A          
   DateTi
@@ -2541,18 +2541,11 @@
     
-// boolean
+   
  isH
@@ -2613,34 +2613,10 @@
     
-boolean isHoliday = false;
+%7D%0A
 %0A   

fix assessor for unit tests