package com.malouane.popularmovies.ui.detail;

import android.arch.lifecycle.LifecycleRegistry;
import android.arch.lifecycle.LifecycleRegistryOwner;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import com.malouane.popularmovies.R;
import com.malouane.popularmovies.databinding.ActivityMovieDetailBinding;

import javax.inject.Inject;

import dagger.android.AndroidInjection;

public class MovieDetailActivity extends AppCompatActivity implements LifecycleRegistryOwner {

  private static final String KEY_MOVIE_ID = "KEY_MOVIE_ID";
  private static final String KEY_MOVIE_POSTER = "KEY_MOVIE_POSTER";

  LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this);
  ActivityMovieDetailBinding binding;

  @Inject MovieDetailViewModel movieDetailViewModel;

  MovieReviewsAdapter movieReviewsAdapter;

  public static Intent newIntent(Context context, int movieID, String posterPath) {
    Intent intent = new Intent(context, MovieDetailActivity.class);
    intent.putExtra(KEY_MOVIE_ID, movieID);
    intent.putExtra(KEY_MOVIE_POSTER, posterPath);
    return intent;
  }

  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    AndroidInjection.inject(this);
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_movie_detail);

    setSupportActionBar(binding.toolbar);
    assert getSupportActionBar() != null;
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
      if (bundle.containsKey(KEY_MOVIE_POSTER) && bundle.containsKey(KEY_MOVIE_ID)) {
        binding.setPosterPath(getIntent().getStringExtra(KEY_MOVIE_POSTER));
        movieDetailViewModel.getMovieWithId(getIntent().getIntExtra(KEY_MOVIE_ID, 0))
                .observe(this, movie -> binding.setMovie(movie));
      }
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        ActivityCompat.finishAfterTransition(this);
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public LifecycleRegistry getLifecycle() {
    return lifecycleRegistry;
  }
}

@@ -1778,24 +1778,25 @@
  != null) %7B%0A
+%0A
       if (bu
@@ -1865,24 +1865,324 @@
 OVIE_ID)) %7B%0A
+%0A        movieDetailViewModel.isFavorite().observe(this, isFavorite -%3E %7B%0A          if (isFavorite == null) isFavorite = false;%0A          binding.favoriteFab.setImageResource(isFavorite ? R.drawable.ic_favorite_border_black_24dp : R.drawable.ic_favorite_black_24dp);%0A          initFAB();%0A        %7D);%0A%0A
         bind
@@ -2279,20 +2279,16 @@
 del.
-g
+s
 etMovie
-With
 Id(g
@@ -2327,16 +2327,64 @@
 _ID, 0))
+;%0A        movieDetailViewModel.getMovieDetails()
 %0A       
@@ -2457,24 +2457,236 @@
 %0A    %7D%0A  %7D%0A%0A
+  private void initFAB() %7B%0A    binding.favoriteFab.setOnClickListener(view1 -%3E%0A            movieDetailViewModel.performFavorites(binding.getMovie()).observe(this, aBoolean -%3E movieDetailViewModel.retry()));%0A  %7D%0A%0A
   @Override 

handle favorite click and state