package com.jdbernard.timeanalyzer

import com.jdbernard.timestamper.core.Timeline
import org.joda.time.DateTime
import org.joda.time.Duration

public class TimelineEventProcessor {

    /** Events whose description matches one of these regex strings or
      * patterns are ignored. */
    List exclusions = []

    public TimelineEventProcessor() {}

    public TimelineEventProcessor(List exclusions) {
        this.exclusions = exclusions
    }

    public List<Event> process(Timeline timeline) {
        List<Event> events = []

        timeline.each { marker ->
            Event e = new Event()

            e.description = marker.mark
            e.notes = marker.notes
            e.start = new DateTime(marker.timestamp)
            e.duration = new Duration(0)

            // if this is not the first event, then we need to update the
            // duration of the previous event
            if (events.size > 0) {
                Event lastEvent = events[-1]
                lastEvent.duration = new Duration(lastEvent.start, e.start)
            }

            events << e
        }

        def excluded = events.findAll { event ->
            exclusions.any { exclusion -> event.description ==~ exclusion }
        }

        return events - excluded
    }
}
