using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ManageRoles_aspx : System.Web.UI.Page
{
    // Zdarzenia strony zostaj wywoywane automatycznie dla
    // metod o nastpujcych nazwach:
    // Page_Load, Page_AbortTransaction, Page_CommitTransaction,
    // Page_DataBinding, Page_Disposed, Page_Error, Page_Init,
    // Page_Init Complete, Page_Load, Page_LoadComplete, Page_PreInit,
    // Page_PreLoad, Page_PreRender, Page_PreRenderComplete,
    // Page_SaveStateComplete, Page_Unload.

   private string[] rolesArray;
   private string[] usersInRole;
   MembershipUserCollection users;


   protected void Page_Load(object sender, EventArgs e)
   {

      //if (User.IsInRole("Manager") == false)
      //{
      //   Response.Redirect("NoPrivs.aspx");
      //}
      Msg.Text = string.Empty;
      if ( ! IsPostBack )
      {
         rolesArray = Roles.GetAllRoles();
         RolesListBox.DataSource = rolesArray;
         RolesListBox.DataBind();
         users = Membership.GetAllUsers();
         this.UsersListBox.DataSource = users;
         this.UsersListBox.DataBind();
      }
      if ( RolesListBox.SelectedItem != null )
      {
         usersInRole = Roles.GetUsersInRole
            (this.RolesListBox.SelectedItem.Value);
         UsersInRoleGrid.DataSource = usersInRole;
         UsersInRoleGrid.DataBind();
      }
   }

   protected void AddUsers_OnClick(object sender, EventArgs e)
   {
      if (RolesListBox.SelectedItem == null)
      {
         this.Msg.Text = "Prosz wybra rol.";
         return;
      }
      if (UsersListBox.SelectedItem == null)
      {
         Msg.Text = "Prosz wybra jednego lub wicej uytkownikw.";
         return;
      }

      int sizeOfArray = UsersListBox.GetSelectedIndices().Length;
      string[] newUsers = new string[sizeOfArray];

      //for (int i = 0; i < newUsers.Length; i++)
      //{
      //   newUsers[i] =
      //      UsersListBox.Items[UsersListBox.GetSelectedIndices()[i]].Value;
      //}



      // Pobieramy tablic zaznaczonych indeksw z listy (umoliwia wiele wyborw).
      int[] selectedIndices = UsersListBox.GetSelectedIndices();

      for ( int i = 0; i < newUsers.Length; i++)
      {
         // Pobieramy okrelony zaznaczony selectedIndex odpowiadajcy counter[i].
         int selectedIndex = selectedIndices[i];
         // Pobieramy ListItem z listy UserListBox i jego offset.
         ListItem myListItem = UsersListBox.Items[selectedIndex];
         // Pobieramy cig znakowy, ktry jest wartoci waciwoci tego ListItem.
         string newUser = myListItem.Value;
         // Dodajemy ten cig znakowy do zbioru cigw newUsers.
         newUsers[i] = newUser;
      }

      // Dodajemy uytkownikw do zaznaczonej roli.
      Roles.AddUsersToRole(newUsers, RolesListBox.SelectedItem.Value);
      usersInRole = Roles.GetUsersInRole(RolesListBox.SelectedItem.Value);
      UsersInRoleGrid.DataSource = usersInRole;
      UsersInRoleGrid.DataBind();

   }

   protected void CreateRole_OnClick(object sender, EventArgs e)
   {
      this.pnlCreateRole.Visible = true;
   }

   protected void btnAddRole_Click(object sender, EventArgs e)
   {
      if (txtNewRole.Text.Length > 0 )
      {
         string newRole = txtNewRole.Text;
         if ( Roles.RoleExists(newRole) == false )
         {
            Roles.CreateRole(newRole);
            rolesArray = Roles.GetAllRoles();
            this.RolesListBox.DataSource = rolesArray;
            this.RolesListBox.DataBind();
         }
      }
      txtNewRole.Text = string.Empty;
      pnlCreateRole.Visible = false;
   }

   protected void UsersInRoleGrid_RemoveFromRole(
      object sender, GridViewCommandEventArgs e)
   {
      int index = Convert.ToInt32(e.CommandArgument);
      DataBoundLiteralControl theControl = (DataBoundLiteralControl)
         (UsersInRoleGrid.Rows[index].Cells[0].Controls[0]);
      string userName = theControl.Text;
      Roles.RemoveUserFromRole(userName, RolesListBox.SelectedItem.Value);
         usersInRole = Roles.GetUsersInRole(RolesListBox.SelectedItem.Value);
         UsersInRoleGrid.DataSource = usersInRole;
      UsersInRoleGrid.DataBind();
   }
}
