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 _Default : System.Web.UI.Page
{
    //********************************
    SqlConnection con;
    SqlCommand cmd = new SqlCommand();
    //********************************
    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 = "صفحه اصلی";
                ///////////////////////////////////////
                string photoesFolder = Request.PhysicalApplicationPath + @"photoes\";
                imgbtnUserImage.ImageUrl = FindUserImageUrl(HttpContext.Current.User.Identity.Name);
                /*
                if (imgbtnUserImage.ImageUrl == photoesFolder + "noPersonalPhoto.gif")
                {
                    imgbtnUserImage.BorderStyle = BorderStyle.None;
                }
                else
                {
                    imgbtnUserImage.BorderStyle = BorderStyle.Solid;
                    imgbtnUserImage.BorderColor = System.Drawing.Color.Gray;
                    imgbtnUserImage.BorderWidth = 2;
                }
                 * */
                if (User.Identity.IsAuthenticated)
                {
                    Hashtable info = FindUserInfo(User.Identity.Name);
                    lblFullName.Text = "نام و نام خانوادگی : " + info["fullName"].ToString();
                    lblUserName.Text = "نام کاربری : " + User.Identity.Name;
                    btnMessages.Text = "صندوق ورودی پیام خصوصی : " + info["unreadCount"].ToString() + " خوانده نشده ، درمجموع " + info["allCount"].ToString();
                }
                else
                {
                    lblFullName.Text = "نام و نام خانوادگی : " + "کاربر مهمان";
                    lblUserName.Text = "نام کاربری : " + "کاربر مهمان";
                    btnMessages.Text = "صندوق ورودی پیام خصوصی : " + "0" + " خوانده نشده ، درمجموع " + "0";
                }
            }
        }
        catch
        {
        }
        finally
        {
        }
    }

    protected string FindImageUrl(object NewsPicUrl)
    {
        string url = "", picUrl = NewsPicUrl.ToString(), imagesFolder = Request.PhysicalApplicationPath + @"images\";
        try
        {
            if (File.Exists(imagesFolder + picUrl))
            {
                url = "~/images/" + picUrl;
            }
            else
            {
                url = "~/images/" + "nophotosquare.gif";
            }
        }
        catch
        {
        }
        return url;
    }

    private Hashtable FindUserInfo(string UserName)
    {
        int allCount = 0, unreadCount = 0;
        string fullName = "";
        Hashtable info = new Hashtable();
        try
        {
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            try
            {
                con.Open();
                if (Roles.IsUserInRole(UserName, "manager"))
                {
                    fullName = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Managers", "fullName", "userName", UserName);
                }
                else if (Roles.IsUserInRole(UserName, "admin"))
                {
                    fullName = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Admins", "fullName", "userName", UserName);
                }
                else if (Roles.IsUserInRole(UserName, "teacher"))
                {
                    fullName = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Teachers", "fullName", "userName", UserName);
                }
                else if (Roles.IsUserInRole(UserName, "student"))
                {
                    fullName = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Students", "fullName", "userName", UserName);
                }
                else if (Roles.IsUserInRole(UserName, "user"))
                {
                    fullName = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Users", "fullName", "userName", UserName);
                }
                cmd.CommandText = "select Count(*) from t_Messages Where recieverUserName = N'" + UserName + "' AND status = N'unread' AND recieverDelete = 0";
                unreadCount = (cmd.ExecuteScalar() != DBNull.Value ? (int)cmd.ExecuteScalar() : 0);
                cmd.CommandText = "select Count(*) from t_Messages Where recieverUserName = N'" + UserName + "' AND recieverDelete = 0";
                allCount = (cmd.ExecuteScalar() != DBNull.Value ? (int)cmd.ExecuteScalar() : 0);
                info["fullName"] = fullName;
                info["allCount"] = allCount;
                info["unreadCount"] = unreadCount;
            }
            catch
            {
            }
            finally
            {
                con.Close();
            }
        }
        catch
        {

        }
        return info;
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
        }
        catch
        {
        }
    }

    protected string GetNewsDetailsShowUrl(object NewsId)
    {
        int newsId = Int32.Parse(NewsId.ToString());
        string url = "~/pg_NewsDetailsShow.aspx?newsId=" + newsId.ToString() + "&returnUrl=Default.aspx";
        return url;
    }

    protected string GetNewsContext(string NewsContext)
    {
        string context = NewsContext;
        if (context.Length > 500)
            context = context.Substring(0, 500);
        context += " ...";
        return context;
    }

    private string FindUserImageUrl(string UserName)
    {
        string url = "";
        try
        {
            string picUrl = "", photoesFolder = Request.PhysicalApplicationPath + @"photoes\";
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            if (Roles.IsUserInRole(UserName, "manager"))
            {
                picUrl = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Managers", "managerPhoto", "userName", UserName);
            }
            else if (Roles.IsUserInRole(UserName, "admin"))
            {
                picUrl = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Admins", "adminPhotoUrl", "userName", UserName);
            }
            else if (Roles.IsUserInRole(UserName, "teacher"))
            {
                picUrl = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Teachers", "teacherPhotoUrl", "userName", UserName);
            }
            else if (Roles.IsUserInRole(UserName, "student"))
            {
                picUrl = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Students", "studentPhotoUrl", "userName", UserName);
            }
            else if (Roles.IsUserInRole(UserName, "user"))
            {
                picUrl = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Users", "userPhotoUrl", "userName", UserName);
            }
            if (File.Exists(photoesFolder + picUrl))
            {
                url = "~/photoes/" + picUrl;
            }
            else
            {
                url = "~/photoes/" + "noPersonalPhoto.gif";
            }
        }
        catch
        {
        }
        return url;
    }

    protected void btnMessages_Click(object sender, EventArgs e)
    {
        try
        {
            string UserName = User.Identity.Name;
            if (Roles.IsUserInRole(UserName, "manager"))
            {
                Response.Redirect("~/rl_admins/pg_MessagesInbox.aspx?returnUrl=pg_Messages.aspx");
            }
            else if (Roles.IsUserInRole(UserName, "admin"))
            {
                Response.Redirect("~/rl_admins/pg_MessagesInbox.aspx?returnUrl=pg_Messages.aspx");
            }
            else if (Roles.IsUserInRole(UserName, "teacher"))
            {
                Response.Redirect("~/rl_teachers/pg_MessagesInbox.aspx?returnUrl=pg_Messages.aspx");
            }
            else if (Roles.IsUserInRole(UserName, "student"))
            {
                Response.Redirect("~/rl_students/pg_MessagesInbox.aspx?returnUrl=pg_Messages.aspx");
            }
            else if (Roles.IsUserInRole(UserName, "user"))
            {
                Response.Redirect("~/rl_users/pg_MessagesInbox.aspx?returnUrl=~/Default.aspx");
            }
        }
        catch
        {
        }
    }

    protected string FindNewsContext(object Context)
    {
        string context = "", inputSt = Context.ToString();
        try
        {
            if (inputSt.Length > 120)
                context = inputSt.Substring(0, 120) + " ...";
            else
                context = inputSt;
        }
        catch
        {
        }
        return context;
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        try
        {
            if (GridNews.PageCount > 1)
            {
                if (GridNews.PageIndex < GridNews.PageCount - 1)
                    GridNews.PageIndex += 1;
                else
                    GridNews.PageIndex = 0;
                UpdatePanel1.Update();
            }
        }
        catch
        {
        }
    }
    protected void GridNews_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "ShowNews")
            {
                int newsId = Int32.Parse(e.CommandArgument.ToString());
                Response.Redirect("~/pg_NewsDetailsShow.aspx?newsId=" + newsId.ToString() + "&returnUrl=Default.aspx");
            }
        }
        catch
        {
        }
    }
}
