package com.example.hellorx;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.hellorx.model.PixaBay;
import com.example.hellorx.model.Quote;
import com.example.hellorx.service.API;
import com.squareup.picasso.Picasso;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    public static final String CATEGORY_MOVIES = "movies";
    public static final String CATEGORY_FAMOUS = "famous";

    @Bind(R.id.quote_textview) TextView  mQuoteTextView;
    @Bind(R.id.quote_author)   TextView  mQuoteAuthor;
    @Bind(R.id.quote_image)    ImageView mQuoteImage;

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

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.quote_button)
    public void onClickGetQuote() {
        getQuote();
    }

    private void getQuote() {
        API.quote().getQuote(CATEGORY_FAMOUS)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnNext(quote -> {
                mQuoteTextView.setText(quote.getQuote());
                mQuoteAuthor.setText(quote.getAuthor());
            })
            .flatMap(quote ->
                    API.image().getPhoto(quote.getAuthor())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
            )
            .subscribe(image -> {
                if (image != null && image.getTotalImages() > 0) {
                    mQuoteImage.setVisibility(View.VISIBLE);
                    Picasso.with(getBaseContext())
                        .load(image.getImages().get(0).getImageURL())
                        .fit()
                        .centerCrop()
                        .noFade()
                        .into(mQuoteImage);
                }
                else {
                    mQuoteImage.setVisibility(View.GONE);
                }
            }, e -> {
                mQuoteImage.setVisibility(View.GONE);
                Log.e(TAG, e.getMessage());
            });
    }

    private void getQuoteCombind() {
        API.quote().getQuote(CATEGORY_FAMOUS)
            .flatMap(quote -> API.image().getPhoto(quote.getAuthor())
                    .map((Func1<PixaBay, Quote>) pixaBay -> {
                        if (pixaBay.getTotalImages() > 0) {
                            quote.setAuthorImage(pixaBay.getImages().get(0));
                        }
                        return quote;
                    })
            )
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(quote -> {
                mQuoteTextView.setText(quote.getQuote());
                mQuoteAuthor.setText(quote.getAuthor());

                if (quote.getAuthorImage() != null) {
                    mQuoteImage.setVisibility(View.VISIBLE);
                    Picasso.with(getBaseContext())
                        .load(quote.getAuthorImage().getImageURL())
                        .fit()
                        .centerCrop()
                        .into(mQuoteImage);
                }
                else {
                    mQuoteImage.setVisibility(View.GONE);
                }
            }, e -> {
                mQuoteImage.setVisibility(View.GONE);
                Log.e(TAG, e.getMessage());
            });
    }
}


@@ -1615,147 +1615,29 @@
 ext(
-quote -%3E %7B%0A                mQuoteTextView.setText(quote.getQuote());%0A                mQuoteAuthor.setText(quote.getAuthor());%0A            %7D
+this::updateQuoteView
 )%0A  
@@ -1663,36 +1663,16 @@
 quote -%3E
-%0A                   
  API.ima
@@ -1716,32 +1716,24 @@
             
-        
 .subscribeOn
@@ -1762,32 +1762,24 @@
             
-    
-    
 .observeOn(A
@@ -1800,32 +1800,33 @@
 rs.mainThread())
+)
 %0A            )%0A 
@@ -1826,52 +1826,410 @@
     
-)
+.subscribe(this::showQuoteImage, this::hideQuoteImage);
 %0A    
+%7D%0A%0A
     
-    .subscribe(image -%3E %7B%0A        
+private void updateQuoteView(Quote quote) %7B%0A        mQuoteTextView.setText(quote.getQuote());%0A        mQuoteAuthor.setText(quote.getAuthor());%0A    %7D%0A%0A    private void hideQuoteImage(Throwable e) %7B%0A        mQuoteImage.setVisibility(View.GONE);%0A        Log.e(TAG, e.getMessage());%0A    %7D%0A%0A    private void showQuoteImage(PixaBay image) %7B%0A
     
@@ -2291,32 +2291,24 @@
             
-        
 mQuoteImage.
@@ -2344,32 +2344,24 @@
             
- 
-       
 Picasso.with
@@ -2375,32 +2375,24 @@
 eContext())%0A
-        
             
@@ -2453,32 +2453,24 @@
             
-        
 .fit()%0A     
@@ -2476,32 +2476,24 @@
             
- 
-       
 .centerCrop(
@@ -2510,24 +2510,16 @@
         
-     
-   
 .noFade(
@@ -2520,24 +2520,16 @@
 oFade()%0A
-        
         
@@ -2564,39 +2564,26 @@
         
- 
-       %7D%0A     
+%7D%0A
         
    else 
@@ -2574,34 +2574,23 @@
         
-   
 else %7B%0A
-        
         
@@ -2643,153 +2643,9 @@
     
- 
-       %7D%0A            %7D, e -%3E %7B%0A                mQuoteImage.setVisibility(View.GONE);%0A                Log.e(TAG, e.getMessage());%0A            %7D);
+%7D
 %0A   
@@ -3253,104 +3253,29 @@
     
-mQuoteTextView.setText(quote.getQuote());%0A                mQuoteAuthor.setText(quote.getAuthor()
+updateQuoteView(quote
 );%0A%0A
@@ -3757,126 +3757,28 @@
  %7D, 
-e -%3E %7B%0A                mQuoteImage.setVisibility(View.GONE);%0A                Log.e(TAG, e.getMessage());%0A            %7D
+this::hideQuoteImage
 );%0A 

refactor to using lambda