package com.moac.android.opensecretsanta.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.moac.android.opensecretsanta.R;
import com.moac.android.opensecretsanta.model.Member;
import com.moac.android.opensecretsanta.util.ContactUtils;

import java.util.List;

public class MemberListAdapter extends ArrayAdapter<Member> {

    private final String TAG = MemberListAdapter.class.getSimpleName();

    private int mResource;

    public MemberListAdapter(Context _context, int _resource,
                             List<Member> _members) {
        super(_context, _resource, _members);
        mResource = _resource;
    }

    @Override
    public View getView(int _position, View _convertView, ViewGroup _parent) {

        Log.v(TAG, "getView() - creating for position: " + _position);

        View v = _convertView;
        ImageView avatarView;
        TextView memberNameView;
        TextView contactAddressView;
        TextView restrictionsView;

        // Attempt to reuse recycled view if possible
        // Refer - http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
        // More up-to-date info here - http://www.piwai.info/android-adapter-good-practices/ (specifically using Tag)
        // Good info on LayoutInflater here - http://stackoverflow.com/questions/5026926/making-sense-of-layoutinflater

        if(v == null) {
            v = LayoutInflater.from(getContext()).inflate(mResource, _parent, false);

            avatarView = (ImageView) v.findViewById(R.id.member_imageview);
            memberNameView = (TextView) v.findViewById(R.id.member_name_textview);
            contactAddressView = (TextView) v.findViewById(R.id.contact_address_textview);
            restrictionsView = (TextView) v.findViewById(R.id.restriction_count_textview);

            v.setTag(R.id.member_imageview, avatarView);
            v.setTag(R.id.member_name_textview, memberNameView);
            v.setTag(R.id.contact_address_textview, contactAddressView);
            v.setTag(R.id.restriction_count_textview, restrictionsView);
        } else {
            // Recycled View is available, retrieve the holder instance from the View
            avatarView = (ImageView) v.getTag(R.id.member_imageview);
            memberNameView = (TextView)v.getTag(R.id.member_name_textview);
            contactAddressView = (TextView)v.getTag(R.id.contact_address_textview);
            restrictionsView = (TextView)v.getTag(R.id.restriction_count_textview);
        }

        Member item = getItem(_position);

        // Assign the view with its content.
        Drawable avatar = ContactUtils.getContactPhoto(getContext(), item.getContactId(), item.getLookupKey());
        if (avatar != null) {
            avatarView.setImageDrawable(avatar);
        } else{
            avatarView.setImageResource(R.drawable.ic_contact_picture);
        }
        memberNameView.setText(item.getName());
        contactAddressView.setText(item.getContactAddress());

        final long restrictionCount = item.getRestrictionCount();
        if(restrictionCount > 0) {
            restrictionsView.setText(String.valueOf(restrictionCount));
            restrictionsView.setVisibility(View.VISIBLE);
        } else {
            restrictionsView.setVisibility(View.GONE);
        }

        return v;
    }
}

@@ -119,16 +119,82 @@
 awable;%0A
+import android.net.Uri;%0Aimport android.provider.ContactsContract;%0A
 import a
@@ -545,25 +545,127 @@
 nta.
-util.ContactUtils
+model.PersistableObject;%0Aimport com.moac.android.opensecretsanta.util.ContactUtils;%0Aimport com.squareup.picasso.Picasso
 ;%0A%0Ai
@@ -2690,32 +2690,33 @@
 iew = (TextView)
+ 
 v.getTag(R.id.me
@@ -2771,32 +2771,33 @@
 iew = (TextView)
+ 
 v.getTag(R.id.co
@@ -2862,16 +2862,17 @@
 extView)
+ 
 v.getTag
@@ -3017,69 +3017,272 @@
     
-Drawable avatar = ContactUtils.getContactPhoto(getContext(), 
+if(item.getContactId() == PersistableObject.UNSET_ID %7C%7C item.getLookupKey() == null) %7B%0A            Picasso.with(getContext()).load(R.drawable.ic_contact_picture).into(avatarView);%0A        %7D else %7B%0A            Uri lookupUri = ContactsContract.Contacts.getLookupUri(
 item
@@ -3332,164 +3332,297 @@
     
+    
-if (avatar != null) %7B%0A            avatarView.setImageDrawable(avatar);%0A        %7D else%7B%0A            avatarView.setImageResource(R.drawable.ic_contact_picture
+Uri contactUri = ContactsContract.Contacts.lookupContact(getContext().getContentResolver(), lookupUri);%0A            Picasso.with(getContext()).load(contactUri)%0A              .placeholder(R.drawable.ic_contact_picture).error(R.drawable.ic_contact_picture)%0A              .into(avatarView
 );%0A 
@@ -3630,16 +3630,17 @@
       %7D%0A
+%0A
         

Use Picasso to load contact avatars in MemberList