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 DiscussionMessage.
	/// </summary>
	public class DiscussionMessage : System.Web.UI.Page
	{
		protected  System.Web.UI.WebControls.DropDownList DiscussionMessageMethodDropDownList;
		protected System.Web.UI.WebControls.Button SelectGoButton;
		protected   System.Web.UI.WebControls.TextBox SubjectBox;
		protected   System.Web.UI.WebControls.TextBox BodyBox;  
		protected   System.Web.UI.WebControls.Button CreateSubmitButton;
		protected   System.Web.UI.WebControls.TextBox DiscussionMessageIDBox;  
		protected   System.Web.UI.WebControls.Button RemoveSubmitButton ; 

		//booleans for create, remove search
		public bool createDiscussionMessage = false;
		public bool removeDiscussionMessage = false;
		public bool searchDiscussionMessages = false;

		//global discussion variable for us to get details and create discussion messages
		public IDiscussion discussion = null;

		//subject and body for create
		public String subject = null;
		public String body = null;

		//discussion message id required for remove discussion message
		public String discussionMessageIDStr = null;
		public int discussionMessageID = -1;

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

		//key for discussion message
		public static readonly String SESSION_DISCUSSION_MESSAGE_KEY = "discussionMessageID";
		
		//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;
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			//we should be getting the discussion id from the query string from Discussion.aspx
			//put it in session if it exists
			String discussionIDStr = Request.QueryString.Get(SESSION_DISCUSSION_KEY);
			if (null != discussionIDStr)
			{
				//get the discussion and put it into the session
				int discussionID = Convert.ToInt32(discussionIDStr);
				IDiscussionManager discussionManager = GetDiscussionManager(Request, Response);
				discussion = discussionManager.GetDiscussion(discussionID);			
				Session.Add(SESSION_DISCUSSION_KEY, discussion);
			}
			else
			{
				//assume that it has already been put in. No error checking for session timeouts. 
				discussion = (IDiscussion) Session[SESSION_DISCUSSION_KEY];
			}

			//set hosted display mode
			IPortletContext portletContext = PortletContextFactory.CreatePortletContext(Request, Response);
			IPortletResponse portletResponse = portletContext.GetResponse();
			portletResponse.SetHostedDisplayMode(HostedDisplayMode.Hosted);
					GetPostBackEventReference(this);
		}

		
		private void CreateSubmitButton_Click(object sender, System.EventArgs es) 
		{
			subject = SubjectBox.Text;
			body = BodyBox.Text;
			createDiscussionMessage = true;
			DiscussionMessageMethodDropDownList.SelectedValue = "createMessage";
		}

		private void RemoveSubmitButton_Click(object sender, System.EventArgs es) 
		{
			discussionMessageIDStr = DiscussionMessageIDBox.Text;
			discussionMessageID = Int32.Parse(discussionMessageIDStr);
			removeDiscussionMessage = true;
			DiscussionMessageMethodDropDownList.SelectedValue = "removeMessage";

		}

		private void SelectGoButton_Click(object sender, System.EventArgs es) 
		{
			//retrieve values from submit
			//determine which value, if any, was chosen in the select
			String select = DiscussionMessageMethodDropDownList.SelectedValue;
			if (null != select)
			{
				if (select.Equals("createMessage"))
				{
					createDiscussionMessage = true;
				}
				else if (select.Equals("removeMessage"))  
				{
					removeDiscussionMessage = true;
				}
				else if (select.Equals("searchMessages"))
				{
					searchDiscussionMessages = true;
				}
			}
		}

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