//------------------------------------------------------------------------------
// <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 CommunityServer.Components;

namespace CommunityServer.Controls {

    public class UserAvatar : UserInfo {

		bool isMiniAvatar = false;

		#region Properties

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

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

		[
		System.ComponentModel.DefaultValue("~/utility/anonymous.gif"),
		]
		public virtual string AnonymousAvatarUrl
		{
			get 
			{
				string state = (string) ViewState["AnonymousAvatarUrl"];
				if ( state != null ) 
				{
					return state;
				}

				return "~/utility/anonymous.gif";
			}
			set 
			{
				ViewState["AnonymousAvatarUrl"] = value;
			}
		}

		

		[
		System.ComponentModel.DefaultValue( 0 ),
		]
		public virtual Int32 Border {
			get {
				Object state = ViewState["Border"];
				if ( state != null ) {
					return (Int32)state;
				}
				return 0;
			}
			set {
				ViewState["Border"] = value;
			}
		}
		/// <summary>
		/// If set this value overrides the default avatar sizing
		/// </summary>
		[
		System.ComponentModel.DefaultValue( 0 ),
		]
		public virtual Int32 Height 
		{
			get 
			{
				Object state = ViewState["Height"];
				if ( state != null ) 
				{
					return (Int32)state;
				}
				return 0;
			}
			set 
			{
				ViewState["Height"] = value;
			}
		}
		/// <summary>
		/// If set this value overrides the default avatar sizing
		/// </summary>
		[
		System.ComponentModel.DefaultValue( 0 ),
		]
		public virtual Int32 Width 
		{
			get 
			{
				Object state = ViewState["Width"];
				if ( state != null ) 
				{
					return (Int32)state;
				}
				return 0;
			}
			set 
			{
				ViewState["Width"] = value;
			}
		}

		#endregion

        protected override void Render(HtmlTextWriter writer) 
		{
            HtmlImage image = new HtmlImage();

            CSContext context = CSContext.Current;
            SiteSettings settings = context.SiteSettings;

			if (!settings.EnableAvatars)
				return;

            if(!OverrideUser && !context.User.EnableUserAvtars)
				return;
            
            if (IsCloakedUser)
                return;

            if (ShowAnonymousAvatar || ((User != null) && (User.HasAvatar) && (User.EnableAvatar) && (User.IsAvatarApproved)) ) {

				if ( this.PadImage ) {
					writer.Write(Formatter.Whitespace(2,0,true,true));
				}

				if (User != null && User.HasAvatar && User.EnableAvatar && User.IsAvatarApproved)
				{
					if (!User.AvatarUrl.StartsWith("users/avatar.aspx"))
					{
						image.Src = this.User.AvatarUrl;

						// Remote avatars need to be restricted in size
						image.Height = settings.AvatarHeight;
						image.Width = settings.AvatarWidth;
					}
					else
					{
						image.Src = Globals.ApplicationPath + "/" + this.User.AvatarUrl;
					}
				}
				else
					image.Src = this.Page.Response.ApplyAppPathModifier(AnonymousAvatarUrl);

				image.Border = this.Border;

				if (IsMiniAvatar) 
				{
					image.Height = 16;
					image.Width = 16;
				}
				else if(IsCustomSize)
				{
					if( this.Height > 0)
						image.Height = this.Height;
					if( this.Width > 0)
						image.Width = this.Width;
				}

                // Render the image
                //
                image.RenderControl(writer);

            }
        }

        private bool _overrideUser = false;
        public bool OverrideUser
        {
            get{ return _overrideUser;}
            set {_overrideUser = value;}
        }
		public bool IsCustomSize
		{
			get{return this.Height > 0 || this.Width > 0;}
		}

		/// <summary>
		/// When true displays a 16x16 version of the Avatar
		/// </summary>
		public bool IsMiniAvatar 
		{ 
			get { return isMiniAvatar; }
			set { isMiniAvatar = value; }
		}


    }

}
