package org.r3pek.guiatv;

import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

public class ProgramList {
	private ArrayList<Program> programList = null;
	
	public ProgramList(String channel, String day) {
		try {
			channel = channel.replaceAll(" ", "+").replaceAll("&", "%26");
			URL url = new URL("http://services.sapo.pt/EPG/GetChannelByDateInterval?channelSigla=" + channel + "&startDate=" + day + "+00:00:00&endDate=" + day + "+23:59:59");
			
			/* Get a SAXParser from the SAXPArserFactory. */ 
			SAXParserFactory spf = SAXParserFactory.newInstance(); 
			SAXParser sp = spf.newSAXParser();
			
			/* Get the XMLReader of the SAXParser we created. */ 
			XMLReader xr = sp.getXMLReader(); 
			/* Create a new ContentHandler and apply it to the XML-Reader*/ 
			ProgramListParser handler = new ProgramListParser();
			xr.setContentHandler(handler);
			
			/* Parse the xml-data from our URL. */
			xr.parse(new InputSource(url.openStream()));
			/* Parsing has finished. */ 
			
			/* Our XMLParserHandler now provides the parsed data to us. */ 
			programList = handler.getProgramList();
		} catch (Exception e) { }
	}
	
	public int getProgramCount() {
		return programList.size();
	}
	
	public ArrayList<Program> getPrograms() {
		return programList;
	}

}
