package net.squanchy.eventdetails;

import net.squanchy.schedule.domain.view.Event;
import net.squanchy.service.firebase.FirebaseSquanchyRepository;
import net.squanchy.service.firebase.model.FirebaseEvent;
import net.squanchy.service.firebase.model.FirebaseSpeakers;

import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;

import static net.squanchy.support.lang.Lists.find;
import static net.squanchy.support.lang.Lists.map;

class EventDetailsService {

    private final FirebaseSquanchyRepository repository;

    EventDetailsService(FirebaseSquanchyRepository repository) {
        this.repository = repository;
    }

    public Observable<Event> event(int dayId, int eventId) {
        Observable<FirebaseEvent> eventObservable = repository.event(dayId, eventId);
        Observable<FirebaseSpeakers> speakersObservable = repository.speakers();

        return Observable.combineLatest(
                eventObservable,
                speakersObservable,
                (firebaseEvent, speakerHolder) -> Event.create(
                        firebaseEvent.eventId,
                        dayId,
                        firebaseEvent.name,
                        firebaseEvent.place,
                        firebaseEvent.experienceLevel,
                        map(
                                map(firebaseEvent.speakers, speakerId -> find(
                                        speakerHolder.speakers, speaker -> speaker.speakerId.equals(speakerId))
                                ),
                                speaker -> speaker == null ? "" : speaker.firstName + " " + speaker.lastName
                        )
                )
        ).subscribeOn(Schedulers.io());
    }
}

@@ -25,24 +25,48 @@
 ntdetails;%0A%0A
+import java.util.List;%0A%0A
 import net.s
@@ -220,24 +220,84 @@
 ebaseEvent;%0A
+import net.squanchy.service.firebase.model.FirebaseSpeaker;%0A
 import net.s
@@ -349,39 +349,121 @@
 rs;%0A
-%0Aimport io.reactivex.Observable
+import net.squanchy.support.lang.Lists;%0A%0Aimport io.reactivex.Observable;%0Aimport io.reactivex.functions.BiFunction
 ;%0Aim
@@ -1164,67 +1164,216 @@
     
-(firebaseEvent, speakerHolder) -%3E Event.create(%0A           
+combineIntoEvent(dayId)%0A        ).subscribeOn(Schedulers.io());%0A    %7D%0A%0A    private BiFunction%3CFirebaseEvent, FirebaseSpeakers, Event%3E combineIntoEvent(int dayId) %7B%0A        return (apiEvent, apiSpeakers) -%3E %7B%0A
     
@@ -1384,67 +1384,180 @@
     
- f
+List%3CF
 irebase
-Event.eventId,%0A                        dayId,%0A    
+Speaker%3E speakers = speakersForEvent(apiEvent, apiSpeakers);%0A            return Event.create(%0A                    apiEvent.eventId,%0A                    dayId,%0A
     
@@ -1568,32 +1568,27 @@
             
-firebase
+api
 Event.name,%0A
@@ -1603,36 +1603,27 @@
             
-    firebase
+api
 Event.place,
@@ -1647,20 +1647,11 @@
     
-    firebase
+api
 Even
@@ -1689,24 +1689,46 @@
         
-    
 map(
+speakers, toSpeakerName())
 %0A       
@@ -1728,32 +1728,35 @@
             
+);%0A
         
             
@@ -1747,120 +1747,340 @@
     
+%7D;%0A
     
+%7D%0A%0A
     
-    map(firebaseEvent.speakers, speakerId -%3E find(%0A                                        speakerHolder
+private List%3CFirebaseSpeaker%3E speakersForEvent(FirebaseEvent apiEvent, FirebaseSpeakers apiSpeakers) %7B%0A        return map(apiEvent.speakers, speakerId -%3E findSpeaker(apiSpeakers, speakerId));%0A    %7D%0A%0A    private FirebaseSpeaker findSpeaker(FirebaseSpeakers apiSpeakers, long speakerId) %7B%0A        return find(apiSpeakers
 .spe
@@ -2082,25 +2082,28 @@
 s.speakers, 
-s
+apiS
 peaker -%3E sp
@@ -2096,25 +2096,28 @@
 iSpeaker -%3E 
-s
+apiS
 peaker.speak
@@ -2143,77 +2143,106 @@
 Id))
+;
 %0A
-  
     
-                          ),%0A                                s
+%7D%0A%0A    private Lists.Function%3CFirebaseSpeaker, String%3E toSpeakerName() %7B%0A        return apiS
 peak
@@ -2251,17 +2251,20 @@
  -%3E 
-s
+apiS
 peaker 
-=
+!
 = nu
@@ -2272,14 +2272,12 @@
 l ? 
-%22%22 : s
+apiS
 peak
@@ -2297,17 +2297,20 @@
 + %22 %22 + 
-s
+apiS
 peaker.l
@@ -2320,91 +2320,15 @@
 Name
-%0A                        )%0A                )%0A        ).subscribeOn(Schedulers.io())
+ : null
 ;%0A  

Rename variables in EventDetailsService