package com.dyo.d3quests;

import java.util.List;

import android.content.Context;
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.dyo.d3quests.model.Hero;

public class HeroesArrayAdapter extends ArrayAdapter<Hero> {

	private final Context context;
	private final List<Hero> heroes;

	public HeroesArrayAdapter(Context context, int resource, List<Hero> objects) {
		super(context, resource, objects);
		this.context = context;
		this.heroes = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.heroes_list_item, parent, false);
		}

		ImageView heroThumbnail = (ImageView) convertView.findViewById(R.id.hero_thumbnail);
		TextView heroName = (TextView) convertView.findViewById(R.id.hero_name);
		TextView heroDetails = (TextView) convertView.findViewById(R.id.hero_detail);

		Hero hero = heroes.get(position);
		heroName.setText(hero.getName());
		heroDetails.setText(hero.getFormattedLevelAndClass());
		heroThumbnail.setImageResource(imageByGenderAndClass(hero));
		return convertView;
	}

	int imageByGenderAndClass(Hero hero) {
		if (hero.getGender() == 0) {
			// Hero is male.
			if (hero.getD3class().equals("barbarian")) {
				return R.drawable.barb_male;
			} else if (hero.getD3class().equals("crusader")) {
				return R.drawable.crusader_male;
			} else if (hero.getD3class().equals("demon-hunter")) {
				return R.drawable.dh_male;
			}else if (hero.getD3class().equals("monk")) {
				return R.drawable.monk_male;
			} else if (hero.getD3class().equals("witch-doctor")) {
				return R.drawable.wd_male;
			}else if (hero.getD3class().equals("wizard")) {
				return R.drawable.wiz_male;
			}
		} else {
			// Hero is female.
			if (hero.getD3class().equals("barbarian")) {
				return R.drawable.barb_female;
			} else if (hero.getD3class().equals("crusader")) {
				return R.drawable.crusader_female;
			} else if (hero.getD3class().equals("demon-hunter")) {
				return R.drawable.dh_female;
			}else if (hero.getD3class().equals("monk")) {
				return R.drawable.monk_female;
			} else if (hero.getD3class().equals("witch-doctor")) {
				return R.drawable.wd_female;
			}else if (hero.getD3class().equals("wizard")) {
				return R.drawable.wiz_female;
			}
		}

		// Unknown class.
		return R.drawable.ab_solid_ros;
	}
}

@@ -798,16 +798,89 @@
 VICE);%0A%0A
+%09%09ImageView heroThumbnail;%0A%09%09TextView heroName;%0A%09%09TextView heroDetails;%0A%0A
 %09%09if (co
@@ -983,23 +983,9 @@
 ;%0A%09%09
-%7D%0A%0A%09%09ImageView 
+%09
 hero
@@ -1053,33 +1053,25 @@
 umbnail);%0A%09%09
-TextView 
+%09
 heroName = (
@@ -1120,33 +1120,25 @@
 ro_name);%0A%09%09
-TextView 
+%09
 heroDetails 
@@ -1193,16 +1193,275 @@
 detail);
+%0A%09%09%09convertView.setTag(new ViewHolder(heroThumbnail, heroName, heroDetails));%0A%09%09%7D else %7B%0A%09%09%09ViewHolder holder = (ViewHolder) convertView.getTag();%0A%09%09%09heroThumbnail = holder.heroThumbnail;%0A%09%09%09heroName = holder.heroName;%0A%09%09%09heroDetails = holder.heroDetails;%0A%09%09%7D
 %0A%0A%09%09Hero
@@ -1490,16 +1490,17 @@
 ition);%0A
+%0A
 %09%09heroNa
@@ -2910,11 +2910,310 @@
 _ros;%0A%09%7D
+%0A%0A%09static class ViewHolder %7B%0A%09%09ImageView heroThumbnail;%0A%09%09TextView heroName;%0A%09%09TextView heroDetails;%0A%0A%09%09public ViewHolder(ImageView heroThumbnail, TextView heroName, TextView heroDetails) %7B%0A%09%09%09this.heroThumbnail = heroThumbnail;%0A%09%09%09this.heroName = heroName;%0A%09%09%09this.heroDetails = heroDetails;%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Implement ViewHolder pattern on heroes listview