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;

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

        public static void SaveChatMessages(ChatMessage poChatMessage)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

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

                string lsSQL = "EXEC spInsertChatMessage '" + poChatMessage.Username + "','" + poChatMessage.Message + "';";

                loDB.openConnection();
                loDB.ExecDB(lsSQL);
                loDB.closeConnection();
            }
        }

        public static ChatMessageList GetChatMessageList()
        {
            ChatMessageList loChatMessageList = new ChatMessageList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                ChatMessage loChatMessage;
                DataTable loChatMessageTable;

                string lsSQL = "SELECT TOP 50 * FROM tblChatMessages ORDER BY id DESC";

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

                foreach (DataRow loDR in loChatMessageTable.Rows)
                {
                    loChatMessage = new ChatMessage();
                    loChatMessage.ID = Convert.ToInt16(loDR["id"]);
                    loChatMessage.Username = (string)loDR["username"];
                    loChatMessage.Message = (string)loDR["message"];
                    loChatMessage.Timestamp = (DateTime)loDR["timestamp"];

                    loChatMessageList.Add(loChatMessage);
                }
            }
            return loChatMessageList;
        }
    }
}



