package de.thomas.dreja.ec.musicquiz.gui.dialog;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import de.thomas.dreja.ec.musicquiz.R;
import de.thomas.dreja.ec.musicquiz.data.Group;

public class GroupListAdapter extends BaseAdapter {

	private Group[] groups;
	private int songIndex;
	
	public GroupListAdapter(Group[] groups, int songIndex) {
		this.groups = groups;
		this.songIndex = songIndex;
	}

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

	@Override
	public Object getItem(int position) {
		return groups[position];
	}
	
	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Group gr = groups[position];
		ViewHolder viewHolder;
		
		if(convertView == null) {
			LayoutInflater ly = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = ly.inflate(R.layout.list_icon_element, parent, false);
			
			viewHolder = new ViewHolder();
			viewHolder.groupText = (TextView) convertView.findViewById(R.id.folder_title);
			
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		
		
		if(gr.hasAnswered(songIndex)) {
			viewHolder.groupText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.group_icon, 0, R.drawable.has_answered_icon, 0);
			viewHolder.groupText.setTextColor(Color.LTGRAY);
		} else {
			viewHolder.groupText.setTextColor(Color.WHITE);
			viewHolder.groupText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.group_icon, 0, 0, 0);
		}		
		viewHolder.groupText.setText(gr.getFullName(parent.getResources()));
		viewHolder.groupText.setBackground(gr.getColor().getDrawable(parent.getResources()));
		
		return convertView;
	}
	
	static class ViewHolder {
		TextView groupText;
	}

	public void setGroups(Group[] groups2) {
		this.groups = groups2;
		this.notifyDataSetChanged();
	}

}
