package com.veyndan.hermes.home;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.ryanharter.auto.value.moshi.AutoValueMoshiAdapterFactory;
import com.squareup.moshi.Moshi;
import com.veyndan.hermes.BaseActivity;
import com.veyndan.hermes.Comic;
import com.veyndan.hermes.R;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.moshi.MoshiConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class HomeActivity extends BaseActivity {

    @BindView(R.id.recycler_view) RecyclerView recyclerView;

    private final List<Comic> comics = new ArrayList<>();
    private HomeAdapter adapter;

    private Moshi moshi = new Moshi.Builder().add(new AutoValueMoshiAdapterFactory()).build();

    private Retrofit retrofit = new Retrofit.Builder()
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(MoshiConverterFactory.create(moshi))
            .baseUrl("https://xkcd.com/")
            .build();

    private interface XKCDService {
        @GET("info.0.json")
        Observable<Comic> latest();

        @GET("{num}/info.0.json")
        Observable<Comic> num(@Path("num") int num);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity);
        ButterKnife.bind(this);

        adapter = new HomeAdapter(comics);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        final XKCDService xkcdService = retrofit.create(XKCDService.class);
        Observable<Comic> latest = xkcdService.latest();

        latest.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Comic>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(Comic comic) {
                        for (int num = comic.num(); num > 0; num--) {
                            if (num == 404) continue;
                            xkcdService.num(num).subscribeOn(Schedulers.newThread())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Subscriber<Comic>() {
                                        @Override
                                        public void onCompleted() {
                                            Log.d("_veyndan", "completed");
                                        }

                                        @Override
                                        public void onError(Throwable e) {
                                            Log.e("_veyndan", e.getMessage(), e);
                                        }

                                        @Override
                                        public void onNext(Comic comic) {
                                            Log.d("_veyndan", comic.toString());
                                            comics.add(comic);
                                            adapter.notifyDataSetChanged();
                                        }
                                    });
                        }
                    }
                });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
            default:
                return false;
        }
    }
}

@@ -158,33 +158,8 @@
 ew;%0A
-import android.util.Log;%0A
 impo
@@ -751,30 +751,8 @@
 le;%0A
-import rx.Subscriber;%0A
 impo
@@ -795,16 +795,72 @@
 dulers;%0A
+import rx.functions.Action1;%0Aimport rx.functions.Func1;%0A
 import r
@@ -2069,24 +2069,25 @@
 class);%0A
+%0A
         
 Observab
@@ -2082,209 +2082,89 @@
     
-Observable%3CComic%3E latest = xkcdService.latest();%0A%0A        latest.subscribeOn(Schedulers.io())%0A                .observeOn(AndroidSchedulers.mainThread())%0A                .subscribe(new Subscriber
+xkcdService.latest()%0A                .flatMap(new Func1%3CComic, Observable
 %3CComic%3E
+%3E
 () %7B
@@ -2221,33 +2221,50 @@
  public 
-void onCompleted(
+Observable%3CComic%3E call(Comic comic
 ) %7B%0A    
@@ -2270,32 +2270,24 @@
             
-     %7D%0A%0A
             
@@ -2287,190 +2287,58 @@
     
-           @Override%0A                    public void onError(Throwable e) %7B%0A                    %7D%0A%0A                    @Override%0A                    public void onNext(Comic comic) %7B
+Observable%3CComic%3E observable = Observable.empty();
 %0A   
@@ -2392,16 +2392,32 @@
 ; num %3E 
+comic.num() - 10
 0; num--
@@ -2452,270 +2452,142 @@
     
-if (num == 404) continue;%0A                            xkcdService.num(num).subscribeOn(Schedulers.newThread())%0A                                    .observeOn(AndroidSchedulers.mainThread())%0A                                    .subscribe(new Subscriber%3CComic%3E() %7B
+observable = Observable.concat(observable, xkcdService.num(num));%0A                        %7D%0A                        return observable;
 %0A   
@@ -2595,32 +2595,34 @@
                 
+%7D%0A
                 
@@ -2617,37 +2617,26 @@
             
-    @Override
+%7D)
 %0A           
@@ -2644,437 +2644,147 @@
     
-                        public void onCompleted() %7B%0A                                            Log.d(%22_veyndan%22, %22completed%22);%0A                                        %7D%0A%0A                                        @Override%0A                                        public void onError(Throwable e) %7B%0A                                            Log.e(%22_veyndan%22, e.getMessage(), e);%0A                                        %7D%0A%0A        
+.subscribeOn(Schedulers.io())%0A                .observeOn(AndroidSchedulers.mainThread())%0A                .subscribe(new Action1%3CComic%3E() %7B%0A
     
@@ -2795,36 +2795,24 @@
             
-            
 @Override%0A  
@@ -2829,36 +2829,16 @@
         
-         
-           
 public v
@@ -2845,14 +2845,12 @@
 oid 
-onNext
+call
 (Com
@@ -2865,109 +2865,8 @@
 ) %7B%0A
-                                            Log.d(%22_veyndan%22, comic.toString());%0A                    
     
@@ -2928,36 +2928,16 @@
         
-   
-                 
 adapter.
@@ -2964,116 +2964,8 @@
 ();%0A
-                                        %7D%0A                                    %7D);%0A                        %7D%0A
     

Use flatMap to infer latest comic number