using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using edu.jhu.Cas.Jobs;
using edu.jhu.Cas.Users;
using edu.jhu.Cas.Util;

namespace edu.jhu.Cas.Web {
	/// <summary>
	/// Summary description for GroupsContent.
	/// </summary>
	public class GroupsContent : System.Web.UI.Page {
		protected bool loggedin,admin;
		protected string groupName,lmessage,user,description,adminID;
		protected long wsid,gID;
		protected CasGroups cGroups = new CasGroups();
		protected DataSet inviteDataSet,membersDataSet;
		protected DataGrid inviteGrid,membersGrid;
		protected System.Web.UI.WebControls.Button createButton;
		protected System.Web.UI.WebControls.Button manageButton;
		protected System.Web.UI.WebControls.Button groupDescriptionSetButton;
		protected System.Web.UI.WebControls.Button destroyButton;
		protected System.Web.UI.WebControls.Button Button2;
		protected TextBox DescriptionBox;
		private void Page_Load(object sender, System.EventArgs e){}
		
		private void LoadInvites() {
			inviteDataSet = cGroups.FindGroupsDataSet(wsid," status = 1",false);
			inviteGrid.DataSource = inviteDataSet.Tables[0];
			inviteGrid.DataBind();
		}
		private void LoadMembers() {
            if (gID == edu.jhu.Cas.Util.ID.PUBLIC_GID) {
                membersDataSet = new DataSet();
                membersDataSet.Tables.Add();
                membersDataSet.Tables[0].Columns.Add("All Users");
            }
            else 
                membersDataSet = cGroups.ListGroupMembers(wsid, gID);
            membersGrid.DataSource = membersDataSet.Tables[0];
            membersGrid.DataBind();
		}
		public void InviteCommand(Object sender, DataGridCommandEventArgs e) {
			try {
			 	long gid;
				switch(e.CommandName) {
					case "Accept":
						gid = long.Parse(e.Item.Cells[1].Text);
						cGroups.AcceptInvite(wsid,gid);
						lmessage = "Invitation to "+e.Item.Cells[0].Text+" accepted.";
						break;
					case "Decline":
						gid = long.Parse(e.Item.Cells[1].Text);
						cGroups.DeclineInvite(wsid,gid);
						lmessage = "Invitation to "+e.Item.Cells[0].Text+" declined.";
						break;
				}
			}
			catch(Exception ee) {
				lmessage = ee.Message;
			}
			Response.Redirect("Groups.aspx?lmessage="+lmessage);
		}
		protected void Create(object sender, EventArgs E) {
			Response.Redirect("CreateGroup.aspx");
		}
		protected void Manage(object sender, EventArgs E) {
			Response.Redirect("InviteUser.aspx?name="+groupName);
		}
		
		public void Remove(Object sender, EventArgs E) {
			lmessage = "You are no longer a member of "+groupName;
			try {
				cGroups.RemoveUser(wsid,wsid,gID);
			}
			catch(Exception e){lmessage = e.Message;}
			Response.Redirect("Groups.aspx?lmessage="+HttpUtility.UrlEncode(lmessage));
		}
		protected void Destroy(Object sender, EventArgs e) {   
			Response.Redirect("DestroyGroup.aspx?groupName="+groupName);
		}
		protected void SetGroupDescription(Object sender, EventArgs E) {
			lmessage = groupName+" description set";
			try {       
				cGroups.SetGroupDescription(wsid,gID,DescriptionBox.Text.Trim());
			}
			catch(Exception e){lmessage = e.Message;}
			Response.Redirect("Groups.aspx?lmessage="+lmessage);
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e) {
			WebUtils.CheckLoggedIn(Session,Response,Request,true);
			lmessage = Request.Params["lmessage"];
			loggedin = Session["USER"] != null;  
			if (loggedin) 
				wsid = (long)Session["USER"];
			groupName = Request.Params["groupName"];
			admin = GroupsIndex.GetAdminGroups(wsid).Contains(groupName);
			user = (new CasUsers()).FindUser(
				edu.jhu.Cas.Util.ID.SERVICE_ID, " webservicesid = "+wsid)[0].Name;
            Group pgroup = (new CasGroups()).FindGroups(wsid, " 1 = 2 ")[0];
			if(groupName != null) {
                if (groupName == pgroup.Name)
                    gID = pgroup.GID;
                else
                    gID = (new CasGroups()).FindGroups(wsid, " Name = '" + groupName + "'", false)[0].GID;
                description = cGroups.GetGroupDescription(gID);
                if (admin) {
                    /**
                     * for some godforsaken reason, descriptionbox won't work if something in this
                     * block is put in PageLoad (where supposedly it should go)
                     * but works just fine if you put it in OnInit
                     */
                    DescriptionBox.Text = cGroups.GetGroupDescription(gID);
                }
                else {
                    adminID = cGroups.GetGroupAdmin(gID);
                }
                LoadMembers();
                
			}
			else {
				LoadInvites();
			}
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {    
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}