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 ChannelList {
	private ArrayList<Channel> channelList = null;
	
	public ChannelList() {
		try {
			URL url = new URL("http://services.sapo.pt/EPG/GetChannelList?");
			
			/* 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*/ 
			ChannelListParser handler = new ChannelListParser();
			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. */ 
			channelList = handler.getChannelList();
		} catch (Exception e) { }
	}
	
	public int getChannelCount() {
		return channelList.size();
	}
	
	public ArrayList<Channel> getChannels() {
		return channelList;
	}
	
	public int namePos(String name) {
		for (int i = 0; i < getChannelCount(); i++) {
			Channel c = channelList.get(i);
			if (c.getName().equals(name))
				return i;
		}
		return 0;
	}

}
