package com.hubspot.singularity.helpers;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dmfs.rfc5545.recur.InvalidRecurrenceRuleException;
import org.dmfs.rfc5545.recur.RecurrenceRule;
import org.dmfs.rfc5545.DateTime;
import org.dmfs.rfc5545.recur.RecurrenceRule.Part;
import org.dmfs.rfc5545.recur.RecurrenceRuleIterator;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class RFC5545Schedule {
  public static final int MAX_ITERATIONS = 10000;
  private final RecurrenceRule recurrenceRule;
  private final org.joda.time.DateTime dtStart;

  public RFC5545Schedule(String schedule) throws InvalidRecurrenceRuleException {
    // DTSTART is RFC5545 but NOT in the recur string, but its a nice to have? :)
    Pattern pattern = Pattern.compile("DTSTART=([0-9]{8}T[0-9]{6})");
    Matcher matcher = pattern.matcher(schedule);

    if (matcher.find()) {
      DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMdd'T'HHmmss");
      if (schedule.contains("REPEAT") || schedule.contains("COUNT")) {
        this.dtStart = formatter.parseDateTime(matcher.group(1));
      } else {
        org.joda.time.DateTime start = formatter.parseDateTime(matcher.group(1));
        org.joda.time.DateTime now = org.joda.time.DateTime.now().withSecondOfMinute(0);
        if (now.getMillis() > start.getMillis()) {
          start = now;
        }
        this.dtStart = start;
      }
      this.recurrenceRule = new RecurrenceRule(matcher.replaceAll("").replace("RRULE:", ""));
    } else {
      this.recurrenceRule = new RecurrenceRule(schedule);
      this.dtStart = org.joda.time.DateTime.now().withSecondOfMinute(0);
    }
  }

  public org.joda.time.DateTime getStartDateTime() {
    return dtStart;
  }

  public Date getNextValidTime() {
    final long now = System.currentTimeMillis();
    DateTime startDateTime = new DateTime(dtStart.getYear(), (dtStart.getMonthOfYear() - 1), dtStart.getDayOfMonth(),
      dtStart.getHourOfDay(), dtStart.getMinuteOfHour(), dtStart.getSecondOfMinute());
    RecurrenceRuleIterator timeIterator = recurrenceRule.iterator(startDateTime);

    int count = 0;
    while (timeIterator.hasNext() && (count < MAX_ITERATIONS || (recurrenceRule.hasPart(Part.COUNT) && count < recurrenceRule.getCount()))) {
      count ++;
      long nextRunAtTimestamp = timeIterator.nextMillis();
      if (nextRunAtTimestamp >= now) {
        return new Date(nextRunAtTimestamp);
      }
    }
    return null;
  }

  public RecurrenceRule getRecurrenceRule() {
    return recurrenceRule;
  }
}

@@ -1197,32 +1197,16 @@
     
-org.joda.time.DateTime s
+this.dtS
 tart
@@ -1255,211 +1255,8 @@
 ));%0A
-        org.joda.time.DateTime now = org.joda.time.DateTime.now().withSecondOfMinute(0);%0A        if (now.getMillis() %3E start.getMillis()) %7B%0A          start = now;%0A        %7D%0A        this.dtStart = start;%0A
     

remove the now > start check, need to do the whole loop