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

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI;
using CommunityServer.Components;


namespace CommunityServer.Controls {

    public class UserImageButtons : PlaceHolder {

        #region Member variables
        CSContext csContext = CSContext.Current;
        
        #endregion

        #region Render Logic

        protected override void Render(HtmlTextWriter writer) {

            switch ( this.Mode )
			{

                case (UserImageButtonMode.Email):
					// Email(writer, false);
                    break;

                case (UserImageButtonMode.HomePage):
                    HomePage(writer, false);
                    break;

                case (UserImageButtonMode.PrivateMessages):
                    PrivateMessage(writer, false);
                    break;

                case (UserImageButtonMode.WebLog):
                    WebLog(writer, false);
                    break;

                case (UserImageButtonMode.Search):
                    Search(writer, false);
                    break;

                default: // All
					// Email(writer, true);
					PrivateMessage(writer, true);
					HomePage(writer, true);
					WebLog(writer, true);
					Search(writer, true);

                    break;
            }
        }

        // User's email
        //
        void Email(HtmlTextWriter writer, bool whitespace) {
			if  ( !User.IsAnonymous && csContext.SiteSettings.EnableEmail) 
			{

				HyperLink l = new HyperLink();

				if (whitespace)
					writer.Write(" ");

				l.NavigateUrl = Globals.GetSiteUrls().EmailToUser(User.UserID);
				if (this.EnableText)
				{
					l.Attributes.Add("style", "background-image: url(" + Globals.GetSkinPath() + "/images/forum/email.gif)");
					l.CssClass = this.CssClass;
					l.Attributes.Add("title", String.Format(CommunityServer.Components.ResourceManager.GetString("User_AltEmail"), User.DisplayName));
					l.Text = ResourceManager.GetString("Button_Email");
				}
				else
				{
					l.ImageUrl = Globals.GetSkinPath() + "/images/post_button_email.gif";
					l.ToolTip = string.Format(ResourceManager.GetString("UserImageButtons_Email"), this.User.DisplayName);
				}
				l.RenderControl(writer);
			}
        }

        // User's Private Messages
        //
        void PrivateMessage(HtmlTextWriter writer, bool whitespace) {
			if  ((!User.IsAnonymous) && (csContext.User.UserID != User.UserID))
			{
				HyperLink l = new HyperLink();
                
				if (whitespace)
					writer.Write(" ");

				l.NavigateUrl = Globals.GetSiteUrls().PrivateMessage(User.UserID);
				if (this.EnableText)
				{
					l.Attributes.Add("style", "background-image: url(" + Globals.GetSkinPath() + "/images/forum/privatemessage.gif)");
					l.CssClass = this.CssClass;
					l.Attributes.Add("title", String.Format(ResourceManager.GetString("User_AltPrivateMessage"), User.DisplayName));
					l.Text = ResourceManager.GetString("Button_PrivateMessage");
				}
				else
				{
					l.ImageUrl = Globals.GetSkinPath() + "/images/post_button_pm.gif";
					l.ToolTip = string.Format(ResourceManager.GetString("UserImageButtons_PrivateMessage"), User.DisplayName);
				}
				l.RenderControl(writer);
			}
        }

        // User's home page
        //
        void HomePage(HtmlTextWriter writer, bool whitespace) {
            if ((User.Profile.WebAddress != String.Empty) && (!User.IsAnonymous)) {
                HyperLink l = new HyperLink();
                
                if (whitespace)
                    writer.Write(" ");
                
				l.NavigateUrl = User.Profile.WebAddress;
				l.Target = "_new";

				if (this.EnableText)
				{
					l.Attributes.Add("style", "background-image: url(" + Globals.GetSkinPath() + "/images/forum/homepage.gif)");
					l.CssClass = this.CssClass;
					l.Attributes.Add("title", String.Format(CommunityServer.Components.ResourceManager.GetString("User_AltHomePage"), User.DisplayName));
					l.Text = ResourceManager.GetString("Button_Homepage");
				}
				else
				{
					l.ImageUrl = Globals.GetSkinPath() + "/images/post_button_www.gif";
					l.ToolTip = string.Format(ResourceManager.GetString("UserImageButtons_HomePage"), User.DisplayName);
				}

                l.RenderControl(writer);
            }
        }

        // User's web log
        //
        void WebLog (HtmlTextWriter writer, bool whitespace) {
            if ((User.Profile.WebLog != String.Empty) && (!User.IsAnonymous)) {
                HyperLink l = new HyperLink();
                
                if (whitespace)
                    writer.Write(" ");
                
				l.NavigateUrl = User.Profile.WebLog;
				l.Target = "_new";

				if (this.EnableText)
				{
					l.Attributes.Add("style", "background-image: url(" + Globals.GetSkinPath() + "/images/forum/weblog.gif)");
					l.CssClass = this.CssClass;
					l.Attributes.Add("title", String.Format(CommunityServer.Components.ResourceManager.GetString("User_AltWeblog"), User.DisplayName));
					l.Text = ResourceManager.GetString("Button_Weblog");
				}
				else
				{
					l.ImageUrl = Globals.GetSkinPath() + "/images/post_button_weblog.gif";
					l.ToolTip = string.Format(ResourceManager.GetString("UserImageButtons_WebLog"), User.DisplayName);
				}
                l.RenderControl(writer);
            }
        }

        // Search
        //
        void Search (HtmlTextWriter writer, bool whitespace) {
            if ((User.TotalPosts > 0) && (!User.IsAnonymous)) {
                HyperLink l = new HyperLink();
                
                if (whitespace)
                    writer.Write(" ");
                
                l.ImageUrl = Globals.GetSkinPath() + "/images/post_button_search.gif";
                l.NavigateUrl = Globals.GetSiteUrls().SearchByUser( User.UserID);
				l.ToolTip = string.Format(ResourceManager.GetString("UserImageButtons_SearchFor"), User.DisplayName);
                l.RenderControl(writer);
            }

        }


        #endregion

        #region Public properties
		public User User {
			get {
				if ( _user == null ) {
					Object state = ViewState[ "UserID" ];
					if ( state != null ) {
						Int32 userID = (Int32)state;
						_user = Users.GetUser( userID, false );
					}
				}
				return _user;
			}
			set {
				_user = value;
				if ( _user != null ) {
					ViewState[ "UserID" ] = _user.UserID;
				} else {
					ViewState.Remove( "UserID" );
				}
			}
		}
		User _user;

		[DefaultValue( UserImageButtonMode.All )]
		public virtual UserImageButtonMode Mode {
			get {
				Object state = ViewState["Mode"];
				if ( state != null ) {
					return (UserImageButtonMode)state;
				}
				return UserImageButtonMode.All;
			}
			set {
				ViewState["Mode"] = value;
			}
		}

		[DefaultValue( false )]
		public bool EnableText
		{
			get 
			{
				object state = ViewState["EnableText"];
				if ( state != null ) 
					return (bool)state;
				return false;
			}
			set 
			{
				ViewState["EnableText"] = value;
			}
		}
		

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

		#endregion

    }

}
