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

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls 
{

    public class ForumImageButton : WebControl 
    {
		CSContext csContext = CSContext.Current;
		User user;


        public ForumImageButton() 
        {
			user = csContext.User;
        }

        public ForumImageButton(CSButtonType buttonType)
        {
            this.ButtonType = buttonType;
        }

        protected override void Render(HtmlTextWriter writer) 
        {

            Section section = null;

            // Are we looking up the section by section id or post id
            //

            if (csContext.SectionID > 0)
                section = Forums.GetForum(csContext.SectionID);
            else if (csContext.PostID > 0)
                section = Forums.GetForumByPostID(csContext.PostID);
            else
                return;

            if(section == null)
                return;

            ForumPermission forumPermission = section.ResolvePermission( csContext.User ) as ForumPermission;

			
            string target = string.Empty;
            string alt = string.Empty;
            string text = string.Empty;

            // What type of button are we displaying?
            //
            string skinPath = Globals.GetSkinPath();
            switch (ButtonType) 
            {

                    // New Thread
                case CSButtonType.NewPost:

                    if (!forumPermission.Post)
                        return;

					// private forum?
					if (section.SectionID == 0)
						return;

					NavigateUrl = ForumUrls.Instance().PostCreate(section.SectionID);
                    text = ResourceManager.GetString("Button_NewPost");
                    break;

                case CSButtonType.Print:

					this.NavigateUrl = ForumUrls.Instance().PrintPost(csContext.PostID);
                    text = ResourceManager.GetString("Button_Print");

                    break;

                case CSButtonType.PreviousTopic:

                    text = ResourceManager.GetString("Button_PreviousTopic");

                    // Ensure we will have a hand cursor on all browsers
                    this.NavigateUrl = ForumUrls.Instance().ThreadNavigation(this.Post.PostID, ThreadNavigationType.Previous);
                    break;

                case CSButtonType.NextTopic:

                    text = ResourceManager.GetString("Button_NextTopic");

                    // Ensure we will have a hand cursor on all browsers
                    this.NavigateUrl = ForumUrls.Instance().ThreadNavigation(this.Post.PostID, ThreadNavigationType.Next);
                    break;

                case CSButtonType.Search:

                    text = ResourceManager.GetString("Button_Search");

                    // Ensure we will have a hand cursor on all browsers
                    this.NavigateUrl = "#";
                    break;

                case CSButtonType.ForumSettings:

                    text = ResourceManager.GetString("Button_ForumSettings");

                    // Ensure we will have a hand cursor on all browsers
                    this.NavigateUrl = "#Options";

                    writer.AddAttribute( HtmlTextWriterAttribute.Name, "Options" );
                    writer.RenderBeginTag( HtmlTextWriterTag.A );
                    writer.RenderEndTag();
                    break;

                    // Reply
                case CSButtonType.Reply:

                    // If this is a locked post we display the special locked icon
                    //
                    if (this.Post.IsLocked) {
						writer.AddAttribute(HtmlTextWriterAttribute.Href, "#");
						writer.AddAttribute(HtmlTextWriterAttribute.Onclick, String.Format("window.alert('{0}'); return false;", ResourceManager.GetString("Button_Locked_Alt").Replace("'", "\\'")));
						writer.AddAttribute("class", this.CssClass);
						writer.AddAttribute("style", "background-image: url(" + skinPath + "/images/forum/locked.gif);");
						writer.RenderBeginTag(HtmlTextWriterTag.A);
						writer.Write(ResourceManager.GetString("Button_Locked"));
						writer.RenderEndTag();

						return;
                    }

					if (!forumPermission.Reply)
						return;

				    NavigateUrl = ForumUrls.Instance().PostReply(this.Post.PostID);
                    text = ResourceManager.GetString("Button_Reply");
                    break;
                    
                    // Quote
                case CSButtonType.Quote:

                    if (this.Post.IsLocked ||
                        Post.PostType == PostContentType.Xml ||
						!forumPermission.Reply)
						return;

					NavigateUrl =ForumUrls.Instance().PostReply(this.Post.PostID, true);

                    text = ResourceManager.GetString("Button_Quote");
                    break;
					
                    // Delete
                case CSButtonType.Delete:

                    if (this.Post.IsLocked ||
                        this.Post.User.UserID != user.UserID ||
						!forumPermission.Delete)
                        return;
                    
					if(Post.PostLevel == 1)
					{
						// If deleting PM, be sure to redirect to main PM screen
						// otherwise user will get Forum Not Found (since SectionID = 0)
						if (Post.SectionID == 0)
							NavigateUrl = ForumUrls.Instance().PostDelete(this.Post.PostID, Globals.UrlEncode(Globals.GetSiteUrls().UserPrivateMessages));
						else
							NavigateUrl = ForumUrls.Instance().PostDelete(this.Post.PostID, Globals.UrlEncode(ForumUrls.Instance().Forum(this.Post.SectionID)));
					}
					else
					{
						NavigateUrl = ForumUrls.Instance().PostDelete(this.Post.PostID, Globals.UrlEncode(Globals.GetSiteUrls().Post(this.Post.ParentID) + "#" + this.Post.PostID.ToString()));
					}

                    text = ResourceManager.GetString("Button_Delete");
                    break;

                    // Edit
                case CSButtonType.Edit:

                    if ((this.Post.IsLocked && this.Post.User.UserID != user.UserID) || 
						this.Post.User.UserID != user.UserID ||
						!forumPermission.Edit ||
						this.Post.PostType == PostContentType.Xml)
                        return;

					NavigateUrl = ForumUrls.Instance().PostEdit(this.Post.PostID, Globals.UrlEncode(HttpContext.Current.Request.Url.PathAndQuery + "#" + this.Post.PostID.ToString()));
                    text = ResourceManager.GetString("Button_Edit");
                    break;

            }
                        
            writer.AddAttribute(HtmlTextWriterAttribute.Href, NavigateUrl);

            switch (ButtonType) 
            {
                case CSButtonType.Search:
                    writer.AddAttribute("onMouseDown", "ToggleSearchMenu()");
                    break;

                case CSButtonType.ForumSettings:
                    writer.AddAttribute("onMouseDown", "ToggleMenuOnOff('ForumSettings')");
                    break;
            }

			writer.AddAttribute("class", this.CssClass);
			writer.AddAttribute("style", "background-image: url(" + skinPath + "/images/forum/" + ButtonType.ToString() + ".gif);");
            writer.RenderBeginTag(HtmlTextWriterTag.A);
			writer.Write(text);
            writer.RenderEndTag();

        }
		
        #region Properties

        [
        System.ComponentModel.DefaultValue( CSButtonType.NewPost ),
        ]
        public virtual CSButtonType ButtonType 
        {
            get 
            {
                Object state = ViewState["ButtonType"];
                if ( state != null ) 
                {
                    return (CSButtonType)state;
                }
                return CSButtonType.NewPost;
            }
            set 
            {
                ViewState["ButtonType"] = value;
            }
        }

        public Post 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" );
                }
            }
        }
        
        Post _post;

        

        [
        System.ComponentModel.DefaultValue( false ),
        ]
        public virtual Boolean OpenInNewWindow 
        {
            get 
            {
                Object state = ViewState["OpenInNewWindow"];
                if ( state != null ) 
                {
                    return (Boolean)state;
                }
                return false;
            }
            set 
            {
                ViewState["OpenInNewWindow"] = value;
            }
        }


        [
        System.ComponentModel.DefaultValue( "" ),
        ]
        public virtual String NavigateUrl 
        {
            get 
            {
                Object state = ViewState["NavigateUrl"];
                if ( state != null ) 
                {
                    return (String)state;
                }
                return "";
            }
            set 
            {
                ViewState["NavigateUrl"] = value;
            }
        }

		[
		System.ComponentModel.DefaultValue("CommonImageTextButton")
		]
		public override String CssClass
		{
			get
			{
				Object state = ViewState["CssClass"];
				if ( state != null ) 
				{
					return (String)state;
				}
				return "CommonImageTextButton";
			}
			set
			{
				ViewState["CssClass"] = value;
			}
		}

        #endregion

    }

    public enum CSButtonType 
    {
        PostMessage,
        PreviewMessage,
        SaveMessage,
        NewPost,
        Reply,
        Quote,
        Delete,
        Edit,
        Print,
        Search,
        PreviousTopic,
        NextTopic,
        ForumSettings,
		AllowReply
    }
}
