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 _UseFull;

public partial class MasterPage : System.Web.UI.MasterPage
{
    //**************************
    SqlConnection con;
    SqlCommand cmd = new SqlCommand();
    //*************************
    protected string WellComeString(System.Security.Principal.IPrincipal User)
    {
        string wellcomeString = "";
        try
        {
            string conString = RamanConnectionString.MyConnectionString();
            con = new SqlConnection(conString);
            cmd.Connection = con;
            if (User.Identity.IsAuthenticated)
            {
                string fullName = "", gender = "";
                int iden = 0;
                if (User.IsInRole("manager"))
                {
                    gender = C_Usefull.FindStringWithStringShart(con, cmd, "t_Managers", "gender", "userName", User.Identity.Name);
                    fullName = C_Usefull.FindStringWithStringShart(con, cmd, "t_Managers", "fullName", "userName", User.Identity.Name);
                    iden = C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Managers", "managerId", "userName", User.Identity.Name);
                    wellcomeString = (gender == "مرد" ? "آقای" : (gender == "زن" ? "خانم" : "")) + "<b>" + fullName + "</b>" + " خوش آمدید ، نقش شما در سایت : مدیر سایت.";
                }
                else if (User.IsInRole("admin"))
                {
                    gender = C_Usefull.FindStringWithStringShart(con, cmd, "t_Admins", "gender", "userName", User.Identity.Name);
                    fullName = C_Usefull.FindStringWithStringShart(con, cmd, "t_Admins", "fullName", "userName", User.Identity.Name);
                    iden = C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Admins", "adminId", "userName", User.Identity.Name);
                    wellcomeString = (gender == "مرد" ? "آقای" : (gender == "زن" ? "خانم" : "")) + " " + "<b>" + fullName + "</b>" + " خوش آمدید ، نقش شما در سایت : کارمند آموزشگاه.";
                }
                else if (User.IsInRole("student"))
                {
                    gender = C_Usefull.FindStringWithStringShart(con, cmd, "t_Students", "gender", "userName", User.Identity.Name);
                    fullName = C_Usefull.FindStringWithStringShart(con, cmd, "t_Students", "fullName", "userName", User.Identity.Name);
                    iden = C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Students", "studentId", "userName", User.Identity.Name);
                    wellcomeString = (gender == "مرد" ? "آقای" : (gender == "زن" ? "خانم" : "")) + " " + "<b>" + fullName + "</b>" + " خوش آمدید ، نقش شما در سایت : دانشجو.";
                }
                else if (User.IsInRole("teacher"))
                {
                    gender = C_Usefull.FindStringWithStringShart(con, cmd, "t_Teachers", "gender", "userName", User.Identity.Name);
                    fullName = C_Usefull.FindStringWithStringShart(con, cmd, "t_Teachers", "fullName", "userName", User.Identity.Name);
                    iden = C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Teachers", "teacherId", "userName", User.Identity.Name);
                    wellcomeString = (gender == "مرد" ? "آقای" : (gender == "زن" ? "خانم" : "")) + " " + "<b>" + fullName + "</b>" + " خوش آمدید ، نقش شما در سایت : مدرس.";
                }
                else if (User.IsInRole("user"))
                {
                    gender = C_Usefull.FindStringWithStringShart(con, cmd, "t_Users", "gender", "userName", User.Identity.Name);
                    fullName = C_Usefull.FindStringWithStringShart(con, cmd, "t_Users", "fullName", "userName", User.Identity.Name);
                    iden = C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Users", "userId", "userName", User.Identity.Name);
                    wellcomeString = (gender == "مرد" ? "آقای" : (gender == "زن" ? "خانم" : "")) + " " + "<b>" + fullName + "</b>" + " خوش آمدید ، نقش شما در سایت : کاربر سایت.";
                }
                Session["fullName"] = fullName;
                Session["gender"] = gender;
                Session["iden"] = iden;
            }
            else
            {
                wellcomeString = "خوش آمدید ، برای عضویت در سایت می توانید از طریق آموزشگاه اقدام فرمایید.";
            }
        }
        catch
        {
            wellcomeString = "";
        }
        return wellcomeString;
    }
    protected void btnMessages_Click(object sender, EventArgs e)
    {
        try
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                if (Roles.IsUserInRole("manager"))
                {
                    Response.Redirect("~/rl_admins/pg_Messages.aspx");
                }
                else if (Roles.IsUserInRole("admin"))
                {
                    Response.Redirect("~/rl_admins/pg_Messages.aspx");
                }
                else if (Roles.IsUserInRole("student"))
                {
                    Response.Redirect("~/rl_students/pg_Messages.aspx");
                }
                else if (Roles.IsUserInRole("teacher"))
                {
                    Response.Redirect("~/rl_teachers/pg_Messages.aspx");
                }
                else if (Roles.IsUserInRole("user"))
                {
                }
            }
        }
        catch
        {
        }
    }

    protected string FindLiginText()
    {
        string comm = "";
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            comm = "خروج";
        }
        else
        {
            comm = "ورود";
        }
        return comm;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            int guestsCount = OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.GuestUsersCount;
            int usersCount = OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.RegistredUsersCount;
            btnUsersCount.Text = usersCount.ToString() + " کاربر و " + guestsCount.ToString() + " مهمان";
        }
        catch
        {
        }
    }

    protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
    {
        try
        {
            OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.SetUserOffline(HttpContext.Current.User.Identity.Name);
        }
        catch
        {
        }
    }
    protected void btnUsersCount_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("~/rl_allusers/pg_OnlineUsers.aspx");
        }
        catch
        {
        }
    }
}

