package com.veyndan.hermes.home;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
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);

        XKCDService xkcdService = retrofit.create(XKCDService.class);
        Observable<Comic> latest = xkcdService.latest();
        Observable<Comic> first = xkcdService.num(1);

        List<Observable<Comic>> comicsObservables = new ArrayList<>();
        comicsObservables.add(latest);
        comicsObservables.add(first);

        Observable.merge(comicsObservables)
                .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) {
                        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;
        }
    }
}

@@ -150,24 +150,49 @@
 cyclerView;%0A
+import android.util.Log;%0A
 import andro
@@ -1992,16 +1992,22 @@
         
+final 
 XKCDServ
@@ -2121,272 +2121,23 @@
 ();%0A
-        Observable%3CComic%3E first = xkcdService.num(1);%0A%0A        List%3CObservable%3CComic%3E%3E comicsObservables = new ArrayList%3C%3E();%0A        comicsObservables.add(latest);%0A        comicsObservables.add(first);%0A%0A        Observable.merge(comicsObservables)%0A                
+%0A        latest
 .sub
@@ -2348,25 +2348,24 @@
 mpleted() %7B%0A
-%0A
             
@@ -2460,17 +2460,16 @@
 le e) %7B%0A
-%0A
         
@@ -2595,82 +2595,1283 @@
     
-comics.add(comic);%0A                        adapter.notifyDataSetChanged();
+for (int num = comic.num(); num %3E 0; num--) %7B%0A                            if (num == 404) continue;%0A                            xkcdService.num(num).subscribeOn(Schedulers.newThread())%0A                                    .observeOn(AndroidSchedulers.mainThread())%0A                                    .subscribe(new Subscriber%3CComic%3E() %7B%0A                                        @Override%0A                                        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                                        @Override%0A                                        public void onNext(Comic comic) %7B%0A                                            Log.d(%22_veyndan%22, comic.toString());%0A                                            comics.add(comic);%0A                                            adapter.notifyDataSetChanged();%0A                                        %7D%0A                                    %7D);%0A                        %7D
 %0A   

Load all comics into recycler view