using System;
using System.Data;
using System.Configuration;
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 System.Collections;
using System.Xml;

namespace digioz
{
    public class RSSFeedManagement
    {
        private string csRSSFeedURL;
        private string csRSSFeedDesc;
        private enum csRSSFeedVer { Ver090, Ver091, Ver20 };
        private static PortalSettings coPortalSettings;


        public RSSFeedManagement()
        {
        }

        public static string ValidateRSSFeed(string psFeedURL)
        {
            XmlDocument lxRssFeedDoc = new XmlDocument();
            string lsStatus = "noerr";
            XmlElement lxRootElmnt = null;
            string lsRssFeedVer = "";

            try
            {
                lxRssFeedDoc.Load(psFeedURL);
                lxRootElmnt = lxRssFeedDoc.DocumentElement;
                if (lxRootElmnt.Name.IndexOf("rdf") != -1)
                {
                    //this rss feed is of version 0.90
                    if (lxRootElmnt.FirstChild.Name == "channel")
                    {
                    }
                    else
                    {
                        lsStatus = "err::Malformed Feed..";
                    }
                }
                else if (lxRootElmnt.Name.IndexOf("rss") != -1)
                {
                    //this rss feed is NOT of version 0.90
                    if (lxRootElmnt.HasAttribute("version"))
                    {
                        lsRssFeedVer = lxRootElmnt.GetAttribute("version");
                        if (lxRootElmnt.FirstChild.Name == "channel")
                        {
                        }
                        else
                        {
                            lsStatus = "err::Malformed Feed..";
                        }
                    }
                    else
                    {
                        lsStatus = "err::Malformed Feed..";
                    }
                }
                else
                {
                    lsStatus = "err::Malformed Feed..";
                }
            }
            catch (Exception ex)
            {
                lsStatus = "err::" + ex.Message;
            }
            finally
            {
            }

            return lsStatus;
        }

        public static RSSFeedList FetchAllRSSFeeds()
        {
            RSSFeedList loRssFdLst = new RSSFeedList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                RSSFeed loRssFeed;
                DataTable loRssFeedTable;

                string lsSQL = "SELECT * FROM tblRSSFeeds ORDER by rssfeed_id DESC;";

                loDB.openConnection();
                loRssFeedTable = loDB.QueryDBDataset(lsSQL);
                loDB.closeConnection();

                foreach (DataRow loDR in loRssFeedTable.Rows)
                {
                    loRssFeed = new RSSFeed();

                    loRssFeed.FeedId = Convert.ToInt16(loDR["rssfeed_id"]);
                    loRssFeed.FeedDesc = Convert.ToString(loDR["rssfeed_desc"]);
                    loRssFeed.FeedURL = Convert.ToString(loDR["rssfeed_url"]);
                    loRssFeed.FeedModBy = Convert.ToString(loDR["last_mod_by"]);
                    loRssFeed.FeedModOn = Convert.ToDateTime(loDR["last_mod_on"]);

                    loRssFdLst.Add(loRssFeed);
                }
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                IEnumerable loObjs = loDB.GetEntity("RssFeed");
                foreach (object obj in loObjs)
                {
                    loRssFdLst.Add((RSSFeed)obj);
                }
                loDB.closeEntity();
            }

            return loRssFdLst;
        }


        public static void SaveRssFeed(string psFeedDesc, string psFeedURL, string psHiddenFieldText, string psUser, bool pbEdit)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsDate = DateTime.Now.ToString();
                string lsSQL = "";

                if (true == pbEdit)
                {
                    lsSQL = @"DECLARE @d DATETIME
                            SELECT @d = GETDATE() UPDATE tblRssFeeds 
                            set rssfeed_desc='" + psFeedDesc.Replace('\'', '`').Replace('"', '`')
                            + "',rssfeed_url='" + psFeedURL.Replace('\'', '`').Replace('"', '`')
                            + "',last_mod_by='" + psUser
                            + "' , last_mod_on=@d  where rssfeed_id="
                            + psHiddenFieldText;
                }
                else
                {
                    lsSQL = @" DECLARE @d DATETIME
                            SELECT @d = GETDATE()
                            EXEC spInsertRssFeed '" + psFeedDesc.Replace('\'', '`').Replace('"', '`') + "','"
                            + psFeedURL.Replace('\'', '`').Replace('"', '`') + "','"
                            + psUser + "',@d";
                }
                loDB.openConnection();
                loDB.ExecDB(lsSQL);
                loDB.closeConnection();
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                if (true == pbEdit)
                {
                    RSSFeed oRssFeed = (RSSFeed)loDB.GetEntity("RssFeed", Convert.ToInt32(psHiddenFieldText));
                    oRssFeed.FeedDesc = psFeedDesc;
                    oRssFeed.FeedURL = psFeedURL;
                    oRssFeed.FeedModBy = psUser;
                    loDB.SaveEntity(oRssFeed);
                }
                else
                {
                    RSSFeed oRssFeed = new RSSFeed();
                    oRssFeed.FeedDesc = psFeedDesc;
                    oRssFeed.FeedModBy = psUser;
                    oRssFeed.FeedModOn = DateTime.Now;
                    oRssFeed.FeedURL = psFeedURL;
                    loDB.CreateEntity(oRssFeed);
                }

                loDB.closeEntity();
            }
        }

        public static void DeleteRssFeed(int piFeedId)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = "EXEC spDeleteRssFeed " + piFeedId;
                loDB.openConnection();
                loDB.ExecDB(lsSQL);
                loDB.closeConnection();
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                loDB.DeleteEntity("RssFeed", piFeedId);
                loDB.closeEntity();
            }
        }

        public static string[] GetRssFeed(int piFeedId)
        {
            string[] lsRssFeedData = new string[2];
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = "select * FROM tblRssFeeds where rssfeed_id = " + piFeedId;
                DataTable loDT;

                loDB.openConnection();
                loDT = loDB.QueryDBDataset(lsSQL);
                loDB.closeConnection();

                lsRssFeedData[0] = loDT.Rows[0][1].ToString();
                lsRssFeedData[1] = loDT.Rows[0][2].ToString();
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                RSSFeed oRssFeed = (RSSFeed)loDB.GetEntity("RssFeed", piFeedId);
                lsRssFeedData[0] = oRssFeed.FeedDesc;
                lsRssFeedData[1] = oRssFeed.FeedURL;
                loDB.closeEntity();
            }
            return lsRssFeedData;
        }
    


    }
}
