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

namespace digioz
{
    public class NewsManagement
    {
        private static PortalSettings coPortalSettings;

        public static News GetLatestNews()
        {
            News loNews = new News();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                DataTable loMenu;

                string lsSQL = "SELECT TOP 1* FROM tblNews ORDER by news_id DESC;";

                loDB.openConnection();
                loMenu = loDB.QueryDBDataset(lsSQL);
                loDB.closeConnection();
                DataRow loDR = loMenu.Rows[0];

                loNews.ID = Convert.ToInt16(loDR["news_id"]);
                loNews.Poster = Convert.ToString(loDR["news_poster"]);
                loNews.Subject = Convert.ToString(loDR["news_subject"]);
                loNews.Description = Convert.ToString(loDR["news_description"]);
                loNews.Timestamp = Convert.ToDateTime(loDR["news_timestamp"]);
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                IEnumerable loObjs = loDB.GetEntity("News");
                loNews = null;
                foreach (object obj in loObjs)
                {
                    if(loNews == null)
                    {
                        loNews = (News)(obj);
                    }
                    else
                    {
                       if(loNews.ID < ((News)obj).ID )
                           loNews = ((News)obj);
                    }
                }
                loDB.closeEntity();
            }
            return loNews;
        }

        public static NewsList GetNewsList()
        {
            NewsList loNewsList = new NewsList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                News loNews;
                DataTable loNewsTable;

                string lsSQL = "SELECT * FROM tblNews ORDER by news_id DESC;";

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

                foreach (DataRow loDR in loNewsTable.Rows)
                {
                    loNews = new News();

                    loNews.ID = Convert.ToInt16(loDR["news_id"]);
                    loNews.Poster = Convert.ToString(loDR["news_poster"]);
                    loNews.Subject = Convert.ToString(loDR["news_subject"]);
                    loNews.Description = Convert.ToString(loDR["news_description"]);
                    loNews.Timestamp = Convert.ToDateTime(loDR["news_timestamp"]);

                    loNewsList.Add(loNews);
                }
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                IEnumerable loObjs = loDB.GetEntity("News");
                foreach (object obj in loObjs)
                {
                    loNewsList.Add((News)obj);
                }
                loDB.closeEntity();
            }
            return loNewsList;
        }

        public static string SaveNews(string psUserName, string psNewsDescriptionEncoded, string psNewsSubject, string psNewsDescription, string psHiddenFieldText, bool pbEdit)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            string coRtnStatus = "";

            try
            {
                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 tblNews set news_subject='"
                            + FormatNews(psNewsSubject) + "',news_description='" + FormatNews(psNewsDescription)
                            + "' , news_timestamp=@d  where news_id=" + psHiddenFieldText;
                    }
                    else
                    {
                        lsSQL = " DECLARE @d DATETIME SELECT @d = GETDATE() EXEC spInsertNews '"
                            + psUserName + "','" + FormatNews(psNewsSubject) + "','"
                            + FormatNews(psNewsDescriptionEncoded) + "',@d";
                    }
                    loDB.openConnection();
                    loDB.ExecDB(lsSQL);
                    loDB.closeConnection();
                }
                else
                {
                    IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                    loDB.openEntity();
                    if (true == pbEdit)
                    {
                        News oNews = (News)loDB.GetEntity("News", Convert.ToInt32(psHiddenFieldText));
                        oNews.Subject = psNewsSubject;
                        oNews.Description = psNewsDescription;
                        loDB.SaveEntity(oNews);
                    }
                    else
                    {
                        News oNews = new News();
                        oNews.Timestamp = DateTime.Now;
                        //oNews.ID = 
                        oNews.Poster = psUserName;
                        oNews.Subject = psNewsSubject;
                        oNews.Description = psNewsDescription;
                        loDB.CreateEntity(oNews);
                    }

                    loDB.closeEntity();
                }
                coRtnStatus = "allok:";
            }
            catch (Exception ex)
            {
                coRtnStatus = "notok:" + ex.Message;
            }
            return coRtnStatus;
        }

        public static void DeleteNews(int piArticleId)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = "EXEC spDeleteNews " + piArticleId;
                loDB.openConnection();
                loDB.ExecDB(lsSQL);
                loDB.closeConnection();
            }
            else
            {
                //ToDo
            }
        }

        public static string[] GetNews(int piNewsId)
        {
            string[] lsNewsData = new string[2];
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

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

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

                lsNewsData[0] = loDT.Rows[0][2].ToString();
                lsNewsData[1] = loDT.Rows[0][3].ToString();
            }
            else
            {
                IDataNonRelational loDB = DataFactoryNonRelational.GetDataObject(coPortalSettings);
                loDB.openEntity();
                News oNews = (News)loDB.GetEntity("News", piNewsId);
                lsNewsData[0] = oNews.Subject;
                lsNewsData[1] = oNews.Description;
                loDB.closeEntity();
            }
            return lsNewsData;
        }
        public static string FormatNews(string psNewsText)
        {
            string lsOutput = psNewsText;
            lsOutput = System.Text.RegularExpressions.Regex.Replace(psNewsText, "<[^>]*>", string.Empty);
            lsOutput = lsOutput.Replace("'", "''");
            return lsOutput;
        }   //end FormatNews
    }
}



