package com.tehmou.rxbookapp.viewmodels;

import com.tehmou.rxbookapp.RxBookApp;
import android.util.Log;

import com.tehmou.rxbookapp.data.DataLayer;
import com.tehmou.rxbookapp.pojo.GitHubRepository;
import com.tehmou.rxbookapp.pojo.GitHubRepositorySearch;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

import rx.Observable;
import rx.functions.Func1;
import rx.functions.FuncN;
import rx.subjects.BehaviorSubject;
import rx.subjects.Subject;
import rx.subscriptions.CompositeSubscription;


/**
 * Created by ttuo on 19/03/14.
 */
public class RepositoriesViewModel {
    private static final String TAG = RepositoriesViewModel.class.getSimpleName();
    final private CompositeSubscription compositeSubscription = new CompositeSubscription();

    @Inject
    DataLayer dataLayer;

    final private String search;

    final private Subject<List<GitHubRepository>, List<GitHubRepository>> repositories
            = BehaviorSubject.create();

    public Observable<List<GitHubRepository>> getRepositories() {
        return repositories;
    }

    public RepositoriesViewModel(final String search) {
        RxBookApp.getInstance().getGraph().inject(this);
        this.search = search;
    }

    public void subscribeToDataStore() {
        compositeSubscription.add(
                dataLayer.getGitHubRepositorySearch(search)
                        .flatMap((repositorySearch) -> {
                            final List<Observable<GitHubRepository>> observables = new ArrayList<>();
                            for (int repositoryId : repositorySearch.getItems()) {
                                Log.d(TAG, "Process repositoryId: " + repositoryId);
                                final Observable<GitHubRepository> observable =
                                        dataLayer.getGitHubRepository(repositoryId);
                                observables.add(observable);
                            }
                            return Observable.combineLatest(
                                    observables,
                                    (args) -> {
                                        Log.v(TAG, "Combine items into a list");
                                        final List<GitHubRepository> list = new ArrayList<>();
                                        for (Object repository : args) {
                                            list.add((GitHubRepository) repository);
                                        }
                                        return list;
                                    }
                            );
                        })
                        .doOnNext((repositories) ->
                                Log.d(TAG, "Publishing " + repositories.size() + " repositories from the ViewModel"))
                        .subscribe(repositories));
    }

    public void unsubscribeFromDataStore() {
        compositeSubscription.clear();
    }
}

@@ -251,16 +251,67 @@
 ySearch;
+%0Aimport com.tehmou.rxbookapp.view.RepositoriesView;
 %0A%0Aimport
@@ -1268,16 +1268,61 @@
 (this);%0A
+        Log.v(TAG, %22RepositoriesViewModel%22);%0A
         
@@ -1383,32 +1383,76 @@
 ToDataStore() %7B%0A
+        Log.v(TAG, %22subscribeToDataStore%22);%0A
         composit
@@ -2825,16 +2825,17 @@
    .
-doOnNext
+subscribe
 ((re
@@ -2848,21 +2848,19 @@
 ries) -%3E
-%0A    
+ %7B%0A
         
@@ -2963,17 +2963,17 @@
 wModel%22)
-)
+;
 %0A       
@@ -2993,31 +2993,99 @@
     
-.subscribe(repositories
+    RepositoriesViewModel.this.repositories.onNext(repositories);%0A                        %7D
 ));%0A
@@ -3128,32 +3128,78 @@
 omDataStore() %7B%0A
+        Log.v(TAG, %22unsubscribeToDataStore%22);%0A
         composit

Add log to RepositoriesViewModel