package com.github.avarabyeu.jashing.integration.stackoverflow;

import com.github.avarabyeu.jashing.core.EventSource;
import com.github.avarabyeu.jashing.core.eventsource.ScheduledEventSource;
import com.github.avarabyeu.jashing.events.StatusAwareNumberEvent;
import com.github.avarabyeu.jashing.integration.stackoverflow.model.Question;

import javax.inject.Inject;
import javax.inject.Named;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

/**
 * @author Andrei Varabyeu
 */
@EventSource(value = "stackoverflow-not-answered-source", explicitConfiguration = StackOverflowModule.class)
public class StackOverflowUnansweredSource extends ScheduledEventSource<StatusAwareNumberEvent> {

    @Inject
    private StackExchangeClient client;

    @Inject
    @Named("stackoverflow.question.tag")
    private String tag;

    @Override
    protected StatusAwareNumberEvent produceEvent() {
        final List<Question> questions = client.getTaggedQuestions(tag).getItems();

        final int count = questions.size();
        final Long notTodayCount = questions.stream()
                .filter(q -> q.getCreationDate()
                        .isBefore(LocalDateTime.now().toLocalDate().atStartOfDay(ZoneId.systemDefault()).toInstant()))
                .count();
        return new StatusAwareNumberEvent(
                count > 0 ? StatusAwareNumberEvent.Status.warning : StatusAwareNumberEvent.Status.ok, count,
                notTodayCount.intValue());
    }
}

@@ -467,16 +467,52 @@
 il.List;
+%0Aimport java.util.stream.Collectors;
 %0A%0A/**%0A *
@@ -994,16 +994,33 @@
 = client
+%0A                
 .getTagg
@@ -1039,18 +1039,144 @@
 tag)
-.getItems(
+%0A                .getItems()%0A                    .stream()%0A                    .filter(q -%3E !q.isAnswered()).collect(Collectors.toList()
 );%0A%0A

make stackoverflow questions not answered