package io.podcst.android.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.OrientationHelper;
import android.widget.Toast;

import com.facebook.litho.Component;
import com.facebook.litho.ComponentContext;
import com.facebook.litho.ComponentInfo;
import com.facebook.litho.LithoView;
import com.facebook.litho.widget.LinearLayoutInfo;
import com.facebook.litho.widget.Recycler;
import com.facebook.litho.widget.RecyclerBinder;

import java.util.List;

import io.podcst.android.data.Api;
import io.podcst.android.data.Podcast;
import io.podcst.android.data.PodcastsResponse;
import io.podcst.android.specs.PodcastRow;
import io.podcst.android.specs.PodcastRowCard;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by eve on 19/04/17.
 */

public class MainActivity extends AppCompatActivity {

    private Api.ApiService api;

    private RecyclerBinder recyclerBinder;
    private ComponentContext viewContext;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        api = Api.get();

        viewContext = new ComponentContext(this);

        recyclerBinder = new RecyclerBinder(
                viewContext,
                new LinearLayoutInfo(
                        getApplicationContext(),
                        OrientationHelper.VERTICAL,
                        false
                ));

        final Component component = Recycler.create(viewContext)
                .binder(recyclerBinder)
                .build();

        final LithoView lithoView = LithoView.create(
                this,
                component);

        setContentView(lithoView);
    }

    private static void populateList(RecyclerBinder recyclerBinder,
                                     ComponentContext c,
                                     List<Podcast> podcasts) {
        int i = 0;
        for (Podcast podcast : podcasts) {
            ComponentInfo.Builder componentInfoBuilder = ComponentInfo.create();
            componentInfoBuilder.component(
                    PodcastRowCard.create(c)
                        .podcast(podcast)
                        .build());

            recyclerBinder.insertItemAt(i++,  componentInfoBuilder.build());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        api.getFeaturedPodcasts()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<PodcastsResponse>() {
                    @Override
                    public void onNext(PodcastsResponse value) {
                        populateList(recyclerBinder, viewContext, value.data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(getApplicationContext(),
                                "Could not load podcasts",
                                Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }
}

@@ -678,51 +678,8 @@
 se;%0A
-import io.podcst.android.specs.PodcastRow;%0A
 impo
@@ -1822,16 +1822,41 @@
 hoView);
+%0A%0A        loadPodcasts();
 %0A    %7D%0A%0A
@@ -2455,76 +2455,37 @@
     
-@Override%0A    protec
+priva
 te
-d
  void 
-onResume() %7B%0A        super.onResume();%0A
+loadPodcasts() %7B
 %0A   

Load podcasts in onCreate