using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using digioz;
using System.Xml;
using System.Xml.XPath;

public partial class controls_RssFeedList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PopulateAllPanels();
    }

    private void PopulateAllPanels()
    {
        RSSFeedList loRssFdLst = new RSSFeedList();
        int lITmp = 0;

        loRssFdLst = RSSFeedManagement.FetchAllRSSFeeds();

        foreach (RSSFeed oRssFd in loRssFdLst)
        {
            LinkButton loLnkBtn = new LinkButton();
            loLnkBtn.ID = "lnkBtn_" + lITmp.ToString();
            loLnkBtn.Width = Unit.Percentage(99);
            loLnkBtn.Text = oRssFd.FeedDesc;
            loLnkBtn.ForeColor = System.Drawing.Color.White;
            loLnkBtn.Font.Bold = true;
            loLnkBtn.Font.Underline = false;
            loLnkBtn.BackColor = System.Drawing.ColorTranslator.FromHtml("#404040");
            //loLnkBtn.CssClass = "newslisttoolbar";
            loLnkBtn.Visible = true;
            loLnkBtn.CommandName = oRssFd.FeedDesc;
            loLnkBtn.CommandArgument = oRssFd.FeedURL;
            loLnkBtn.Command += new CommandEventHandler(DisplayRssFeed);
            loLnkBtn.ToolTip = oRssFd.FeedURL;
            rssFeedListHolder.Controls.Add(loLnkBtn);
            
            Panel loPnl = new Panel();
            loPnl.Width = Unit.Percentage(99);
            loPnl.ID = "pnl_" + lITmp.ToString();
            loPnl.Direction = ContentDirection.LeftToRight;
            loPnl.BorderColor = System.Drawing.Color.Red;
            loPnl.Visible = true;
            rssFeedListHolder.Controls.Add(loPnl);
            rssFeedListHolder.Controls.Add(GetLiteral("<hr width='100%' align='left' />"));
            lITmp++;
        }
        
    }

    void DisplayRssFeed(object sender, CommandEventArgs e)
    {
        IButtonControl loLnkBtnClicked = (IButtonControl)sender;
        string lsFeedUrl = loLnkBtnClicked.CommandArgument;
        string lsOrigCtlId = ((System.Web.UI.Control)(loLnkBtnClicked)).ID.Replace("lnkBtn_", "");
        XmlDocument lxRssFeedDoc = new XmlDocument();
        XPathDocument lxpRssFeedDoc;

        XmlElement lxRootElmnt = null;
        string lsStatus = "noerr";
        XmlNodeList lxNodeLst, lxNodeLst_1;

        string lsFeedTtl = "";
        string lsFeedDesc = "";
        string lsFeedLink = "";
        string lsRootElem = "";
        string lsFeedItemTtl = "";
        string lsFeedItemLnk = "";
        string lsFeedItemDesc = "";

        Table loFeedLstTbl = new Table();
        TableRow loFeedLstTr1 = new TableRow();
        TableRow loFeedLstTr2 = new TableRow();
        TableRow loFeedLstTr3 = new TableRow();
        TableRow loFeedLstTr4 = new TableRow();
        TableHeaderCell loFeedLstTc1 = new TableHeaderCell();
        TableCell loFeedLstTc2 = new TableCell();
        TableCell loFeedLstTc3 = new TableCell();
        TableCell loFeedLstTc4 = new TableCell();
        Label loLable = new Label();
        HyperLink loHypLnk = new HyperLink();
        loHypLnk.Target = "_blank";

        loFeedLstTbl.ID = "tblFeedData";
        loFeedLstTbl.Width = Unit.Percentage(100);

        try
        {
            lxRssFeedDoc.Load(lsFeedUrl);
            
            lxRootElmnt = lxRssFeedDoc.DocumentElement;
            if (lxRootElmnt.Name.IndexOf("rdf") != -1)
            {
                //this rss feed is of version 0.90
                lsRootElem = "/RDF/";
            }
            else if (lxRootElmnt.Name.IndexOf("rss") != -1)
            {
                //this rss feed is NOT of version 0.90
                lsRootElem = "/rss/channel/";
            }

            lxpRssFeedDoc = new XPathDocument(lsFeedUrl);
            lxNodeLst = lxRootElmnt.FirstChild.ChildNodes;
            for (int i = 0; i < lxNodeLst.Count; i++)
            {
                switch (lxNodeLst[i].Name)
                {
                    case "title":
                        lsFeedTtl = lxNodeLst[i].InnerText;
                        break;
                    case "description":
                        lsFeedDesc = lxNodeLst[i].InnerText;
                        break;
                    case "link":
                        lsFeedLink = lxNodeLst[i].InnerText;
                        break;
                }
            }

            loHypLnk = new HyperLink();
            loHypLnk.ForeColor = System.Drawing.Color.Black;
            loHypLnk.NavigateUrl = lsFeedLink;
            loHypLnk.Text = lsFeedTtl;

            loFeedLstTc1 = new TableHeaderCell();
            loFeedLstTc1.Controls.Add(loHypLnk);

            loFeedLstTr1 = new TableRow();
            loFeedLstTr1.Cells.Add(loFeedLstTc1);

            loLable = new Label();
            loLable.Text = lsFeedDesc + "<br /><br />";

            loFeedLstTc2 = new TableCell();
            loFeedLstTc2.Controls.Add(loLable);

            loFeedLstTr2 = new TableRow();
            loFeedLstTr2.Cells.Add(loFeedLstTc2);

            loFeedLstTbl.Rows.Add(loFeedLstTr1);
            loFeedLstTbl.Rows.Add(loFeedLstTr2);

            if (lsRootElem != "/RDF/")
            {
                lxNodeLst = lxRootElmnt.ChildNodes[0].ChildNodes;
            }
            else
            {
                lxNodeLst = lxRootElmnt.ChildNodes;
            }

            for (int i = 0; i < lxNodeLst.Count; i++)
            {
                switch (lxNodeLst[i].Name)
                {
                    case "item":
                        lxNodeLst_1 = lxNodeLst[i].ChildNodes;
                        for (int j = 0; j < lxNodeLst_1.Count; j++)
                        {
                            switch (lxNodeLst_1[j].Name)
                            {
                                case "title":
                                    lsFeedItemTtl = lxNodeLst_1[j].InnerText;
                                    break;
                                case "link":
                                    lsFeedItemLnk = lxNodeLst_1[j].InnerText;
                                    break;
                                case "description":
                                    lsFeedItemDesc = lxNodeLst_1[j].InnerText;
                                    break;
                            }
                        }
                        if (lsFeedItemTtl != "")
                        {
                            loHypLnk = new HyperLink();
                            loHypLnk.NavigateUrl = lsFeedItemLnk;
                            loHypLnk.Text = lsFeedItemTtl;
                            loFeedLstTc3 = new TableCell();
                            loFeedLstTc3.Wrap = true;
                            loFeedLstTc3.Controls.Add(loHypLnk);

                            loFeedLstTr3 = new TableRow();
                            loFeedLstTr3.Cells.Add(loFeedLstTc3);

                            loFeedLstTbl.Rows.Add(loFeedLstTr3);
                        }
                        if (lsFeedItemDesc != "")
                        {
                            loLable = new Label();
                            loLable.Text = lsFeedItemDesc;

                            loFeedLstTc4 = new TableCell();
                            loFeedLstTc4.Controls.Add(loLable);

                            loFeedLstTr4 = new TableRow();
                            loFeedLstTc4.Wrap = true;
                            loFeedLstTr4.Cells.Add(loFeedLstTc4);

                            loFeedLstTbl.Rows.Add(loFeedLstTr4);
                        }
                        break;
                }
            }
        }
        catch (Exception ex)
        {
            lsStatus = "err::" + ex.Message;
        }
        finally
        {
        }
        Control loCtl = rssFeedListHolder.FindControl("pnl_" + lsOrigCtlId);
        Panel loPnl = (Panel)loCtl;
        loPnl.Controls.Add(loFeedLstTbl);
        loPnl.BorderStyle = BorderStyle.Solid;
        loPnl.BorderWidth = Unit.Pixel(1);
        loPnl.BorderColor = System.Drawing.ColorTranslator.FromHtml("#404040");
        loPnl.BackColor = System.Drawing.Color.Snow;
        loPnl.ScrollBars = ScrollBars.Auto;
        loPnl.Wrap = true;
        loPnl.Visible = true;
    }

    private Literal GetLiteral(string psTxt)
    {
        Literal loLtrl = new Literal();
        loLtrl.Text = psTxt;
        return (loLtrl);
    }

}
