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

namespace WebProjectSample
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class ProjectSample: System.Web.UI.Page
	{
		//booleans for create, remove and search
		public bool create = false;
		public bool remove = false;
		public bool search = false;

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

		//project id required for remove
		public String strProjectID = null;
		public int projectID = -1;

		//search text required for search
		public String searchText = null;



		//gets the project manager
		public  IProjectManager GetProjectManager( HttpRequest req, HttpResponse res)  
		{
			IProjectManager projectManager = 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 project manager
			ICollaborationFactory collabFactory = portalSession.GetCollaborationFactory();
			projectManager = collabFactory.GetProjectManager();

			return projectManager;
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			Request.Params.Get("startPage");

			String select = Request.Params.Get("projectMethod");
			if (null != select)
			{
				if (select.Equals("create"))
				{
					create = true;
				}
				else if (select.Equals("remove"))  
				{
					remove = true;
				}
				else if (select.Equals("search"))
				{
					search = true;
				}
			}

			//if any of these is true, set hosted display mode
			if (create || remove || search)
			{
				IPortletContext portletContext = PortletContextFactory.CreatePortletContext(Request, Response);
				IPortletResponse portletResponse = portletContext.GetResponse();
				portletResponse.SetHostedDisplayMode(HostedDisplayMode.Hosted);
			}

			//see if we have a project id- if so, convert it to an int.
			strProjectID = Request.Params.Get("projectID");
			if (null != strProjectID)
			{

				projectID = Int32.Parse(strProjectID); 
	
			} 

			name = Request.Params.Get("name");
			description = Request.Params.Get("description");
			searchText = Request.Params.Get("searchText");
		}

		#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.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}
