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

using System;
using System.Web.UI;

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

namespace CommunityServer.Discussions.Controls 
{

    // *********************************************************************
    //  PostView
    //
    /// <summary>
    /// This server control is used to display a Post
    /// </summary>
    /// 
    // ********************************************************************/
    public class PostView : Control, INamingContainer 
    {
        ForumPost post;
		CSContext csContext = CSContext.Current;

        // *********************************************************************
        //  CreateChildControls
        //
        /// <summary>
        /// Initializes the user control loaded in CreateChildControls. Initialization
        /// consists of finding well known control names and wiring up any necessary events.
        /// </summary>
        /// 
        // ********************************************************************/ 
        protected override void CreateChildControls() 
        {
            TextPost textPost;

            // What kind of post are we working with (XML check is used to support pre-2.0 polls)
			if (this.Post.PostType == PostContentType.Xml)
			{
				PollPost pollPost = new PollPost(this.Post);

				if (csContext.User.IsAnonymous)
					pollPost.Enabled = false;
				else 
				{
					if (Permissions.ValidatePermissions(post.Section,Permission.Vote,csContext.User))
						pollPost.Enabled = true;
				}

				Controls.Add(pollPost);
			}
			else
			{
                textPost = new TextPost();
                textPost.Post = Post;
                Controls.Add(textPost);
            }

        }

        public ForumPost Post 
        {
            get 
            {
                if ( post == null ) 
                {
                    Object state = ViewState["Post"];
                    if ( state != null ) 
                    {
                        Int32 postID = (Int32)state;
                        post = Posts.GetPost( postID, csContext.User.UserID, false );
                    }
                }
                return post;
            }
            set 
            {
                post = value;
                if ( post != null ) 
                {
                    ViewState[ "Post" ] = post.PostID;
                } 
                else 
                {
                    ViewState.Remove( "Post" );
                }
            }
        }


    }
}