package com.example.omgandroid;

import android.content.Context;
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.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONObject;

public class JSONAdapter extends BaseAdapter {

    private static final String IMAGE_URL_BASE
            = "http://covers.openlibrary.org/b/id/";

    Context mContext;
    LayoutInflater mInflater;
    JSONArray mJsonArray;

    public JSONAdapter(Context context,
                       LayoutInflater inflater) {

        mContext = context;
        mInflater = inflater;
        mJsonArray = new JSONArray();
    }

    public void updateData(JSONArray jsonArray) {

        // update the adapter's dataset
        mJsonArray = jsonArray;
        notifyDataSetChanged();
    }

    @Override public int getCount() {
        return mJsonArray.length();
    }

    @Override public JSONObject getItem(int position) {
        return mJsonArray.optJSONObject(position);
    }

    @Override public long getItemId(int position) {

        // your particular dataset uses String IDs
        // but you have to put something in this method
        return 0;
    }

    @Override public View getView(int position, View convertView, ViewGroup parent) {

        // if the view already exists, no need to inflate again!
        if (convertView == null) {

            // Inflate the custom row layout from your XML.
            convertView = mInflater.inflate(R.layout.row_book, null);
        }

        // Initialize the three views you will be populating
        ImageView thumbnailImageView = (ImageView) convertView.findViewById(R.id.img_thumbnail);
        TextView titleTextView = (TextView) convertView.findViewById(R.id.text_title);
        TextView authorTextView = (TextView) convertView.findViewById(R.id.text_author);

        // Get the current book's data in JSON form
        JSONObject jsonObject = getItem(position);

        // See if there is a cover ID in the Object
        if (jsonObject.has("cover_i")) {

            // If so, grab the Cover ID out from the object
            String imageID = jsonObject.optString("cover_i");

            // Construct the image URL (specific to API)
            String imageURL = IMAGE_URL_BASE
                    + imageID
                    + "-S.jpg";

            // Use Picasso to load the image
            // Temporarily have a placeholder in case it's slow to load
            Picasso.with(mContext)
                    .load(imageURL)
                    .placeholder(R.drawable.ic_books)
                    .into(thumbnailImageView);
        } else {

            // If there is no cover ID in the object, use a placeholder
            thumbnailImageView.setImageResource(R.drawable.ic_books);
        }

        // Grab the title and author from the JSON
        String bookTitle = "";
        String authorName = "";

        if (jsonObject.has("title")) {
            bookTitle = jsonObject.optString("title");
        }

        if (jsonObject.has("author_name")) {
            authorName = jsonObject.optJSONArray("author_name")
                    .optString(0);
        }

        // Send these Strings to the TextViews for display
        titleTextView.setText(bookTitle);
        authorTextView.setText(authorName);

        return convertView;
    }
}
@@ -1302,32 +1302,303 @@
 eturn 0;%0A    %7D%0A%0A
+    // this is used so you only ever have to do%0A    // inflation and finding by ID once ever per View%0A    private static class ViewHolder %7B%0A        public ImageView thumbnailImageView;%0A        public TextView titleTextView;%0A        public TextView authorTextView;%0A    %7D%0A%0A
     @Override pu
@@ -1664,24 +1664,52 @@
  parent) %7B%0A%0A
+        ViewHolder holder;%0A%0A
         // i
@@ -1754,16 +1754,29 @@
 inflate 
+and findById 
 again!%0A 
@@ -1940,27 +1940,21 @@
  null);%0A
-    
+%0A
     
-%7D%0A%0A
         
@@ -1960,96 +1960,142 @@
  // 
-Initialize the three views you will be populating%0A        ImageView thumbnailImageView =
+create a new %22Holder%22 with subviews%0A            holder = new ViewHolder();%0A            holder.thumbnailImageView =%0A                   
  (Im
@@ -2157,25 +2157,27 @@
         
-TextView 
+    holder.
 titleTex
@@ -2175,32 +2175,52 @@
 .titleTextView =
+%0A                   
  (TextView) conv
@@ -2266,25 +2266,27 @@
         
-TextView 
+    holder.
 authorTe
@@ -2293,16 +2293,36 @@
 xtView =
+%0A                   
  (TextVi
@@ -2366,24 +2366,311 @@
 t_author);%0A%0A
+            // hang onto this holder for future recyclage%0A            convertView.setTag(holder);%0A        %7D else %7B%0A%0A            // skip all the inflation/findByIds%0A            // and just get the holder you already made%0A            holder = (ViewHolder) convertView.getTag();%0A        %7D%0A%0A
         // G
@@ -3411,16 +3411,23 @@
   .into(
+holder.
 thumbnai
@@ -3541,16 +3541,23 @@
         
+holder.
 thumbnai
@@ -4059,16 +4059,23 @@
         
+holder.
 titleTex
@@ -4104,24 +4104,31 @@
 e);%0A        
+holder.
 authorTextVi

implement viewholder pattern