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 ChatUsersOnlineManagement
    {
        private static PortalSettings coPortalSettings;

        public static void SaveChatUsersOnline(string psUsername)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

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

                string lsSQL = "EXEC spUpdateUserOnline '" + psUsername + "';";

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

        public static ChatUsersOnlineList GetChatUsersOnlineList()
        {
            ChatUsersOnlineList loChatUsersOnlineList = new ChatUsersOnlineList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                ChatUsersOnline loChatUsersOnline;
                DataTable loChatUsersOnlineTable;

                string lsSQL = "SELECT * FROM tblChatUsersOnline;";

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

                foreach (DataRow loDR in loChatUsersOnlineTable.Rows)
                {
                    loChatUsersOnline = new ChatUsersOnline();
                    loChatUsersOnline.ID = Convert.ToInt16(loDR["id"]);
                    loChatUsersOnline.Username = (string)loDR["username"];
                    loChatUsersOnline.Timestamp = (DateTime)loDR["timestamp"];

                    loChatUsersOnlineList.Add(loChatUsersOnline);
                }
            }
            return loChatUsersOnlineList;
        }

        public static void DeleteChatUserOnline(string psUserName)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

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

                string lsSQL = "EXEC spDeleteChatUserOnline '" + psUserName + "';";

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


