package com.anotherdev.stackapp.app;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;

import com.anotherdev.stackapp.R;
import com.anotherdev.stackapp.adapter.AnswerAdapter;
import com.anotherdev.stackapp.api.stackexchange.Answer;
import com.anotherdev.stackapp.api.stackexchange.Question;
import com.anotherdev.stackapp.intent.DetailIntent;
import com.google.common.collect.Lists;

import butterknife.Bind;

public class DetailActivity extends StackActivity {

    @Bind(R.id.recyclerview) RecyclerView mRecyclerView;


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

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        Question q = DetailIntent.getQuestion(getIntent());
        AnswerAdapter adapter = new AnswerAdapter(q, Lists.<Answer>newArrayList());
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    protected int getActivityLayout() {
        return R.layout.activity_detail;
    }

    @Override
    protected void setupSupportActionBar(@NonNull ActionBar actionBar) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        final int itemId = item.getItemId();
        if (android.R.id.home == itemId) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

@@ -454,87 +454,316 @@
 api.
-stackexchange.Answer;%0Aimport com.anotherdev.stackapp.api.stackexchange.Question
+ApiComponent;%0Aimport com.anotherdev.stackapp.api.DaggerApiComponent;%0Aimport com.anotherdev.stackapp.api.stackexchange.Answer;%0Aimport com.anotherdev.stackapp.api.stackexchange.Answers;%0Aimport com.anotherdev.stackapp.api.stackexchange.Question;%0Aimport com.anotherdev.stackapp.api.stackexchange.StackOverflowApi
 ;%0Aim
@@ -877,16 +877,93 @@
 fe.Bind;
+%0Aimport rx.android.schedulers.AndroidSchedulers;%0Aimport rx.functions.Action1;
 %0A%0Apublic
@@ -1009,16 +1009,62 @@
 vity %7B%0A%0A
+    private StackOverflowApi mStackOverflow;%0A%0A
     @Bin
@@ -1228,16 +1228,136 @@
 eState);
+%0A        ApiComponent apiComponent = DaggerApiComponent.create();%0A        mStackOverflow = apiComponent.stackoverflow();
 %0A%0A      
@@ -1674,16 +1674,41 @@
 dapter);
+%0A%0A        loadAnswers(q);
 %0A    %7D%0A%0A
@@ -2226,11 +2226,603 @@
 );%0A    %7D
+%0A%0A    private void loadAnswers(final Question question) %7B%0A        mStackOverflow.answers(question.getId())%0A                .observeOn(AndroidSchedulers.mainThread())%0A                .subscribe(%0A                        new Action1%3CAnswers%3E() %7B%0A                            @Override%0A                            public void call(Answers answers) %7B%0A                                AnswerAdapter adapter = new AnswerAdapter(question, answers.get());%0A                                mRecyclerView.setAdapter(adapter);%0A                            %7D%0A                        %7D%0A                );%0A    %7D
 %0A%7D%0A

load answers