using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;

[DataObject(true)]
public class WebAuthorityData : SqlData
{
    public WebAuthorityData()
    {
        DataConnection = ConfigurationManager.ConnectionStrings["ConnString_WEBSecurity"].ConnectionString;
        TableName = "WebAuthority";
        //PrimaryKeyColumnNames = new string[] { "Account", "Page" };
    }


    private static string[] arPages = null;
    public static string[] GetPages {
        get {
            return arPages;
        }
        set {
        }
    }


    [DataObjectMethod(DataObjectMethodType.Select, true)]
    public string[] Select(string SelectField, string Condition) {
        DataTable dt = new DataTable(TableName);

        using (SqlConnection cn = new SqlConnection(DataConnection))
        using (SqlDataAdapter da = new SqlDataAdapter(
            "SELECT " + SelectField + " FROM  WebAuthority WHERE " + Condition, cn)) {
            da.Fill(dt);

            string[] ar = null;
            ar = new string[dt.Rows.Count];
            int i = 0;
            foreach (DataRow dr in dt.Rows) {
                ar[i] = dr.ItemArray[0].ToString();
                i++;
            }

            return ar;
        }
    }


    [DataObjectMethod(DataObjectMethodType.Insert, true)]
    public void Insert(string Account, string RoleOrUser, string Page) {
        using (SqlConnection cn = new SqlConnection(DataConnection))
        using (SqlCommand cm = new SqlCommand(
            "INSERT INTO " + TableName +
            " ([Account],[RoleOrUser],[Page]) " +
            " VALUES " +
            " (@Account, @RoleOrUser, @Page)", cn)) {
            cn.Open();

            cm.CommandType = CommandType.Text;
            cm.Parameters.AddWithValue("@Account", Account.Trim());
            cm.Parameters.AddWithValue("@RoleOrUser", RoleOrUser.Trim());
            cm.Parameters.AddWithValue("@Page", Page.Trim());

            cm.ExecuteNonQuery();

            cn.Close();
        }
    }



    [DataObjectMethod(DataObjectMethodType.Delete, true)]
    public void Delete(string Account) {
        using (SqlConnection cn = new SqlConnection(DataConnection))
        using (SqlCommand cm = new SqlCommand("DELETE " + TableName +
            " WHERE Account=@Account", cn)) {
            cn.Open();

            cm.CommandType = CommandType.Text;
            SqlParameter parameter = cm.Parameters.AddWithValue("@Account", Account);

            cm.ExecuteNonQuery();

            cn.Close();
        }
    }

    //================================================================================================

    public void UpdateEmail(string username, string email) {
        string sUid= "";

        using (SqlConnection cn = new SqlConnection(DataConnection))
        using (SqlCommand cm = new SqlCommand(
            "SELECT UserId FROM Users WHERE UserName='" + username + "'", cn)) {
            cn.Open();
            cm.CommandType = CommandType.Text;
            object result = cm.ExecuteScalar();

            if (result == null)
                return;

            sUid = result.ToString();

            cm.CommandText = "Update Membership Set Email='" + email + "' WHERE UserId='" + sUid + "'";
            cm.ExecuteNonQuery();

            cn.Close();
        }
    }

}
