package com.daksh.tmdbsample.movielist;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.daksh.tmdbsample.R;
import com.daksh.tmdbsample.data.model.Movie;
import com.daksh.tmdbsample.databinding.MovieListItemBinding;

import java.util.List;

/**
 * Created by daksh on 03-Sep-16.
 */
public class MovieListAdapter extends RecyclerView.Adapter<MovieListAdapter.ViewHolder> {

    @NonNull
    private final ItemClickListener itemClickListener;
    private final boolean twoPane;
    private List<Movie> movies;

    public MovieListAdapter(@NonNull ItemClickListener itemClickListener, boolean twoPane) {
        this.itemClickListener = itemClickListener;
        this.twoPane = twoPane;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MovieListItemBinding B =
                DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                        R.layout.movie_list_item, parent, false);
        return new ViewHolder(B, itemClickListener);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.movie = movies.get(position);
        //TODO Load image using Picasso
    }

    @Override
    public int getItemCount() {
        return movies.size();
    }

    public Movie getItem(int position) {
        return movies.get(position);
    }

    public void setMovies(List<Movie> movies) {
        this.movies = movies;
        //TODO
    }

    public void addMovies(List<Movie> movies) {
        this.movies.addAll(movies);
        //TODO
    }

    public boolean isTwoPane() {
        return twoPane;
    }

    public interface ItemClickListener {
        void onItemClicked(Movie movie);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private final MovieListItemBinding B;
        private final ItemClickListener itemClickListener;
        private Movie movie;

        public ViewHolder(@NonNull MovieListItemBinding B,
                @NonNull ItemClickListener itemClickListener) {
            super(B.getRoot());
            this.B = B;
            this.itemClickListener = itemClickListener;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            itemClickListener.onItemClicked(movie);
        }
    }
}

@@ -402,16 +402,44 @@
 nding;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -892,32 +892,68 @@
 Pane = twoPane;%0A
+        movies = new ArrayList%3C%3E();%0A
     %7D%0A%0A    @Over
@@ -1064,32 +1064,16 @@
 ding B =
-%0A               
  DataBin
@@ -1131,24 +1131,16 @@
 ext()),%0A
-        
         
@@ -1487,77 +1487,62 @@
     
-return movies.size();
+if (movies == null) %7B
 %0A    
-%7D%0A%0A
     
-public Movie getItem(int position) %7B
+    return 0;%0A        %7D%0A
 %0A   
@@ -1556,36 +1556,29 @@
 turn movies.
-get(position
+size(
 );%0A    %7D%0A%0A  
@@ -1597,24 +1597,33 @@
 d setMovies(
+@NonNull 
 List%3CMovie%3E 
@@ -1662,38 +1662,74 @@
 movies;%0A        
-//TODO
+notifyItemRangeInserted(0, movies.size());
 %0A    %7D%0A%0A    publ
@@ -1746,16 +1746,25 @@
 dMovies(
+@NonNull 
 List%3CMov
@@ -1821,22 +1821,71 @@
         
-//TODO
+notifyItemRangeInserted(getItemCount(), movies.size());
 %0A    %7D%0A%0A
@@ -2143,54 +2143,8 @@
  %7B%0A%0A
-        private final MovieListItemBinding B;%0A
     
@@ -2387,32 +2387,8 @@
 ));%0A
-            this.B = B;%0A
     

Add Adapter Notification calls for insertion of Movies