/*
 * Copyright 2016 Benoît Prioux
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.bdxjug.api.meetings;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import org.bdxjug.api.interfaces.MeetupAPI;

import java.time.Instant;
import java.time.ZoneId;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class MeetingRepository {

    private static final MeetupAPI API = MeetupAPI.api();
    private static final String JUG_GROUP = "BordeauxJUG";

    private final LoadingCache<String, List<MeetingAttendee>> attendeeByMeetingId;
    private final List<Meeting> meetings;

    public MeetingRepository() {
        meetings = API.pastEvents(JUG_GROUP).stream()
                .filter(e -> e.yes_rsvp_count > 17) // Filter JugOff
                .map(MeetingRepository::toMeeting)
                .collect(Collectors.toList());

        attendeeByMeetingId = Caffeine.newBuilder()
                .maximumSize(10_000)
                .expireAfterWrite(10, TimeUnit.MINUTES)
                .build(key ->
                        API.eventAttendance(JUG_GROUP, key).stream()
                        .filter(a -> !a.member.id.equals("0"))
                        .filter(a -> a.rsvp.response.equals("yes"))
                        .filter(a -> !a.member.name.equals("Bordeaux JUG"))
                        .map(a -> new MeetingAttendee(a.member.id, a.member.name))
                        .collect(Collectors.toList()));
    }

    public List<Meeting> all() {
        return meetings;
    }

    public List<MeetingAttendee> attendees(Meeting meeting) {
        return attendeeByMeetingId.get(meeting.id());
    }

    private static Meeting toMeeting(MeetupAPI.Event e) {
        Meeting meeting = new Meeting(e.id, e.name, Instant.ofEpochMilli(e.time).atZone(ZoneId.systemDefault()).toLocalDate());
        meeting.setAttendance(e.yes_rsvp_count);
        return meeting;
    }
}

@@ -1177,16 +1177,37 @@
 te final
+ LoadingCache%3CString,
  List%3CMe
@@ -1212,16 +1212,17 @@
 Meeting%3E
+%3E
  meeting
@@ -1276,16 +1276,145 @@
 etings =
+ Caffeine.newBuilder()%0A                .expireAfterAccess(30, TimeUnit.MINUTES)%0A                .build(key -%3E%0A                   
  API.pas
@@ -1433,32 +1433,36 @@
 GROUP).stream()%0A
+    
                 
@@ -1514,16 +1514,20 @@
  JugOff%0A
+    
         
@@ -1577,32 +1577,36 @@
                 
+    
 .collect(Collect
@@ -1618,16 +1618,17 @@
 oList())
+)
 ;%0A%0A     
@@ -1678,45 +1678,8 @@
 r()%0A
-                .maximumSize(10_000)%0A
     
@@ -1706,15 +1706,16 @@
 fter
-Write(1
+Access(3
 0, T
@@ -2239,16 +2239,27 @@
 meetings
+.get(%22all%22)
 ;%0A    %7D%0A

change cache configuration