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 Plumtree.Remote.Portlet;
using Plumtree.Remote.PRC;
using Plumtree.Remote.PRC.Collaboration;
using Plumtree.Remote.PRC.Collaboration.Discussion;
using Plumtree.Remote.PRC.Collaboration.Project;




namespace DiscussionSample
{
	/// <summary>
	/// Summary description for Discussion.
	/// </summary>
	public class Discussion : System.Web.UI.Page
	{
		protected  System.Web.UI.WebControls.DropDownList DiscussionMethodDropDownList;
		protected System.Web.UI.WebControls.Button SelectGoButton;
		protected   System.Web.UI.WebControls.TextBox DiscussionNameBox;
		protected   System.Web.UI.WebControls.TextBox DiscussionDescriptionBox;  
		protected   System.Web.UI.WebControls.Button CreateSubmitButton;
		protected   System.Web.UI.WebControls.TextBox DiscussionIDStrBox;  
		protected   System.Web.UI.WebControls.Button RemoveSubmitButton;  
                                        
		//booleans for create, remove and search
		public bool createDiscussion = false;
		public bool removeDiscussion = false;
		public bool searchDiscussion = false;
		public bool searchDiscussionMessages = false;

		//optional name and description for create
		public String name = null;
		public String description = null;

		//discussion id required for remove discussion
		public String strDiscussionID = null;
		public int discussionID = -1;

		//portlet id
		public int portletID;

		//key for discussion
		public static readonly String SESSION_DISCUSSION_KEY = "discussionID";

		//key for discussion message
		public static readonly String SESSION_DISCUSSION_MESSAGE_KEY = "discussionMessageID";

		//key for project
		public static readonly String SESSION_PROJECT_KEY = "edk_sample_project";

		private void Page_Load(object sender, System.EventArgs e)
		{
			//check if the session has a project id. If not, get the default project id for the edk.
			Plumtree.Remote.PRC.Collaboration.Project.IProject project = (Plumtree.Remote.PRC.Collaboration.Project.IProject) Session[SESSION_PROJECT_KEY];
			//if this is null, store, and get out of the session again
			if (null == project)
			{
      Response.Redirect("../project/ProjectCreatorCS.aspx?source=../discussion/Discussion.aspx");
			}
    IPortletContext portletContext  = PortletContextFactory.CreatePortletContext(Request, Response);
   IPortletRequest portletRequest = portletContext.GetRequest();
    portletID = portletRequest.GetPortletID();

			GetPostBackEventReference(this);

		}
		private void SelectGoButton_Click(object sender, System.EventArgs es) 
		{
			//see if something was chosen in select. If so, assign the appropriate Booleanean
			String selectStr  = DiscussionMethodDropDownList.SelectedValue;

			if (null != selectStr)
			{
				if (selectStr.Equals("create") )
				{
					createDiscussion = true;
				}
				else if (selectStr.Equals("remove")) 
				{
					removeDiscussion = true;
				}
				else if (selectStr.Equals("search")) 
				{
					searchDiscussion = true;
				}
				else if  (selectStr.Equals("searchDiscussionMessages"))
				{
					searchDiscussionMessages = true;
				}
			}
		}

		private void CreateSubmitButton_Click(object sender, System.EventArgs es) 
		{
			name = DiscussionNameBox.Text;
			description = DiscussionDescriptionBox.Text;
			createDiscussion = true;
			DiscussionMethodDropDownList.SelectedValue = "create";
		}

		private void RemoveSubmitButton_Click(object sender, System.EventArgs es) 
		{
			//see if we have a discussion id- if so, convert it to an Integer.
			strDiscussionID = DiscussionIDStrBox.Text;
			if( strDiscussionID != null)
			{
				discussionID = Int32.Parse(strDiscussionID);
			}	
			removeDiscussion = true;
			DiscussionMethodDropDownList.SelectedValue = "remove";
		}
		//gets the discussion manager
		public  IDiscussionManager GetDiscussionManager( HttpRequest req, HttpResponse res)  
		{
			IDiscussionManager discussionManager = null;
			IPortletContext portletContext = PortletContextFactory.CreatePortletContext(req, res);
			IPortletRequest portletRequest = portletContext.GetRequest();
			String loginToken = portletRequest.GetLoginToken();
			if (null == loginToken)
			{
				res.Write("Unable to retrieve the login token. Confirm that the login token has been checked in the Advanced Settings page of the Web Service.");
			}

			//get the remote session
			Plumtree.Remote.PRC.IRemoteSession portalSession = portletContext.GetRemotePortalSession();

			//get a collab factory and a discussion manager
			ICollaborationFactory collabFactory = portalSession.GetCollaborationFactory();
			discussionManager = collabFactory.GetDiscussionManager();

			return discussionManager;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			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.SelectGoButton.Click += new System.EventHandler(this.SelectGoButton_Click);
			this.CreateSubmitButton.Click += new System.EventHandler(this.CreateSubmitButton_Click);
			this.RemoveSubmitButton.Click += new System.EventHandler(this.RemoveSubmitButton_Click);
					this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}
