package org.theotech.ceaselessandroid.fragment;


import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.joanzapata.iconify.widget.IconTextView;
import com.squareup.picasso.Picasso;

import org.theotech.ceaselessandroid.R;
import org.theotech.ceaselessandroid.person.PersonManager;
import org.theotech.ceaselessandroid.person.PersonManagerImpl;
import org.theotech.ceaselessandroid.realm.pojo.PersonPOJO;
import org.theotech.ceaselessandroid.util.CommonUtils;
import org.theotech.ceaselessandroid.util.Constants;
import org.theotech.ceaselessandroid.util.FragmentUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class PeopleActiveSupportFragment extends Fragment {
    private static final String TAG = PeopleActiveSupportFragment.class.getSimpleName();

    @Bind(R.id.people_active)
    ListView peopleActive;

    private PersonManager personManager;

    public PeopleActiveSupportFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

        personManager = PersonManagerImpl.getInstance(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // set title
        getActivity().setTitle(getString(R.string.nav_people));

        // create view and bind
        View view = inflater.inflate(R.layout.fragment_support_people_active, container, false);
        ButterKnife.bind(this, view);

        // populate the list of active people
        final List<PersonPOJO> activePersons = personManager.getActivePeople();
        peopleActive.setAdapter(new ActivePeopleArrayAdapter(getActivity(), activePersons));
        peopleActive.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString(Constants.PERSON_ID_BUNDLE_ARG, activePersons.get(position).getId());
                FragmentUtils.loadFragment(getActivity(), getActivity().getFragmentManager(), null,
                        R.id.person_card, bundle, new FragmentState(getString(R.string.nav_people)));
            }
        });

        return view;
    }

    private class ActivePeopleArrayAdapter extends ArrayAdapter<PersonPOJO> {
        private final Context context;
        private final List<PersonPOJO> persons;
        private LayoutInflater inflater;

        public ActivePeopleArrayAdapter(Context context, List<PersonPOJO> persons) {
            super(context, -1, persons);
            this.context = context;
            this.persons = persons;
            this.inflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            final ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = inflater.inflate(R.layout.list_item_people_active, parent, false);
                holder.favorite = (IconTextView) view.findViewById(R.id.person_favorite);
                holder.personThumbnail = (ImageView) view.findViewById(R.id.person_thumbnail);
                holder.personListName = (TextView) view.findViewById(R.id.person_list_name);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            final PersonPOJO person = persons.get(position);
            // favorite
            if (person.isFavorite()) {
                holder.favorite.setText(getString(R.string.favorite_on));
            } else {
                holder.favorite.setText(getString(R.string.favorite_off));
            }
            holder.favorite.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PersonPOJO updatedPerson = personManager.getPerson(person.getId());
                    if (updatedPerson.isFavorite()) {
                        personManager.unfavoritePerson(updatedPerson.getId());
                        holder.favorite.setText(getString(R.string.favorite_off));
                    } else {
                        personManager.favoritePerson(updatedPerson.getId());
                        holder.favorite.setText(getString(R.string.favorite_on));
                    }
                }
            });
            // thumbnail picture
            Uri thumbnailUri = CommonUtils.getContactPhotoUri(context.getContentResolver(), person.getId(), false);
            Picasso.with(context).load(thumbnailUri).placeholder(R.drawable.placeholder_user).fit().into(holder.personThumbnail);
            // person name
            holder.personListName.setText(person.getName());

            return view;
        }

        private class ViewHolder {
            IconTextView favorite;
            ImageView personThumbnail;
            TextView personListName;
        }
    }
}

@@ -2949,49 +2949,8 @@
 ons;
-%0A        private LayoutInflater inflater;
 %0A%0A  
@@ -3149,66 +3149,8 @@
 ns;%0A
-            this.inflater = LayoutInflater.from(context);%0A
     
@@ -3217,25 +3217,32 @@
 ition, View 
-v
+convertV
 iew, ViewGro
@@ -3270,105 +3270,128 @@
     
-final ViewHolder holder;%0A            if (view == null) %7B%0A                holder = new ViewHolder(
+LayoutInflater inflater = (LayoutInflater) context%0A                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE
 );%0A 
@@ -3401,21 +3401,25 @@
         
-    v
+View rowV
 iew = in
@@ -3491,35 +3491,43 @@
             
-    holder.
+final IconTextView 
 favorite = (
@@ -3532,33 +3532,36 @@
  (IconTextView) 
-v
+rowV
 iew.findViewById
@@ -3592,35 +3592,34 @@
             
-    holder.
+ImageView 
 personThumbn
@@ -3632,25 +3632,28 @@
 (ImageView) 
-v
+rowV
 iew.findView
@@ -3689,35 +3689,33 @@
             
-    holder.
+TextView 
 personListNa
@@ -3730,17 +3730,20 @@
 xtView) 
-v
+rowV
 iew.find
@@ -3779,133 +3779,8 @@
 e);%0A
-                view.setTag(holder);%0A            %7D else %7B%0A                holder = (ViewHolder) view.getTag();%0A            %7D%0A
     
@@ -3907,39 +3907,32 @@
                 
-holder.
 favorite.setText
@@ -3995,39 +3995,32 @@
                 
-holder.
 favorite.setText
@@ -4073,39 +4073,32 @@
   %7D%0A            
-holder.
 favorite.setOnCl
@@ -4447,39 +4447,32 @@
                 
-holder.
 favorite.setText
@@ -4637,23 +4637,16 @@
         
-holder.
 favorite
@@ -4998,23 +4998,16 @@
 ().into(
-holder.
 personTh
@@ -5059,15 +5059,8 @@
     
-holder.
 pers
@@ -5121,169 +5121,15 @@
 urn 
-view;%0A        %7D%0A%0A        private class ViewHolder %7B%0A            IconTextView favorite;%0A            ImageView personThumbnail;%0A            TextView personListName
+rowView
 ;%0A  

Revert "Modifying the adapter to use the holder pattern - much better listview performance"