package org.r3pek.guiatv;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
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.SimpleAdapter;

public class ProgramListSimpleAdapter extends SimpleAdapter {
	private ProgramList programList;

	public ProgramListSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to, ProgramList programList) {
		super(context, data, resource, from, to);
		this.programList = programList;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = super.getView(position, convertView, parent);
		if (programList == null) return v;
		if (programList.getProgramCount() == 0) return v;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		
		Program p = programList.getPrograms().get(position);
		Date now = new Date(System.currentTimeMillis());
		Date start = null, end = null;
		try {
			start = format.parse(p.getStartHour());
			end = format.parse(p.getEndHour());
		} catch (ParseException e) { }
		
		if (now.after(start) && now.before(end))
			v.setBackgroundColor(Color.GRAY);
		else
			v.setBackgroundDrawable(null);
		
		return v;
	}

}
