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

using System;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Collections;

using CommunityServer.Components;


namespace CommunityServer.Controls {

    public class UserAttribute : UserInfo {
        CSContext csContext = CSContext.Current;

        protected override void Render(HtmlTextWriter writer) {
            Literal l = new Literal();
			HtmlAnchor a = null;

            if (this.User.IsAnonymous || IsCloakedUser)
                return;

            // Note, we could have used reflection, but that 
            // would have been slower
            //
            switch (this.Attribute) {

                case UserAttributes.Joined:
                    l.Text = String.Format(this.FormatString, ResourceManager.GetString("PostFlatView_Joined") + User.GetTimezone(this.User.DateCreated).ToString(csContext.User.Profile.DateFormat));
                    break;

                case UserAttributes.Location:
                    if (this.User.Profile.Location == string.Empty)
                        return;

                    l.Text = String.Format(this.FormatString, ResourceManager.GetString("PostFlatView_Location") + this.User.Profile.Location);
                    break;

                case UserAttributes.Posts:
                    UserActivityDisplayMode displayMode = csContext.SiteSettings.PostingActivityDisplay;
                    bool displayRankAsPicture = csContext.SiteSettings.DisplayUserRankAsPicture;
                    ArrayList ranks = Ranks.GetRanks();

					// Wire up an the anchor
					a = new HtmlAnchor();
					a.Target = this.Target;
					a.HRef = Globals.GetSiteUrls().SearchByUser(User.UserID);

                    if (displayMode == UserActivityDisplayMode.PostCount || 
                        ranks == null || 
                        (ranks != null && ranks.Count == 0)) { 
                        
                        l.Text = String.Format( this.FormatString, ResourceManager.GetString("PostFlatView_Posts") + this.User.TotalPosts.ToString(ResourceManager.GetString("NumberFormat")) );
                    } 
                    else { 
                        l.Text = String.Format( this.FormatString, Formatter.FormatUserRank( Math.Max(User.DummyTotalPosts, User.TotalPosts), displayRankAsPicture, ranks ) );
                    }
                    break;
				case UserAttributes.Points:
					if (CanSeeUserPoints())
						l.Text = String.Format( this.FormatString, ResourceManager.GetString("PostFlatView_UserPoints") + this.User.Points.ToString(ResourceManager.GetString("NumberFormat")));
					break;
            }

			if (a !=  null) 
			{
				a.Controls.Add(l);
				a.RenderControl(writer);
			} 
			else 
			{
				l.RenderControl(writer);
			}

           
        }

	

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

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

		public virtual string Target 
		{
			get 
			{
				String state = ViewState["Target"] as String;
				if ( state != null ) 
					return state;
				else
					return string.Empty;				
			}
			set 
			{
				ViewState["Target"] = value;
			}
		}

		private bool CanSeeUserPoints()
		{
			if (!csContext.SiteSettings.EnablePointSystem)
				return false;

			switch (csContext.SiteSettings.PointsUserDisplayLevel)
			{
				case DisplayLevel.None:
					return false;
				case DisplayLevel.Everyone:
					return true;
				case DisplayLevel.AdministratorsAndModerators:
					return (csContext.User.IsAdministrator || csContext.User.IsModerator);
				default:
					return false;
			}
		}
    }

}
