package fishrungames.bashgid.core;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.util.Log;

public class RssReader {

	public static class Rss2Item
	{
		String title;
		String link;
		String description;
		
		
		public static class Rss2Enclosure
		{
			String url;
			int length;
			String mimeType;
			int width;
			int height;
			
			Rss2Enclosure()
			{
				url = null;
				length = 0;
				mimeType = null;
				width = 0;
				height = 0;
			}
			
			Rss2Enclosure(String url, int length, String mimeType, int width, int height)
			{
				this.url = url;
				this.length = length;
				this.mimeType = mimeType;
				this.width = width;
				this.height = height;
			}
		}
		
		Rss2Enclosure rss2Enclosure;
		
		Date pubDate;
		
		Rss2Item(String title, String link, String description, Rss2Enclosure rss2Enclosure, Date pubDate)
		{
			this.title = title;
			this.link = link;
			this.description = description;
			this.rss2Enclosure = rss2Enclosure;
			this.pubDate = pubDate;
			
		}
		
	}
	
	ArrayList<Rss2Item> rss2ItemArr = new ArrayList<Rss2Item>();	
	
	void Clear()
	{
		rss2ItemArr.clear();
	}
	
	void ReadRss(String xmlCode)
	{
		Document xmlDoc = XmlProcessor.getDomElement(xmlCode);
		
		if (xmlDoc == null)
		{
			Log.e("Error XML parsing", "Error XML parsing in ReadRss");
			return;
		}
		
		Element channel = (Element) xmlDoc.getElementsByTagName("channel").item(0);
	
		NodeList nl = channel.getElementsByTagName("item");
		
		 
		// looping through all item nodes <item>     
		for (int i = 0; i < nl.getLength(); i++) {
			
			Element e = (Element) nl.item(i);
			
			String title = XmlProcessor.getValue(e, "title");
			String link = XmlProcessor.getValue(e, "link");
			String description = XmlProcessor.getValue(e, "description"); // description child value
			
			
			SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
			
			Date pubDate = new Date();
			
			try {
				pubDate = dateFormat.parse(XmlProcessor.getValue(e, "pubDate"));
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
			
			
			NodeList nodeList = e.getElementsByTagName("enclosure");
			
			Rss2Item.Rss2Enclosure rss2Enclosure = null;
			
			if (nodeList.getLength() != 0)
			{
			
				String enclosureUrl = XmlProcessor.getAttributeValue(e, "enclosure", "url");
				String enclosureLengthStr = XmlProcessor.getAttributeValue(e, "enclosure", "length");
				
				if (enclosureLengthStr.equals(""))
				{
					enclosureLengthStr = "0";
				}
				
				int enclosureLength = Integer.valueOf(enclosureLengthStr);
				String enclosureMimeType = XmlProcessor.getAttributeValue(e, "enclosure", "type");
				
				String enclosureWidthStr = XmlProcessor.getAttributeValue(e, "enclosure", "width");
				if (enclosureWidthStr.equals(""))
				{
					enclosureWidthStr = "0";
				}
				
				int enclosureWidth = Integer.valueOf(enclosureWidthStr);
				
				String enclosureHeightStr = XmlProcessor.getAttributeValue(e, "enclosure", "height");
				if (enclosureHeightStr.equals(""))
				{
					enclosureHeightStr = "0";
				}
				
				int enclosureHeight = Integer.valueOf(enclosureHeightStr);
				
				
				rss2Enclosure = new Rss2Item.Rss2Enclosure(enclosureUrl, enclosureLength, enclosureMimeType, enclosureHeight, enclosureWidth);
				
			}
			
			
			Rss2Item rss2Item = new Rss2Item(title, link, description, rss2Enclosure, pubDate);
			
			rss2ItemArr.add(rss2Item);
			
		}
	}
	
	
}
