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.Collections.Generic;

public partial class rl_admins_pg_MessageSendToRole : System.Web.UI.Page
{
    //**********************
    SqlConnection con;
    SqlCommand cmd;
    SqlTransaction tr;
    //**********************
    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 = "ارسال پیام به نقش خاص";
            }
        }
        catch
        {
        }
        finally
        {
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        try
        {
            string returnUrl = Request.QueryString["returnUrl"].ToString();
            Response.Redirect(returnUrl);
        }
        catch
        {
        }
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            if ((cmbRecieverRole.SelectedIndex > 0) && (tbSubject.Text.Length > 0) && (tbContext.Text.Length > 0))
            {
                string recieverRole = (cmbRecieverRole.SelectedIndex > 0 ? (cmbRecieverRole.SelectedIndex == 1 ? "admin" : (cmbRecieverRole.SelectedIndex == 2 ? "teacher" : (cmbRecieverRole.SelectedIndex == 3 ? "student" : (cmbRecieverRole.SelectedIndex == 4 ? "user" : "")))) : "");
                string senderRole = (Roles.IsUserInRole("manager") ? "manager" : (Roles.IsUserInRole("admin") ? "admin" : (Roles.IsUserInRole("teacher") ? "teacher" : (Roles.IsUserInRole("student") ? "student" : (Roles.IsUserInRole("user") ? "user" : "")))));
                string senderFullName = Session["fullName"].ToString();
                string senderUserName = User.Identity.Name;
                string subject = tbSubject.Text;
                string context = tbContext.Text;
                string dateOfSend = _UseFull.C_Usefull.FarsiShamsiDate(DateTime.Now);
                string status = "unread";
                bool senderDelete = false;
                bool recieverDelete = false;
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    tr = con.BeginTransaction();
                    cmd.Transaction = tr;
                    if (recieverRole == "admin")
                    {
                        SendMessageToAdmins(senderRole, senderFullName, senderUserName, subject, context, dateOfSend, status, senderDelete, recieverDelete);
                    }
                    else if (recieverRole == "teacher")
                    {
                        SendMessageToTeachers(senderRole, senderFullName, senderUserName, subject, context, dateOfSend, status, senderDelete, recieverDelete);
                    }
                    else if (recieverRole == "student")
                    {
                        SendMessageToStudents(senderRole, senderFullName, senderUserName, subject, context, dateOfSend, status, senderDelete, recieverDelete);
                    }
                    else if (recieverRole == "user")
                    {
                        SendMessageToUsers(senderRole, senderFullName, senderUserName, subject, context, dateOfSend, status, senderDelete, recieverDelete);
                    }
                    //////////////////////////
                    tr.Commit();
                    lblMessage.ForeColor = System.Drawing.Color.Green;
                    lblMessage.Text = "با موفقیت اسال شد.";
                }
                catch (Exception ex)
                {
                    lblMessage.ForeColor = System.Drawing.Color.Green;
                    lblMessage.Text = "خطا در ارسال پیام.";
                }
                finally
                {
                    con.Close();
                }
            }
        }
        catch
        {
        }
    }

    private void SendMessageToAdmins(string SenderRole, string SenderFullName, string SenderUserName, string Subject, string Context, string DateOfSend, string Status, bool SenderDelete, bool RecieverDelete)
    {
        List<Hashtable> admins = new List<Hashtable>();
        Hashtable adminInfo;
        string RecieverRole = "admin", RecieverFullName = "", RecieverUserName = "";
        cmd.CommandText = "select fullName, userName FROM t_Admins";
        SqlDataReader rdr = cmd.ExecuteReader();
        if (rdr.HasRows)
        {
            while (rdr.Read())
            {
                adminInfo = new Hashtable();
                adminInfo["fullName"] = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                adminInfo["userName"] = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                admins.Add(adminInfo);
            }
        }
        rdr.Close();
        for (int i = 0; i < admins.Count; i++)
        {
            Hashtable info = admins[i];
            RecieverFullName = info["fullName"].ToString();
            RecieverUserName = info["userName"].ToString();
            cmd.CommandText = "INSERT INTO t_Messages (senderRole, senderFullName, senderUserName, recieverRole, recieverFullName, recieverUserName, subject, context, dateOfSend, status, senderDelete, recieverDelete) values (N'" +
                    SenderRole + "', N'" +
                    SenderFullName + "', N'" +
                    SenderUserName + "', N'" +
                    RecieverRole + "', N'" +
                    RecieverFullName + "', N'" +
                    RecieverUserName + "', N'" +
                    Subject + "', N'" +
                    Context + "', N'" +
                    DateOfSend + "', N'" +
                    Status + "', " +
                    (SenderDelete ? 1 : 0) + " , " +
                    (RecieverDelete ? 1 : 0) + " )";
            cmd.ExecuteNonQuery();
        }
    }

    private void SendMessageToTeachers(string SenderRole, string SenderFullName, string SenderUserName, string Subject, string Context, string DateOfSend, string Status, bool SenderDelete, bool RecieverDelete)
    {
        List<Hashtable> teachers = new List<Hashtable>();
        Hashtable teacherInfo;
        string RecieverRole = "teacher", RecieverFullName = "", RecieverUserName = "";
        cmd.CommandText = "select fullName, userName FROM t_Teachers";
        SqlDataReader rdr = cmd.ExecuteReader();
        if (rdr.HasRows)
        {
            while (rdr.Read())
            {
                teacherInfo = new Hashtable();
                teacherInfo["fullName"] = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                teacherInfo["userName"] = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                teachers.Add(teacherInfo);
            }
        }
        rdr.Close();
        for (int i = 0; i < teachers.Count; i++)
        {
            Hashtable info = teachers[i];
            RecieverFullName = info["fullName"].ToString();
            RecieverUserName = info["userName"].ToString();
            cmd.CommandText = "INSERT INTO t_Messages (senderRole, senderFullName, senderUserName, recieverRole, recieverFullName, recieverUserName, subject, context, dateOfSend, status, senderDelete, recieverDelete) values (N'" +
                    SenderRole + "', N'" +
                    SenderFullName + "', N'" +
                    SenderUserName + "', N'" +
                    RecieverRole + "', N'" +
                    RecieverFullName + "', N'" +
                    RecieverUserName + "', N'" +
                    Subject + "', N'" +
                    Context + "', N'" +
                    DateOfSend + "', N'" +
                    Status + "', " +
                    (SenderDelete ? 1 : 0) + " , " +
                    (RecieverDelete ? 1 : 0) + " )";
            cmd.ExecuteNonQuery();
        }
    }

    private void SendMessageToStudents(string SenderRole, string SenderFullName, string SenderUserName, string Subject, string Context, string DateOfSend, string Status, bool SenderDelete, bool RecieverDelete)
    {
        List<Hashtable> students = new List<Hashtable>();
        Hashtable studentInfo;
        string RecieverRole = "student", RecieverFullName = "", RecieverUserName = "";
        cmd.CommandText = "select fullName, userName FROM t_Students";
        SqlDataReader rdr = cmd.ExecuteReader();
        if (rdr.HasRows)
        {
            while (rdr.Read())
            {
                studentInfo = new Hashtable();
                studentInfo["fullName"] = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                studentInfo["userName"] = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                students.Add(studentInfo);
            }
        }
        rdr.Close();
        for (int i = 0; i < students.Count; i++)
        {
            Hashtable info = students[i];
            RecieverFullName = info["fullName"].ToString();
            RecieverUserName = info["userName"].ToString();
            cmd.CommandText = "INSERT INTO t_Messages (senderRole, senderFullName, senderUserName, recieverRole, recieverFullName, recieverUserName, subject, context, dateOfSend, status, senderDelete, recieverDelete) values (N'" +
                    SenderRole + "', N'" +
                    SenderFullName + "', N'" +
                    SenderUserName + "', N'" +
                    RecieverRole + "', N'" +
                    RecieverFullName + "', N'" +
                    RecieverUserName + "', N'" +
                    Subject + "', N'" +
                    Context + "', N'" +
                    DateOfSend + "', N'" +
                    Status + "', " +
                    (SenderDelete ? 1 : 0) + " , " +
                    (RecieverDelete ? 1 : 0) + " )";
            cmd.ExecuteNonQuery();
        }
    }

    private void SendMessageToUsers(string SenderRole, string SenderFullName, string SenderUserName, string Subject, string Context, string DateOfSend, string Status, bool SenderDelete, bool RecieverDelete)
    {
        List<Hashtable> users = new List<Hashtable>();
        Hashtable userInfo;
        string RecieverRole = "user", RecieverFullName = "", RecieverUserName = "";
        cmd.CommandText = "select fullName, userName FROM t_Users";
        SqlDataReader rdr = cmd.ExecuteReader();
        if (rdr.HasRows)
        {
            while (rdr.Read())
            {
                userInfo = new Hashtable();
                userInfo["fullName"] = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                userInfo["userName"] = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                users.Add(userInfo);
            }
        }
        rdr.Close();
        for (int i = 0; i < users.Count; i++)
        {
            Hashtable info = users[i];
            RecieverFullName = info["fullName"].ToString();
            RecieverUserName = info["userName"].ToString();
            cmd.CommandText = "INSERT INTO t_Messages (senderRole, senderFullName, senderUserName, recieverRole, recieverFullName, recieverUserName, subject, context, dateOfSend, status, senderDelete, recieverDelete) values (N'" +
                    SenderRole + "', N'" +
                    SenderFullName + "', N'" +
                    SenderUserName + "', N'" +
                    RecieverRole + "', N'" +
                    RecieverFullName + "', N'" +
                    RecieverUserName + "', N'" +
                    Subject + "', N'" +
                    Context + "', N'" +
                    DateOfSend + "', N'" +
                    Status + "', " +
                    (SenderDelete ? 1 : 0) + " , " +
                    (RecieverDelete ? 1 : 0) + " )";
            cmd.ExecuteNonQuery();
        }
    }

}
