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 string lmessage,user;
		protected long wsid;
		protected CasGroups cGroups = new CasGroups();
        protected Group ThisGroup = null;
		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;
        protected edu.jhu.Cas.Util.Table[] GTables;
		private void Page_Load(object sender, System.EventArgs e){}
		
		private void LoadInvites() {
			inviteDataSet = cGroups.FindPendingInvitationsDS(wsid);
			inviteGrid.DataSource = inviteDataSet.Tables[0];
			inviteGrid.DataBind();
		}
		private void LoadMembers() {
            if (ThisGroup.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, ThisGroup.GID);
            membersGrid.DataSource = membersDataSet.Tables[0];
            membersGrid.DataBind();
                //membersGrid.Columns[0].HeaderText = "TEST";
		}
		public void InviteCommand(Object sender, DataGridCommandEventArgs e) {
			try {
			 	long gid;
				switch(e.CommandName) {
					case "Accept":
						gid = long.Parse(e.Item.Cells[5].Text);
						cGroups.AcceptInvite(wsid,gid);
						lmessage = "Invitation to "+e.Item.Cells[2].Text+" accepted.";
						break;
					case "Decline":
						gid = long.Parse(e.Item.Cells[5].Text);
						cGroups.DeclineInvite(wsid,gid);
						lmessage = "Invitation to "+e.Item.Cells[2].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?gid="+ThisGroup.GID);
		}
		
		public void Remove(Object sender, EventArgs E) {
			lmessage = "You are no longer a member of "+ThisGroup.Name;
			try {
				cGroups.RemoveUser(wsid,wsid,ThisGroup.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?gid="+ThisGroup.GID);
		}
		protected void SetGroupDescription(Object sender, EventArgs E) {
			lmessage = ThisGroup.Name +" description set";
			try {       
				cGroups.SetGroupDescription(wsid,ThisGroup.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"];
				wsid = (long)Session["USER"];
			//admin = GroupsIndex.GetAdminGroups(wsid).Contains(groupName);
			user = (new CasUsers()).FindUser(
				edu.jhu.Cas.Util.ID.SERVICE_ID, " webservicesid = "+wsid)[0].Name;
            if (Request.Params["gid"] != null) {
                long gid = long.Parse(Request.Params["gid"]);
                ThisGroup = cGroups.GetGroupByID(wsid, gid);
                DescriptionBox.Text = ThisGroup.Description;
                LoadMembers();
                GTables = (new TableJobs()).GetTablesByGroup(wsid, gid);

            } 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
	}
}