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

/// <summary>
/// Summary description for C_myClass
/// </summary>
public class C_myClass
{
	public C_myClass()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static string GetErrorMessageCreateUser(MembershipCreateStatus status)
    {
        switch (status)
        {
            case MembershipCreateStatus.DuplicateUserName:
                return "نام کاربری قبلاً استفاده شده است ، لطفاً نام دیگری وارد فرمایید.";

            case MembershipCreateStatus.DuplicateEmail:
                return "کاربری با آدرس ایمیل وارد شده قبلاً وجود دارد ، لطفاً آدرس ایمیل دیگری وارد فرمایید.";

            case MembershipCreateStatus.InvalidPassword:
                return "فرمت پسورد وارد شده اشتباه است ، لطفاً مجدداً سعی کنید.";

            case MembershipCreateStatus.InvalidEmail:
                return "آدرس ایمیل وارد شده صحیح نمی باشد ، لطفاً مجدداً وارد فرمایید.";

            case MembershipCreateStatus.InvalidAnswer:
                return "The password retrieval answer provided is invalid. Please check the value and try again.";

            case MembershipCreateStatus.InvalidQuestion:
                return "The password retrieval question provided is invalid. Please check the value and try again.";

            case MembershipCreateStatus.InvalidUserName:
                return "The user name provided is invalid. Please check the value and try again.";

            case MembershipCreateStatus.ProviderError:
                return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

            case MembershipCreateStatus.UserRejected:
                return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

            default:
                return "یک خطای ناشناخته اتفاق افتاده است ، لطفاً مجدداً سعی فرمایید ، چنانچه برطرف نشد با شرکت پشتیبان تماس بگیرید.";
        }
    }

    public static string Links_LinksList(string SourcePage, string UserRole, Page curPage)
    {
        string ConnectionString = RamanConnectionString.MyConnectionString();
        SqlConnection Connection = new SqlConnection(ConnectionString);
        string CommandText = "select address, name, folder from t_LinksPanel where sourcePage = '" + SourcePage + "' AND userRole ='" + UserRole + "'";
        SqlCommand Command = new SqlCommand();
        Command.Connection = Connection;
        Command.CommandText = CommandText;
        SqlDataReader RecordSet;
        string Marquee = "", Name, Address, Folder;
        Connection.Open();
        RecordSet = Command.ExecuteReader();
        Marquee = "<marquee onmouseover=\"this.stop()\" onmouseout=\"this.start()\" direction=\"up\" scrolldelay=\"1\" scrollamount=\"1\" style=\"Width:100%;Height:100%;padding:3px\">";
        if (RecordSet.HasRows == true)
        {
            while (RecordSet.Read())
            {
                Address = RecordSet["address"].ToString();
                Name = RecordSet["name"].ToString();
                Folder = RecordSet["folder"].ToString();
                Marquee += String.Format("<p align=\"center\"><a href=\"{0}\" target=\"_self\" style=\"text-decoration:none; font-family:Tahoma; font-size:0.7em; font-style :normal; font-weight :bold; color:navy;\">{1}</a></p>", curPage.Server.MapPath("") + @"\" + Address, Name);
            }
        }
        else
        {
            Marquee += "<p align=\"center\" style=\"text-decoration:none; font-family:Tahoma; font-size:0.7em; font-style :normal; font-weight :bold; color:navy;\">لینکی ثبت نشده است</p>";
        }
        Marquee += "</marquee>";
        Connection.Close();
        RecordSet.Close();
        string filePath = @"C:\tttt.txt";
        File.WriteAllText(filePath, Marquee);
        return Marquee;
    }

    public static bool IsValidFileType(string FileName)
    {
        string[] AcceptedFileTypes = new string[] { "jpg", "gif", "jpeg" };
        string ext = FileName.Substring(FileName.LastIndexOf(".") + 1, FileName.Length - FileName.LastIndexOf(".") - 1);
        for (int i = 0; i < AcceptedFileTypes.Length; i++)
        {
            if (ext.ToLower() == AcceptedFileTypes[i])
            {
                return true;
            }
        }
        return false;
    }

    public static string FileExtenssion(string FileName)
    {
        string ext = FileName.Substring(FileName.LastIndexOf(".") + 1, FileName.Length - FileName.LastIndexOf(".") - 1);
        return ext;
    }

    public static void MenueItemsCreate(System.Security.Principal.IPrincipal CurUser, Panel ContainerPanel)
    {
        string conString = RamanConnectionString.MyConnectionString();
        SqlConnection con = new SqlConnection(conString);
        SqlCommand cmd = new SqlCommand();
        con.Open();
        cmd = con.CreateCommand();
        string userRole = "";
        List<LinkButton> listLinks = new List<LinkButton>();
        if (CurUser.Identity.IsAuthenticated)
        {
            if (CurUser.IsInRole("manager"))
            {
                userRole = "manager";
            }
            else if (CurUser.IsInRole("admin"))
            {
                userRole = "admin";
            }
            else if (CurUser.IsInRole("student"))
            {
                userRole = "student";
            }
            else if (CurUser.IsInRole("teacher"))
            {
                userRole = "teacher";
            }
            else if (CurUser.IsInRole("user"))
            {
                userRole = "user";
            }
        }
        else
        {
            userRole = "anonymouse";
        }
        cmd.CommandText = "select name , address from t_LinksPanel WHERE userRole = '" + userRole + "' ORDER BY position";
        SqlDataReader rdr = cmd.ExecuteReader();
        if (rdr.HasRows)
        {
            while (rdr.Read())
            {
                if ((rdr.GetValue(0) != DBNull.Value) && (rdr.GetValue(1) != DBNull.Value))
                {
                    string name = rdr.GetString(0);
                    string address = rdr.GetString(1);
                    LinkButton btnitem = new LinkButton();
                    btnitem.ID = name.Trim() + "Id";
                    btnitem.Text = name;
                    btnitem.PostBackUrl = @"~/" + address;
                    btnitem.Width = 160;
                    btnitem.Height = 22;
                    btnitem.CssClass = "menueLinks";
                    listLinks.Add(btnitem);
                }
            }
        }
        rdr.Close();
        for (int i = 0; i < listLinks.Count; i++)
        {
            ContainerPanel.Controls.Add(listLinks[i]);
        }
        ContainerPanel.Style.Add("padding-bottom", "10px");
    }

    public static string[] DateSplit(string date)
    {
        string[] dateSplit;
        System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex(@"\s+");
        string[] dateSplit1 = rgx.Split(date);
        if (dateSplit1[0] == "سه")
        {
            dateSplit = new string[dateSplit1.Length - 1];
            dateSplit[0] = "سه شنبه";
            for (int i = 2; i < dateSplit1.Length; i++)
            {
                dateSplit[i-1] = dateSplit1[i];
            }
        }
        else
        {
            dateSplit = dateSplit1;
        }
        
        return dateSplit;
    }

    public static void DeleteAllUsers()
    {
        try
        {
            MembershipUserCollection muColl = Membership.GetAllUsers();
            MembershipUser[] muArray = new MembershipUser[muColl.Count];
            muColl.CopyTo(muArray, 0);
            for (int i = 0; i < muArray.Length; i++)
            {
                MembershipUser mu = muArray[i];
                string userName = mu.UserName;
                Membership.DeleteUser(userName, true);
            }
        }
        catch
        {
        }
    }

    public static void SetUserOnline(string UserName)
    {
        try
        {
            string conString = RamanConnectionString.LocalConnectionString();
            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = con.CreateCommand();
            try
            {
                con.Open();
                cmd.CommandText = "insert into t_OnlineUsers (onlineUserName, onlineUserRole) values (N'" +
                    UserName + "', N'" +
                    "" + "')";
                cmd.ExecuteNonQuery();
            }
            catch
            {
            }
            finally
            {
                con.Close();
            }
        }
        catch
        {
        }
    }

    public static void SetUserOffline(string UserName)
    {
        try
        {
            string conString = RamanConnectionString.LocalConnectionString();
            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = con.CreateCommand();
            try
            {
                con.Open();
                cmd.CommandText = "DELETE FROM t_OnlineUsers Where onlineUserName = N'" + UserName + "'";
                cmd.ExecuteNonQuery();
            }
            catch
            {
            }
            finally
            {
                con.Close();
            }
        }
        catch
        {
        }
    }

    public static int OnlinesCount()
    {
        int count = 0;
        try
        {
            string conString = RamanConnectionString.LocalConnectionString();
            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = con.CreateCommand();
            try
            {
                con.Open();
                cmd.CommandText = "select Count(*) From t_OnlineUsers";
                count = (cmd.ExecuteScalar() != DBNull.Value ? (int)cmd.ExecuteScalar() : 0);
            }
            catch
            {
            }
            finally
            {
                con.Close();
            }
        }
        catch
        {
        }
        return count;
    }
}
