package org.r3pek.guiatv;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class ChannelsSimpleAdapter extends SimpleAdapter {
	private ChannelList channels;
	private Context context;

	public ChannelsSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from,	int[] to, ChannelList channels) {
		super(context, data, resource, from, to);
		this.context= context;  
		this.channels = channels;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = super.getView(position, convertView, parent);
		
		ImageView ivIcon = (ImageView) v.findViewById(R.id.ivIcon);
		TextView tvName = (TextView) v.findViewById(R.id.tvNome);
		String channel = channels.getChannels().get(channels.namePos(tvName.getText().toString())).getSigla();
		channel = channel.replaceAll(" ", "_").replaceAll("-", "_").replaceAll("\\+", "_").replaceAll("!", "_").replaceAll("&", "_");
		channel = channel.toLowerCase();
		if (channel.charAt(0) >= '0' && channel.charAt(0) <= '9')
			channel = "a" + channel;
		
		int res = context.getResources().getIdentifier(channel, "drawable", "org.r3pek.guiatv");
		if (res > 0) {
			ivIcon.setImageResource(res);
			ivIcon.setBackgroundColor(Color.WHITE);
		} else {
			ivIcon.setImageResource(R.drawable.none);
			ivIcon.setBackgroundDrawable(null);
		}
		
		return v;
	}

}
