using System;
using System.Text;
using System.Web.UI;


using Plumtree.Remote.Portlet;
using IRemoteSession = Plumtree.Remote.PRC.IRemoteSession;
using Plumtree.Remote.PRC.Collaboration.Document;
using Plumtree.Remote.PRC.Collaboration.Project;

public class DocumentExampleCS : System.Web.UI.Page
{
	protected System.Web.UI.WebControls.Label EnterProjectIDLabel;
	protected System.Web.UI.WebControls.Button ProjectSelectButton;
	protected System.Web.UI.WebControls.Label OrLabel;
	protected System.Web.UI.WebControls.LinkButton SampleProjectLink;
	protected System.Web.UI.WebControls.Label ErrorLabel;
	protected System.Web.UI.WebControls.TextBox ProjectIDBox;

	IProject sampleProject = null;
	protected System.Web.UI.WebControls.Literal TreeView;

	IDocumentManager documentManager;
	IRemoteSession remoteSession;

	private void Page_Load(object sender, System.EventArgs e)
	{
		IPortletContext portletContext = PortletContextFactory.CreatePortletContext(Request, Response);

		remoteSession = portletContext.GetRemotePortalSession();
		documentManager = remoteSession.GetCollaborationFactory().GetDocumentManager();

		sampleProject = (IProject)Session["edk_sample_project"];

		if(TreeView.Text == "" && sampleProject != null)
			CreateTree(sampleProject);	
	}

	private void CreateTree(IProject project)
	{
		IDocumentFolder rootFolder = documentManager.GetTopLevelFolder(project);
		TreeView.Text = AppendFolder(rootFolder, 0).Text;
	}

	#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.ProjectSelectButton.Click += new System.EventHandler(this.ProjectSelectButton_Click);
		this.SampleProjectLink.Click += new System.EventHandler(this.SampleProjectLink_Click);
		this.Load += new System.EventHandler(this.Page_Load);

	}
	#endregion

	private void SampleProjectLink_Click(object sender, System.EventArgs e)
	{
		sampleProject = (IProject)Session["edk_sample_project"];

		if(sampleProject == null)
			Response.Redirect("../project/ProjectCreatorCS.aspx?source=../document/DocumentExampleCS.aspx");
		else
			CreateTree(sampleProject);
	}

	private void ProjectSelectButton_Click(object sender, System.EventArgs e)
	{
		try
		{
			int projectID = Int32.Parse(ProjectIDBox.Text);
			
			sampleProject = remoteSession.GetCollaborationFactory().GetProjectManager().GetProject(projectID);
			if(sampleProject == null)
				ErrorLabel.Text = "Could not find project: " + projectID;
		}
		catch (FormatException)
		{
			ErrorLabel.Text = "Cannot parse projectID: [" + ProjectIDBox.Text + "]";
		}

		CreateTree(sampleProject);
	}

	public LiteralControl AppendFolder(IDocumentFolder folder, int depth)
	{
		if(folder == null)
			return null;

		StringBuilder html = new StringBuilder();

        for(int i = 0; i < depth; i++)
            html.Append("&nbsp;&nbsp;&nbsp;");
        html.Append(folder.Name);

        html.Append(" [<a href=\"" + folder.DetailsURL + "\">");
        html.Append("details");
        html.Append("</a>]");
        html.Append("<br/>");

		LiteralControl rv = new LiteralControl(html.ToString());

        IDocument[] childDocuments = GetChildDocuments(folder);

        for(int i = 0; i < childDocuments.Length; i++)
			rv.Text += AppendDocument(childDocuments[i], depth + 1).Text;

        IDocumentFolder[] childFolders = GetChildFolders(folder);

        for(int i = 0; i < childFolders.Length; i++)
            rv.Text += AppendFolder(childFolders[i], depth + 1).Text;

		return rv;
	}

	public LiteralControl AppendDocument(IDocument document, int depth)
	{
		StringBuilder html = new StringBuilder();

		for(int i = 0; i < depth; i++)
		html.Append("&nbsp;&nbsp;&nbsp;");
		html.Append(document.Name);
		html.Append(" [<a href=\"" + document.DetailsURL + "\">");
		html.Append("details");
		html.Append("</a>]");
		html.Append(" [<a href=\"" + document.ContentURL + "\">");
		html.Append("download");
		html.Append("</a>]");
		html.Append("<br/>");

		LiteralControl rv = new LiteralControl(html.ToString());

		return rv;
	}

	public IDocumentFolder[] GetChildFolders(IDocumentFolder folder) 
	{
		IDocumentFolderFilter filter = documentManager.CreateDocumentFolderFilter();
		IDocumentFolder[] children = documentManager.QueryFolders(folder, filter);
		return children;
	}

	public IDocument[] GetChildDocuments(IDocumentFolder folder) 
	{
		IDocumentFilter filter = documentManager.CreateDocumentFilter();
		IDocument[] children = documentManager.QueryDocuments(folder, filter);
		return children;
	}
}

