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

// TODO: Remove code that display help...

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls 
{
    // *********************************************************************
    //  PostModerateView
    //
    /// <summary>
    /// This server control is used to display top level threads. Note, a thread
    /// is a post with more information. The Thread class inherits from the Post
    /// class.
    /// </summary>
    /// 
    // ********************************************************************/
    public class PostModerateView : TemplatedWebControl 
    {
         #region Child Controls

        Repeater postRepeater;

        CSContext csContext = CSContext.Current;

        #endregion

		/// <summary>
		/// If not a page back, calls DataBind()
		/// </summary>
		/// <param name="e"></param>
		protected override void OnLoad(EventArgs e) 
		{
            // Data bind even on postback
            //
            if (!Page.IsPostBack)
			    this.DataBind();

			base.OnLoad(e);
		}
        
        #region Skin

		/// <summary>
		/// Sets a default Skin name. All Applications should reuse this control and
		/// hopefully this skin as well.
		/// </summary>
		protected override String ExternalSkinFileName
		{
			get
			{
				return "/Moderation/View-ModerateForum.ascx";
			}
		}

		protected override void AttachChildControls() 
		{
			// Get the cached forum information
			//
			Forum forum = (Forum) Forums.GetForum(csContext.SectionID);            
            if (forum == null)
                return;

			// Set the title
			//
            ((Literal) FindControl("ForumDescription")).Text = string.Format(ResourceManager.GetString("Moderate_PostsToModerateDescription"), ForumGroups.GetForumGroup(forum.GroupID).Name + "->" + forum.Name);
            ((Literal) FindControl("ForumDetailDescription")).Text = "Forum Description: " + forum.Description;

			// Find the post repeater
			//
			postRepeater = (Repeater) FindControl("PostRepeater");
			postRepeater.ItemDataBound += new RepeaterItemEventHandler(postRepeater_ItemDataBound);
		}

        #endregion
        
        #region DataBind

        public override void DataBind() 
        {
			base.DataBind();

			// Get a populated thread set
			//
			PostSet p = Moderate.GetPosts(csContext.SectionID, 0, 25, 0, 0);

			if (p.Posts.Count == 0) 
			{
				csContext.Context.Response.Redirect(Globals.GetSiteUrls().Moderate, true);
			} 
			else 
			{
				postRepeater.DataSource = p.Posts;
				postRepeater.DataBind();
			}
        }
        
        #endregion

        #region Event Handlers

        public void Pager_IndexChanged (Object sender, EventArgs e) 
        {
            DataBind();
        }

		private void postRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e) 
		{
			ForumPost post = e.Item.DataItem as ForumPost;

			switch ( e.Item.ItemType ) 
			{
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
					HyperLink threadLink = new HyperLink();
					threadLink = (HyperLink) e.Item.FindControl("ThreadLink");

					if(post.PostID != post.ParentID)
						threadLink.NavigateUrl = Globals.GetSiteUrls().Post(post.ParentID);
					else
						threadLink.NavigateUrl = "";

				break;
			}
		}

        #endregion
    }
}