package com.maheshgaya.android.popularmovies;

import android.content.Context;
import android.graphics.Point;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.util.Arrays;

/**
 * Copyright (c) Mahesh Gaya
 * Author: Mahesh Gaya
 * Date: 9/18/16.
 */

/**
 * MovieAdapter
 * Deals with setting images/poster/thumbnail to the gridview
 */
public class MovieAdapter extends ArrayAdapter<Movie> {
    private static final String LOG_TAG = MovieAdapter.class.getSimpleName();

    /**
     * Constructor
     * @param context
     * @param movies
     * Change movies array to List
     */
    public MovieAdapter(Context context, Movie[] movies){
        super(context, 0, Arrays.asList(movies));
        //Log.d(LOG_TAG, "MovieAdapter: " + context.getPackageName() + " : " + movies.length);
    }


    /**
     * getView
     * @param position
     * @param convertView
     * @param parent
     * @return converView
     * Deals with setting the images for the gridview
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Log.d(LOG_TAG, "getView: method executed");
        ViewHolder holder;

        Movie movie = getItem(position); //get current movie

        //initialize the thumbnail image view
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item_movie, parent, false);
            holder = new ViewHolder();
            holder.thumbnailImageView = (ImageView)convertView.findViewById(R.id.thumbnail_image_view);
            convertView.setTag(holder);
        } else{
            holder= (ViewHolder) convertView.getTag();
        }

        //use picasso to deal with getting the images and setting them to the gridview

        try {
            Picasso
                    .with(getContext())
                    .load(movie.getThumbnailURL())
                    .error(R.drawable.ic_photo_placeholder)
                    .into(holder.thumbnailImageView);
            //Log.d(LOG_TAG, "getView: Picasso executed");
        }catch (java.lang.IllegalArgumentException e){
            Log.e(LOG_TAG, "getView: " + e.getMessage(), e );
            e.printStackTrace();
        }

        return convertView;
    }

    /**
     * template for getting views about each gridview item
     */
    private static class ViewHolder {
        ImageView thumbnailImageView;
    }
}

@@ -72,16 +72,48 @@
 ontext;%0A
+import android.database.Cursor;%0A
 import a
@@ -349,24 +349,61 @@
 rayAdapter;%0A
+import android.widget.CursorAdapter;%0A
 import andro
@@ -691,27 +691,21 @@
 nds 
-Array
+Cursor
 Adapter
-%3CMovie%3E
  %7B%0A 
@@ -849,49 +849,14 @@
 ram 
-movies%0A     * Change movies array to List
+cursor
 %0A   
@@ -905,21 +905,31 @@
 xt, 
-Movie%5B%5D movie
+Cursor cursor, int flag
 s)%7B%0A
@@ -955,127 +955,21 @@
 xt, 
-0, Arrays.asList(movies));%0A        //Log.d(LOG_TAG, %22MovieAdapter: %22 + context.getPackageName() + %22 : %22 + movies.length
+cursor, flags
 );%0A 
@@ -977,190 +977,13 @@
   %7D%0A
-%0A%0A
     
-/**%0A     * getView%0A     * @param position%0A     * @param convertView%0A     * @param parent%0A     * @return converView%0A     * Deals with setting the images for the gridview%0A     */
+%0A
 %0A   
@@ -1013,318 +1013,88 @@
 iew 
-get
+new
 View(
-int position, View convertView, ViewGroup parent) %7B%0A        //Log.d(LOG_TAG, %22getView: method executed%22);%0A        ViewHolder holder;%0A%0A        Movie movie = getItem(position); //get current movie%0A%0A        //initialize the thumbnail image view%0A        if (convertView == null) %7B%0A            convertView =
+Context context, Cursor cursor, ViewGroup parent) %7B%0A        View view = 
  Lay
@@ -1110,28 +1110,23 @@
 er.from(
-getC
+c
 ontext
-()
 ).inflat
@@ -1173,29 +1173,40 @@
 e);%0A        
-    h
+ViewHolder viewH
 older = new 
@@ -1216,30 +1216,31 @@
 wHolder(
+view
 );%0A
-   
         
  holder.
@@ -1235,175 +1235,175 @@
     
- holder.thumbnailImageView = (ImageView)convertView.findViewById(R.id.thumbnail_image_
+view.setTag(viewHolder);%0A        return 
 view
-)
 ;%0A    
+%7D%0A%0A
     
-    convertView.setTag(holder);%0A        %7D else%7B%0A            h
+@Override%0A    public void bindView(View view, Context context, Cursor cursor) %7B%0A        ViewHolder viewH
 older
+ 
 = (V
@@ -1413,24 +1413,17 @@
 Holder) 
-convertV
+v
 iew.getT
@@ -1432,105 +1432,95 @@
 ();%0A
-        %7D%0A%0A        //use picasso to deal with getting the images and setting them to the gridview
+%0A        String imageUrl = %22%22; //cursor.getString(); //TODO: To replace with projection
 %0A%0A  
@@ -1581,20 +1581,15 @@
 ith(
-getC
+c
 ontext
-()
 )%0A  
@@ -1616,31 +1616,16 @@
 oad(
-movie.getThumbnailURL()
+imageUrl
 )%0A  
@@ -1708,17 +1708,21 @@
   .into(
-h
+viewH
 older.th
@@ -1962,37 +1962,8 @@
    %7D
-%0A%0A        return convertView;
 %0A   
@@ -2042,30 +2042,29 @@
     */%0A    p
-rivate
+ublic
  static clas
@@ -2111,21 +2111,165 @@
 ilImageView;
+%0A%0A        public ViewHolder(View view)%7B%0A            this.thumbnailImageView = (ImageView)view.findViewById(R.id.thumbnail_image_view);%0A        %7D
 %0A    %7D%0A%7D%0A

Refactor MovieAdapter