package io.github.droidkaigi.confsched2017.repository.sessions;

import com.annimon.stream.Stream;

import android.text.TextUtils;

import java.util.List;

import javax.inject.Inject;

import io.github.droidkaigi.confsched2017.api.DroidKaigiClient;
import io.github.droidkaigi.confsched2017.model.Session;
import io.reactivex.Maybe;
import io.reactivex.Observable;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public final class SessionsRemoteDataSource implements SessionsDataSource {

    private final DroidKaigiClient client;

    @Inject
    public SessionsRemoteDataSource(DroidKaigiClient client) {
        this.client = client;
    }

    @Override
    public Single<List<Session>> findAll(String languageId) {
        return client.getSessions(languageId)
                .map(sessions -> {
                    // API returns some sessions which have empty room info.
                    for (Session session : sessions) {
                        if (session.room != null && TextUtils.isEmpty(session.room.name)) {
                            session.room = null;
                        }
                    }
                    return sessions;
                });
    }

    @Override
    public Maybe<Session> find(int sessionId, String languageId) {
        return findAll(languageId)
                .toObservable()
                .flatMap(Observable::fromIterable)
                .filter(session -> session.id == sessionId)
                .singleElement();
    }

    @Override
    public void updateAllAsync(List<Session> sessions) {
        // Do nothing
    }

}

@@ -62,43 +62,8 @@
 s;%0A%0A
-import com.annimon.stream.Stream;%0A%0A
 impo
@@ -354,149 +354,8 @@
 gle;
-%0Aimport io.reactivex.android.schedulers.AndroidSchedulers;%0Aimport io.reactivex.functions.Function;%0Aimport io.reactivex.schedulers.Schedulers;
 %0A%0Apu

remove unused import statements