package com.example.prakhar.movieapp.widgets.people_detail;

import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.prakhar.movieapp.R;
import com.example.prakhar.movieapp.model.person_search.KnownFor;
import com.example.prakhar.movieapp.utils.Constants;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Prakhar on 4/4/2017.
 */

public class KnownForAdapter extends BaseAdapter {

    @BindView(R.id.people_known_for_card)
    CardView movieCard;
    @BindView(R.id.movie_know_for_poster)
    ImageView moviePoster;
    @BindView(R.id.movie_known_for_title)
    TextView movieTitle;
    @BindView(R.id.movie_known_for_rating)
    TextView movieRating;

    private List<KnownFor> knownForList;
    private KnownForListener listener;

    public KnownForAdapter(List<KnownFor> knownFors, KnownForListener knownForListener) {
        knownForList = new ArrayList<>();
        knownForList.addAll(knownFors);
        listener = knownForListener;
    }

    @Override
    public int getCount() {
        return knownForList.size();
    }

    @Override
    public Object getItem(int position) {
        return knownForList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.people_credits_acting_item, null);
        }
        ButterKnife.bind(this, convertView);

        Glide.with(moviePoster.getContext())
                .load(Constants.TMDB_IMAGE_URL + "w185" + knownForList.get(position).getPosterPath())
                .placeholder(R.drawable.movie_poster_placeholder)
                .crossFade()
                .into(moviePoster);

        if (knownForList.get(position).getMediaType().equals("movie")) {
            movieTitle.setText(knownForList.get(position).getTitle());
        } else {
            movieTitle.setText(knownForList.get(position).getName());
        }

        if (knownForList.get(position).getVoteAverage() != 0.0) {
            movieRating.setText(String.format(Locale.US, "%.1f",
                    knownForList.get(position).getVoteAverage()));
        } else {
            movieRating.setText("-");
        }

        movieCard.setOnClickListener(v -> listener.knownForMovieClicked(
                knownForList.get(position).getId(), position));

        return convertView;
    }

    public interface KnownForListener {
        void knownForMovieClicked(Integer movieId, int clickedPosition);
    }
}

@@ -54,16 +54,59 @@
 etail;%0A%0A
+import android.support.v4.view.ViewCompat;%0A
 import a
@@ -1118,23 +1118,16 @@
 r;%0A%0A    
-public 
 KnownFor
@@ -1860,26 +1860,21 @@
  null);%0A
-   
+%0A
     
- %7D%0A
         
@@ -1907,24 +1907,281 @@
 vertView);%0A%0A
+            final ViewHolder viewHolder =%0A                    new ViewHolder(movieCard, moviePoster, movieTitle, movieRating);%0A%0A            convertView.setTag(viewHolder);%0A        %7D%0A%0A        final ViewHolder viewHolder = (ViewHolder) convertView.getTag();%0A%0A
         Glid
@@ -2402,53 +2402,158 @@
    .
-crossFade()%0A                .into(moviePoster
+into(viewHolder.moviePoster);%0A%0A        ViewCompat.setTransitionName(viewHolder.moviePoster,%0A                knownForList.get(position).getPosterPath()
 );%0A%0A
@@ -3191,16 +3191,40 @@
 position
+, viewHolder.moviePoster
 ));%0A%0A   
@@ -3251,24 +3251,501 @@
 iew;%0A    %7D%0A%0A
+    private class ViewHolder %7B%0A        CardView movieCard;%0A        ImageView moviePoster;%0A        TextView movieTitle;%0A        TextView movieRating;%0A%0A        public ViewHolder(CardView movieCard, ImageView moviePoster, TextView movieTitle,%0A                          TextView movieRating) %7B%0A            this.movieCard = movieCard;%0A            this.moviePoster = moviePoster;%0A            this.movieTitle = movieTitle;%0A            this.movieRating = movieRating;%0A        %7D%0A    %7D%0A%0A
     public i
@@ -3838,23 +3838,50 @@
 ckedPosition
+, ImageView sharedImageView
 );%0A    %7D%0A%7D%0A

Add view holder pattern in people known for adapter