using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data.OleDb;


public partial class admin_DefaultAdmin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void stats_Click(object sender, EventArgs e)
    {
        //regularexpression for ID field
        RegularExpressionValidator1.Validate();
        if (RegularExpressionValidator1.IsValid == false) return;

        //databinding
        DetailsView2.DataBind();
        GridView1.DataBind();

        //visibilty for some objects
        DetailsView2.Visible = true;
        GridView2.Visible = false;
        darth.Visible = false;
    }

    protected void delete_Click(object sender, EventArgs e)
    {
        //regularexpression for ID field
        RegularExpressionValidator1.Validate();
        if (RegularExpressionValidator1.IsValid == false) return;
       
        //darthVader invisible
        darth.Visible = false;

        OleDbConnection conn = null;
        OleDbCommand command = null;
        String userId = id.Text;

        //you can't delete Andrea! :)
        if (id.Text == "113")
        {
            darth.Visible = true;
            Label1.Text = "Luke, non puoi Scancellare tuo padre...";
            return;
        }

        string another = WebConfigurationManager.ConnectionStrings["AccessFileName"].ToString();
     
        //***Deleting user from some tables
        try
        {
            RegularExpressionValidator1.Validate();
            if (RegularExpressionValidator1.IsValid == false) return;
            darth.Visible = false;
            conn = new OleDbConnection(another);
            conn.Open();
            command = new OleDbCommand(@"DELETE FROM aspnet_Membership WHERE UserId = @UserId", conn);
            command.Parameters.Add(new OleDbParameter("@UserId", userId));
            int added = 0;
            try
            {
                added = command.ExecuteNonQuery();
                Label1.Text = added.ToString() + " record deleted. <br />";
            }
            catch (Exception err)
            {
                Label1.Text = "Error deleting record. <br />";
                Label1.Text += "Error type: " + err.Message;
            }
            command = new OleDbCommand(@"DELETE FROM aspnet_UsersInRoles WHERE UserId = @UserId", conn);
            command.Parameters.Add(new OleDbParameter("@UserId", userId));
            try
            {
                added = command.ExecuteNonQuery();
                Label1.Text = added.ToString() + " record deleted.<br />";
            }
            catch (Exception err)
            {
                Label1.Text = "Error deleting record. <br />";
                Label1.Text += "Error type: " + err.Message;
            }

            command = new OleDbCommand(@"DELETE FROM aspnet_Profile WHERE UserId = @UserId", conn);
            command.Parameters.Add(new OleDbParameter("@UserId", userId));
            try
            {
                added = command.ExecuteNonQuery();
                Label1.Text = added.ToString() + " record deleted.<br />";
            }
            catch (Exception err)
            {
                Label1.Text = "Error deleting record. <br />";
                Label1.Text += "Error type: " + err.Message;
            }
            command = new OleDbCommand(@"DELETE FROM aspnet_PagePersonalizationPerUser WHERE UserId = @UserId", conn);
            command.Parameters.Add(new OleDbParameter("@UserId", userId));
            try
            {
                added = command.ExecuteNonQuery();
                Label1.Text = added.ToString() + " record delete.<br />";
            }
            catch (Exception err)
            {
                Label1.Text = "Error deleting record. <br />";
                Label1.Text += "Error type: " + err.Message;
            }
            command = new OleDbCommand(@"DELETE FROM aspnet_Users WHERE UserId = @UserId", conn);
            command.Parameters.Add(new OleDbParameter("@UserId", userId));
            try
            {
                added = command.ExecuteNonQuery();
                Label1.Text = added.ToString() + " record deleted.<br />";
            }
            catch (Exception err)
            {
                Label1.Text = "Error deleting record. <br />";
                Label1.Text += "Error type: " + err.Message;
            }
        }
        finally
        {
            if (conn != null)
            {
                conn.Close();
                Label1.Text = "<br />Connection with db is " + conn.State.ToString();
            }
        }
        GridView1.DataBind();
        DetailsView2.DataBind();
    }
    

    protected void AddAdmin_Click(object sender, EventArgs e)
    {

        darth.Visible = false;
        RegularExpressionValidator1.Validate();
        if (RegularExpressionValidator1.IsValid == false) return;
        OleDbConnection conn = null;
        OleDbCommand command = null;
        String userId = id.Text;
        string another = WebConfigurationManager.ConnectionStrings["AccessFileName"].ToString();
      
        //Do it exist?
        try
        {
            conn = new OleDbConnection(another);
            conn.Open();

            command = new OleDbCommand(@"SELECT UserId FROM aspnet_UsersInRoles WHERE UserId = @UserId AND RoleId = @RoleId", conn);
            command.Parameters.Add(new OleDbParameter("@UserId", userId));
            command.Parameters.Add(new OleDbParameter("@RoleId", 18));
            object result = command.ExecuteScalar();
            if (result != null && (result is int) && ((int)result) == Convert.ToInt32(userId))
            {
                // Exists!
                Label1.Text = "utente già amministratore <br />";
                return;
            }

            //It doesn't exist so I put in UsersInRoles table
            command = new OleDbCommand(@"INSERT INTO aspnet_UsersInRoles (UserId, RoleId) VALUES(@UserId, @RoleId)", conn);
            command.Parameters.Add(new OleDbParameter("@UserId", userId));
            //18 is for administrators
            command.Parameters.Add(new OleDbParameter("@UserId", 18));

            int added = 0;
            try
            {
                added = command.ExecuteNonQuery();
                Label1.Text = added.ToString() + " record updated. <br />";
            }
            catch (Exception err)
            {
                Label1.Text = "Error updating record. <br />";
                Label1.Text += "Error type: " + err.Message;
            }
        }
        finally
        {
            if (conn != null)
            {
                conn.Close();
                Label1.Text = "<br />Connection with db is " + conn.State.ToString();
            }
        }
        DetailsView2.DataBind();
    }




    protected void remadmin_Click(object sender, EventArgs e)
    {
        darth.Visible = false;
        OleDbConnection conn = null;
        OleDbCommand command = null;
        String userId = id.Text;
        RegularExpressionValidator1.Validate();
        if (RegularExpressionValidator1.IsValid == false) return;

        //you can't rem admin role for Andrea :)
        if (id.Text == "113")
        {
            darth.Visible = true;
            Label1.Text = "Luke, non puoi Scancellare tuo padre...";
            return;
        }

        //deleting user from UserInRoles
        string another = WebConfigurationManager.ConnectionStrings["AccessFileName"].ToString();
        try
        {
            conn = new OleDbConnection(another);
            conn.Open();
            command = new OleDbCommand(@"DELETE FROM aspnet_UsersInRoles WHERE (UserId = @UserId)", conn);
            command.Parameters.Add(new OleDbParameter("@UserId", userId));
            int added = 0;
            try
            {
                added = command.ExecuteNonQuery();
                Label1.Text = added.ToString() + " record updated. <br />";
            }
            catch (Exception err)
            {
                Label1.Text = "Error updating record. <br />";
                Label1.Text += "Error type: " + err.Message;
            }
        }
        finally
        {
            if (conn != null)
            {
                conn.Close();
                Label1.Text += "<br />Connection with db is " + conn.State.ToString();
            }
        }
        DetailsView2.DataBind();
    }

    protected void ShowStream_Click(object sender, EventArgs e)
    {
        //Some datastreams stats
        RegularExpressionValidator1.Validate();
        if (RegularExpressionValidator1.IsValid == false) return;
        darth.Visible = false;

        DetailsView2.DataBind();
        GridView1.DataBind();
        GridView2.DataBind();

        GridView2.Visible = true;
        DetailsView2.Visible = false;

    }

    protected void mypage_Click(object sender, EventArgs e)
    {
        darth.Visible = false;
        Response.Redirect("~/code/mypage.aspx");
    }
}







