using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

using BS.Shared.DataContracts;
using DevExpress.Data.Helpers;

namespace BS.Shared.RSS
{
    public class RssManager : IDisposable
    {
        private readonly List<RssItem> _rssItems = new List<RssItem>();

        private bool _IsDisposed;

        private string _feedDescription;

        private string _feedTitle;

        private string _url;

        public RssManager(string feedUrl)
        {
            this._url = feedUrl;
        }

        public string FeedDescription
        {
            get
            {
                return this._feedDescription;
            }
        }

        public string FeedTitle
        {
            get
            {
                return this._feedTitle;
            }
        }

        public List<RssItem> RssItems
        {
            get
            {
                return this._rssItems;
            }
        }

        public string Url
        {
            get
            {
                return this._url;
            }

            set
            {
                this._url = value;
            }
        }

	    public List<RssItem> GetFeed()
	    {
		    try
		    {
			    using (XmlReader reader = XmlReader.Create(this.Url))
			    {
				    XmlDocument xmlDoc = new XmlDocument();
				    xmlDoc.Load(reader);

				    // parse the items of the feed
				    this._feedTitle = this.ParseDocElements(xmlDoc.SelectSingleNode("//channel"), "title");
				    this._feedDescription = this.ParseDocElements(xmlDoc.SelectSingleNode("//channel"), "description");
				    this.ParseRssItems(xmlDoc);

				    // return the feed items
				    return this._rssItems;
			    }
		    }
		    catch (Exception ex)
		    {
			    List<RssItem> list = new List<RssItem>();
			    RssItem item = new RssItem();
			    item.Title = "Fehler beim Lesen des RSS-Feeds.";
			    item.Description = ex.Message;
				list.Add(item);
			    return list;

		    }
	    }

	    public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (disposing && !this._IsDisposed)
            {
                this._rssItems.Clear();
                this._url = null;
                this._feedTitle = null;
                this._feedDescription = null;
            }

            this._IsDisposed = true;
        }

        private string ParseDocElements(XmlNode parent, string xPath)
        {
            XmlNode node = parent.SelectSingleNode(xPath);
            if (node != null)
            {
                return StripHTML(node.InnerText);
            }
            else
            {
                return null;
            }
        }

		public static string StripHTML(string HTMLText)
		{
			Regex reg = new Regex("<[^>]+>", RegexOptions.IgnoreCase);
			String text = reg.Replace(HTMLText, "");

			text = text.Replace("&quot;", "\"");
			return text.Trim();
		}

        private void ParseRssItems(XmlDocument xmlDoc)
        {
            this._rssItems.Clear();
            XmlNodeList nodes = xmlDoc.SelectNodes("rss/channel/item");

            foreach (XmlNode node in nodes)
            {
                RssItem item = new RssItem();
                item.Title = this.ParseDocElements(node, "title");
                item.Description = this.ParseDocElements(node, "description");
                item.Link = this.ParseDocElements(node, "link");

                string date = null;
                date = this.ParseDocElements(node, "pubDate");
                DateTime dt;
                if (DateTime.TryParse(date, out dt))
                {
                    item.Date = dt;
                }

                this._rssItems.Add(item);
            }
        }
    }
}