using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using System.Data.SqlClient;
using System.IO;

public partial class rl_allusers_pg_OnlineUsers : System.Web.UI.Page
{
    //***********************************************
    SqlConnection con;
    SqlCommand cmd;
    private static DataTable dt;
    //***********************************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                C_myClass.MenueItemsCreate(User, panelMenues);
                Label lblTitle = (Label)this.Master.FindControl("lblTitle");
                lblTitle.Text = "کاربران آنلاین";
                ///////////////////////////////////////
                System.Collections.Generic.List<string> onlineUserNames = new System.Collections.Generic.List<string>();
                onlineUserNames = FindOnlineUserNames();
                System.Collections.Generic.List<Hashtable> onlinesInfo = new System.Collections.Generic.List<Hashtable>();
                onlinesInfo = FindOnlinesInformation(onlineUserNames);
                dt = new DataTable();
                dt.Columns.Add("photoUrl");
                dt.Columns.Add("fullName");
                dt.Columns.Add("userName");
                dt.Columns.Add("userRole");
                dt = FillDataTable(dt, onlinesInfo);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
        catch(Exception ex)
        {
        }
    }

    private DataTable FillDataTable(DataTable DT, System.Collections.Generic.List<Hashtable> Onlines)
    {
        DataTable table = DT;
        try
        {
            string photoesFolder = Request.PhysicalApplicationPath + @"photoes\";
            for (int i = 0; i < Onlines.Count; i++)
            {
                Hashtable info = Onlines[i];
                DataRow dr = table.NewRow();
                dr["photoUrl"] = (File.Exists(photoesFolder + info["photoUrl"].ToString()) ? "~/photoes/" + info["photoUrl"].ToString() : "~/photoes/" + "noPersonalPhoto.gif");
                dr["fullName"] = info["fullName"].ToString();
                dr["userName"] = info["userName"].ToString();
                dr["userRole"] = info["userRole"].ToString();
                table.Rows.Add(dr);
            }
        }
        catch
        {
        }
        return table;
    }

    private System.Collections.Generic.List<string> FindOnlineUserNames()
    {
        System.Collections.Generic.List<string> onlineUserNames = new System.Collections.Generic.List<string>();
        try
        {
            MembershipUserCollection usersColl = Membership.GetAllUsers();
            MembershipUser[] allusersArray = new MembershipUser[usersColl.Count];
            usersColl.CopyTo(allusersArray, 0);
            for (int i = 0; i < allusersArray.Length; i++)
            {
                MembershipUser user = allusersArray[i];
                if (OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.IsOnline(user.UserName))
                {
                    onlineUserNames.Add(user.UserName);
                }
            }
        }
        catch
        {
        }
        return onlineUserNames;
    }

    private System.Collections.Generic.List<Hashtable> FindOnlinesInformation(System.Collections.Generic.List<string> UserNames)
    {
        System.Collections.Generic.List<Hashtable> infos = new System.Collections.Generic.List<Hashtable>();
        try
        {
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            try
            {
                con.Open();
                for (int i = 0; i < UserNames.Count; i++)
                {
                    string userName = UserNames[i];
                    if (Roles.IsUserInRole(userName, "manager"))
                    {
                        string fullName = "", photoUrl = "";
                        cmd.CommandText = "select fullName, managerPhoto from t_Managers Where userName = N'" + userName + "'";
                        SqlDataReader rdr = cmd.ExecuteReader();
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                fullName = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                                photoUrl = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                            }
                        }
                        rdr.Close();
                        Hashtable userInfo = new Hashtable();
                        userInfo["userName"] = userName;
                        userInfo["fullName"] = fullName;
                        userInfo["photoUrl"] = photoUrl;
                        userInfo["userRole"] = "manager";
                        infos.Add(userInfo);
                    }
                    else if (Roles.IsUserInRole(userName, "admin"))
                    {
                        string fullName = "", photoUrl = "";
                        cmd.CommandText = "select fullName, adminPhotoUrl from t_Admins Where userName = N'" + userName + "'";
                        SqlDataReader rdr = cmd.ExecuteReader();
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                fullName = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                                photoUrl = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                            }
                        }
                        rdr.Close();
                        Hashtable userInfo = new Hashtable();
                        userInfo["userName"] = userName;
                        userInfo["fullName"] = fullName;
                        userInfo["photoUrl"] = photoUrl;
                        userInfo["userRole"] = "admin";
                        infos.Add(userInfo);
                    }
                    else if (Roles.IsUserInRole(userName, "teacher"))
                    {
                        string fullName = "", photoUrl = "";
                        cmd.CommandText = "select fullName, teacherPhotoUrl from t_Teachers Where userName = N'" + userName + "'";
                        SqlDataReader rdr = cmd.ExecuteReader();
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                fullName = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                                photoUrl = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                            }
                        }
                        rdr.Close();
                        Hashtable userInfo = new Hashtable();
                        userInfo["userName"] = userName;
                        userInfo["fullName"] = fullName;
                        userInfo["photoUrl"] = photoUrl;
                        userInfo["userRole"] = "teacher";
                        infos.Add(userInfo);
                    }
                    else if (Roles.IsUserInRole(userName, "student"))
                    {
                        string fullName = "", photoUrl = "";
                        cmd.CommandText = "select fullName, studentPhotoUrl from t_Students Where userName = N'" + userName + "'";
                        SqlDataReader rdr = cmd.ExecuteReader();
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                fullName = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                                photoUrl = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                            }
                        }
                        rdr.Close();
                        Hashtable userInfo = new Hashtable();
                        userInfo["userName"] = userName;
                        userInfo["fullName"] = fullName;
                        userInfo["photoUrl"] = photoUrl;
                        userInfo["userRole"] = "student";
                        infos.Add(userInfo);
                    }
                    else if (Roles.IsUserInRole(userName, "user"))
                    {
                        string fullName = "", photoUrl = "";
                        cmd.CommandText = "select fullName, userPhotoUrl from t_Users Where userName = N'" + userName + "'";
                        SqlDataReader rdr = cmd.ExecuteReader();
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                fullName = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                                photoUrl = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                            }
                        }
                        rdr.Close();
                        Hashtable userInfo = new Hashtable();
                        userInfo["userName"] = userName;
                        userInfo["fullName"] = fullName;
                        userInfo["photoUrl"] = photoUrl;
                        userInfo["userRole"] = "user";
                        infos.Add(userInfo);
                    }
                }
            }
            catch
            {
                con.Close();
            }
        }
        catch
        {
        }
        return infos;
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "SendMessage")
            {
                string userName = e.CommandArgument.ToString();
                string recieverRole = (Roles.IsUserInRole(userName, "manager") ? "manager" : (Roles.IsUserInRole(userName, "admin") ? "admin" : (Roles.IsUserInRole(userName, "teacher") ? "teacher" : (Roles.IsUserInRole(userName, "student") ? "student" : (Roles.IsUserInRole(userName, "user") ? "user" : "")))));
                int id = FindID(userName);
                Response.Redirect("pg_MessageSendNew.aspx?recieverRole=" + recieverRole + "&id=" + id + "&recieverUserName=" + userName + "&returnUrl=pg_OnlineUsers.aspx");
            }
        }
        catch
        {
        }
    }

    private int FindID(string UserName)
    {
        int id = 0;
        try
        {
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            if (Roles.IsUserInRole(UserName, "manager"))
            {
                id = _UseFull.C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Managers", "managerId", "userName", UserName);
            }
            else if (Roles.IsUserInRole(UserName, "admin"))
            {
                id = _UseFull.C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Admins", "adminId", "userName", UserName);
            }
            else if (Roles.IsUserInRole(UserName, "teacher"))
            {
                id = _UseFull.C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Teachers", "teacherId", "userName", UserName);
            }
            else if (Roles.IsUserInRole(UserName, "student"))
            {
                id = _UseFull.C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Students", "studentId", "userName", UserName);
            }
            else if (Roles.IsUserInRole(UserName, "user"))
            {
                id = _UseFull.C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Users", "userId", "userName", UserName);
            }
        }
        catch
        {
        }
        return id;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        catch
        {
        }
    }
}
