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

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

        public static SmileyList GetSmileyList(out string psError)
        {
            psError = string.Empty;
            SmileyList loSmileyList = new SmileyList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                Smiley loSmiley;
                DataTable loSmileyTable;
                string lsSQL = "SELECT * FROM tblSmilies";

                try
                {
                    loDB.openConnection();
                    loSmileyTable = loDB.QueryDBDataset(lsSQL);
                    foreach (DataRow loDR in loSmileyTable.Rows)
                    {
                        loSmiley = new Smiley();
                        loSmiley.ID = Convert.ToInt32(loDR["smiley_id"]);

                        loSmiley.Abbr = Convert.ToString(loDR["smiley_abbr"]);
                        loSmiley.Image = Convert.ToString(loDR["smiley_image"]);
                        loSmileyList.Add(loSmiley);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }

                psError = loDB.sErr;
            }
            else
            {
                // Non Relational Database
            }

            return loSmileyList;
        }

        public static SmileyList GetSmileyListTop38(out string psError)
        {
            psError = string.Empty;
            SmileyList loSmileyList = new SmileyList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                Smiley loSmiley;
                DataTable loSmileyTable;
                string lsSQL = "SELECT Top 38* FROM tblSmilies";

                try
                {
                    loDB.openConnection();
                    loSmileyTable = loDB.QueryDBDataset(lsSQL);
                    foreach (DataRow loDR in loSmileyTable.Rows)
                    {
                        loSmiley = new Smiley();
                        loSmiley.ID = Convert.ToInt32(loDR["smiley_id"]);

                        loSmiley.Abbr = Convert.ToString(loDR["smiley_abbr"]);
                        loSmiley.Image = Convert.ToString(loDR["smiley_image"]);
                        loSmileyList.Add(loSmiley);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }

                psError = loDB.sErr;
            }
            else
            {
                // Non Relational Database
            }

            return loSmileyList;
        }

        /// <summary>
        /// Get a Smiley based on id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Smiley GetSmiley(int piID)
        {
            Smiley loSmiley = null;

            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSql = string.Format("SELECT * FROM tblSmilies WHERE smiley_id = {0}", piID);
                try
                {
                    loDB.openConnection();
                    DataTable ta = loDB.QueryDBDataset(lsSql);
                    if (ta.Rows.Count > 0)
                    {
                        loSmiley = new Smiley();
                        DataRow dr = ta.Rows[0];
                        loSmiley.ID = (int)dr["smiley_id"];
                        loSmiley.Abbr = Convert.ToString(dr["smiley_abbr"]);
                        loSmiley.Image = Convert.ToString(dr["smiley_image"]);
                    }

                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            return loSmiley;
        }

        /// <summary>
        /// Add/Update Smiley
        /// </summary>
        /// <param name="poSmiley"></param>
        /// <param name="psError"></param>
        /// <returns></returns>
        public static bool Save(Smiley poSmiley, out string psError)
        {
            if (poSmiley.ID == -1)
            {
                return Insert(poSmiley, out psError);
            }
            else
            {
                return Update(poSmiley, out psError);
            }
        }

        /// <summary>
        /// Insert a new record
        /// </summary>
        /// <param name="poSmiley"></param>
        /// <param name="psError"></param>
        /// <returns></returns>
        public static bool Insert(Smiley poSmiley, out string psError)
        {
            bool lbRetValue = false;
            psError = string.Empty;

            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSql;

                lsSql = string.Format("Exec spInsertSmiley @abbr = '{0}', " + "@img = '{1}'",
                                    poSmiley.Abbr, poSmiley.Image);
                try
                {
                    loDB.openConnection();
                    loDB.ExecDB(lsSql);
                    lbRetValue = (String.IsNullOrEmpty(loDB.sErr));
                    psError = loDB.sErr;
                }
                finally
                {
                    loDB.closeConnection();
                }
            }

            return lbRetValue;
        }

        /// <summary>
        /// Update a record
        /// </summary>
        /// <param name="poSmiley"></param>
        /// <param name="psError"></param>
        /// <returns></returns>
        public static bool Update(Smiley poSmiley, out string psError)
        {
            bool lbRetValue = false;
            psError = string.Empty;

            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSql;

                lsSql = string.Format("Exec spUpdateSmiley @id={0}, @abbr='{1}', @img='{2}'",
                                    poSmiley.ID,
                                    poSmiley.Abbr,
                                    poSmiley.Image);
                try
                {
                    loDB.openConnection();
                    loDB.ExecDB(lsSql);
                    lbRetValue = (String.IsNullOrEmpty(loDB.sErr));
                    psError = loDB.sErr;
                }
                finally
                {
                    loDB.closeConnection();
                }
            }

            return lbRetValue;
        }

        /// <summary>
        /// Delete a Smiley based on id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool Delete(int piID, out string psError)
        {
            bool lbRetValue = false;
            psError = string.Empty;

            coPortalSettings = PortalSettingsManagement.GetPortalSettings();
            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSql = string.Format("Exec spDeleteSmiley @id = {0}", piID);
                try
                {
                    loDB.openConnection();
                    loDB.ExecDB(lsSql);
                    lbRetValue = (String.IsNullOrEmpty(loDB.sErr));
                    psError = loDB.sErr;
                }
                finally
                {
                    loDB.closeConnection();
                }
            }

            return lbRetValue;
        }
    }
}