package net.squanchy.contest;

import net.squanchy.remoteconfig.RemoteConfig;
import net.squanchy.service.firebase.FirebaseAuthService;
import net.squanchy.service.firebase.FirebaseDbService;
import net.squanchy.service.firebase.model.FirebaseAchievements;

import io.fabric.sdk.android.services.common.SystemCurrentTimeProvider;
import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.Single;
import io.reactivex.functions.BiFunction;
import io.reactivex.schedulers.Schedulers;

public class ContestService {

    private final RemoteConfig remoteConfig;
    private final FirebaseDbService dbService;
    private final FirebaseAuthService authService;

    public ContestService(RemoteConfig remoteConfig, FirebaseDbService dbService, FirebaseAuthService authService) {
        this.remoteConfig = remoteConfig;
        this.dbService = dbService;
        this.authService = authService;
    }

    public Single<ContestStandings> standings() {
        return authService.ifUserSignedInThenObservableFrom(dbService::achievements)
                .first(FirebaseAchievements.empty())
                .zipWith(remoteConfig.contestGoal(), buildStandings())
                .subscribeOn(Schedulers.io());
    }

    private BiFunction<FirebaseAchievements, Integer, ContestStandings> buildStandings() {
        return (achievements, goal) -> ContestStandings.create(goal, achievements.achievements.size(), "");
    }

    public Single<ContestStandings> addAchievement(String checkpointId) {
        return authService.ifUserSignedInThenCompletableFrom(
                userId -> dbService.addAchievement(checkpointId, userId, System.currentTimeMillis())
        ).andThen(standings()).subscribeOn(Schedulers.io());
    }
}

@@ -255,145 +255,8 @@
 s;%0A%0A
-import io.fabric.sdk.android.services.common.SystemCurrentTimeProvider;%0Aimport io.reactivex.Completable;%0Aimport io.reactivex.Observable;%0A
 impo
@@ -1272,31 +1272,18 @@
 nts.
-achievements
+map
 .size()
-, %22%22
 );%0A 
@@ -1423,33 +1423,16 @@
 bleFrom(
-%0A                
 userId -
@@ -1503,24 +1503,25 @@
 imeMillis())
+)
 %0A        ).a
@@ -1517,17 +1517,24 @@
         
-)
+        
 .andThen

reformat code