using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

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

        public UserOnlineManagement()
        { }

        public static void InsertUserOnline(string sessionId)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

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

        public static void UpdateUserOnline(string username, string sessionId)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

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

                string lsSQL;

                if (username == null)
                {
                    lsSQL = "EXEC spUpdateUsernameOnline null,'" + sessionId + "';";
                }
                else
                {
                    lsSQL = "EXEC spUpdateUsernameOnline '" + username + "','" + sessionId + "';";
                }

                loDB.openConnection();
                loDB.ExecDB(lsSQL);
                loDB.closeConnection();
            }
            else
            {
                // ToDo
            }
        }

        public static void DeleteUserOnline(string sessionId)
        {
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = "EXEC spDeleteUserOnline '" + sessionId + "';";

                loDB.openConnection();
                loDB.ExecDB(lsSQL);
                loDB.closeConnection();
            }
            else
            {
                // ToDo
            }
        }

        public static Int16 GetTotalUsersOnline(string psUsername, string psSessionID)
        {                
            Int16 lsTotalUserCount = 0;
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL;
                if (psUsername == String.Empty)
                {
                    lsSQL = "EXEC spSubmitSessionInfo null,'" + psSessionID + "';";
                }
                else
                {
                    lsSQL = "EXEC spSubmitSessionInfo '" + psUsername + "','" + psSessionID + "';"; ;
                }

                loDB.openConnection();
                lsTotalUserCount = Convert.ToInt16(loDB.ExecDBScalar(lsSQL).ToString());
                loDB.closeConnection();
            }
            else
            {
                // ToDo
            }
            return lsTotalUserCount;
        }

        public static string GetUsersOnline()
        {
            int count = 0;
            string usersOnline = string.Empty;
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                string lsSQL = "SELECT username FROM tblUsersOnline where username IS NOT NULL;";

                loDB.openConnection();
                DataTable loUserTable = loDB.QueryDBDataset(lsSQL);

                for (int i = 0; i <= loUserTable.Rows.Count - 1; i++)
                {
                    if (loUserTable.Rows[i].ItemArray[0].ToString() != "")
                    {
                        count++;
                        if (count <= 1)
                        {
                            usersOnline += loUserTable.Rows[i].ItemArray[0].ToString();
                        }
                        else if (count > 1)
                        {
                            if (usersOnline.IndexOf(loUserTable.Rows[i].ItemArray[0].ToString()) < 0)
                            {
                                usersOnline += ", " + loUserTable.Rows[i].ItemArray[0].ToString();
                            }
                        }
                    }
                }
                loDB.closeConnection();
            }
            else
            {
                // ToDo
            }
            
            return usersOnline;
        }
    }
}