//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web;
using CommunityServer.Components;

namespace CommunityServer.Reader.Components
{
	/// <summary>
	/// Summary description for TreeXML.
	/// </summary>
	public class FolderXMLHandler : IHttpHandler
	{

		#region Constructor(s)
		
		public FolderXMLHandler()
		{
			
		}

		#endregion

		#region Public Methods

	    public void ProcessRequest(HttpContext context)
	    {
            context.Response.ContentType = "text/xml";

            int userId; 
            int parentFolderId = 0;
	
	
            if (context.Request.QueryString["ParentFolderId"] != null)
                parentFolderId = Convert.ToInt32(context.Request.QueryString["ParentFolderId"]);
		
            userId = CSContext.Current.User.UserID;

            // We'll use a TreeView instance to generate the appropriate XML structure 
            ComponentArt.Web.UI.TreeView TreeView1 = new ComponentArt.Web.UI.TreeView();
	
            TreeView1.DragAndDropEnabled = true;
	  
            // Get all the folder nodes with the appropriate ParentId 
	        ArrayList folders = FeedReaderDataProvider.Instance().GetFolders(userId, parentFolderId);

            foreach (Folder folder in folders)
            {
                ComponentArt.Web.UI.TreeViewNode newNode = new ComponentArt.Web.UI.TreeViewNode();
		
				if (folder.FolderName == null || folder.FolderName.Trim() == "")
				{
					newNode.Text				= "Unknown folder name";
					newNode.ToolTip				= "An error occurred to where no folder name was recorded for this folder. Please try to delete it and recreate it if possible.";
				}
				else
				{
					newNode.Text				= folder.FolderName + (folder.UnreadCount > 0 ? " (" + folder.UnreadCount + ")" : "");	
					newNode.ToolTip				= folder.FolderName + (folder.UnreadCount > 0 ? " (" + folder.UnreadCount + ")" : "");
				}
                
                newNode.ImageUrl			= "images/folder.gif";
                newNode.ID					= "folder-" + parentFolderId + "-" + folder.FolderId.ToString();
                newNode.ContentCallbackUrl	= context.Response.ApplyAppPathModifier("~/reader/FolderXml.ashx?ParentFolderId=" + folder.FolderId.ToString());
                newNode.EditingEnabled		= true;
				newNode.DraggingEnabled		= true;
				newNode.DroppingEnabled		= true;
                
		
                if (folder.UnreadCount > 0)
                {
                    newNode.CssClass = "UnreadTreeNode";
                    newNode.HoverCssClass = "UnreadHoverTreeNode";
                }
		
                TreeView1.Nodes.Add(newNode);
            }

            // Get all the feed nodes for the current folder.
            ArrayList feeds = FeedReaderDataProvider.Instance().GetFeeds(userId, parentFolderId);
	
            foreach (Feed feed in feeds)
            {
                ComponentArt.Web.UI.TreeViewNode newNode = new ComponentArt.Web.UI.TreeViewNode();
		
				if (feed.Title == null || feed.Title.Trim() == "")
				{
					newNode.Text			= "Unknown feed name";
					newNode.ToolTip			= "The title of this feed is unknown. An error probably occurred with this feed, making it impossible to record it's title. Please try re-adding the feed if possible.";
				}
				else
				{
					newNode.Text			= feed.Title + (feed.UnreadCount > 0 ? " (" + feed.UnreadCount + ")" : "");	
					newNode.ToolTip			= feed.Title + (feed.UnreadCount > 0 ? " (" + feed.UnreadCount + ")" : "");
				}
                
                newNode.ID				="feed-" + feed.FeedId.ToString() + "-" + feed.FolderFeedId.ToString() + "-" + (int) feed.FeedState;
                newNode.EditingEnabled	= false;
				newNode.DraggingEnabled		= true;
				newNode.DroppingEnabled		= true;
                
                if (feed.FeedState == FeedState.UpdateSuccessful || feed.FeedState == FeedState.FeedCached)
                {
                    newNode.ImageUrl		= "images/file.gif";
                }
                else
                {
                    newNode.ImageUrl		= "images/file_exception.gif";
                    newNode.ToolTip			= "This feed has had problems updating. Please see the feed properties for details, and be aware that you may be viewing old posts.";
                }
		
                if (feed.UnreadCount > 0)
                {
                    newNode.CssClass = "UnreadTreeNode";
                    newNode.HoverCssClass = "UnreadHoverTreeNode";
                }
			
                TreeView1.Nodes.Add(newNode);
            }

            // Use TreeView's GetXml method to return the XML structure   
            context.Response.Write(TreeView1.GetXml());  
	    }

		#endregion

		#region Public Properties
        
	    public bool IsReusable
	    {
	        get { return false; }
	    }

		#endregion

	}
}
