//------------------------------------------------------------------------------
// <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;

using System.IO;
using System.Collections;

namespace CommunityServer.Controls {

    public class PostIcons: PlaceHolder {

        #region Member variables
        CSContext csContext = CSContext.Current;
        bool isCloakedUser = false;
        #endregion

        #region Render functions
        protected override void Render(HtmlTextWriter writer) 
        {
            string img = "<img title=\"{0}\" alt=\"{0}\" src=\"" +  Globals.GetSkinPath() + "/images/rankicons/{1}.gif" + "\" />";

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

			bool hasWritten = false;

            switch (this.User.PostRank[0]) {
                case 1:
                    writer.Write( string.Format(img, CommunityServer.Components.ResourceManager.GetString("User_PostIcons_Top10"), "rankTop10"));
					hasWritten = true;
                    break;

                case 2:
                    writer.Write( string.Format(img, CommunityServer.Components.ResourceManager.GetString("User_PostIcons_Top25"), "rankTop25"));
					hasWritten = true;
                    break;

                case 4:
                    writer.Write( string.Format(img, CommunityServer.Components.ResourceManager.GetString("User_PostIcons_Top50"), "rankTop50"));
					hasWritten = true;
                    break;

                case 8:
                    writer.Write( string.Format(img, CommunityServer.Components.ResourceManager.GetString("User_PostIcons_Top75"), "rankTop75"));
					hasWritten = true;
                    break;

                case 16:
                    writer.Write( string.Format(img, CommunityServer.Components.ResourceManager.GetString("User_PostIcons_Top100"), "rankTop100"));
					hasWritten = true;
                    break;

                case 32:
                    writer.Write( string.Format(img, CommunityServer.Components.ResourceManager.GetString("User_PostIcons_Top150"), "rankTop150"));
					hasWritten = true;
                    break;

                case 64:
                    writer.Write( string.Format(img, CommunityServer.Components.ResourceManager.GetString("User_PostIcons_Top200"), "rankTop200"));
					hasWritten = true;
                    break;

                case 128:
                    writer.Write( string.Format(img, CommunityServer.Components.ResourceManager.GetString("User_PostIcons_Top500"), "rankTop500"));
					hasWritten = true;
                    break;

                default:
                    writer.Write( string.Format(img, CommunityServer.Components.ResourceManager.GetString("User_PostIcons_NotRanked"), "rank0"));
					hasWritten = true;
                    break;
            }

            // Do we have a gender for the this.User?
            //
			if ((csContext.SiteSettings.AllowGender) && (this.User.Profile.Gender != Gender.NotSet))
			{
				if (hasWritten)
					writer.Write("<br />");

				if (this.User.Profile.Gender == Gender.Male)
					writer.Write( string.Format(img, CommunityServer.Components.ResourceManager.GetString("Male"), "GenderMale"));
				else
					writer.Write( string.Format(img, CommunityServer.Components.ResourceManager.GetString("Female"), "GenderFemale"));
			}


        }
        #endregion

        #region Private methods
        private bool FileExists (string filename) {
            string cacheKey = "fileExistsLookupTable";
            Hashtable fileLookup = CSCache.Get(cacheKey) as Hashtable;

            if (fileLookup == null) {
                fileLookup= new Hashtable();
                CSCache.Max(cacheKey,fileLookup);
            }


            if(!fileLookup.ContainsKey(filename))
            {
                fileLookup[filename] = File.Exists(filename);
            }


            return (bool) fileLookup[filename];

        }
        #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;

        /// <summary>
        /// Added to specify if this user should be displayed as anonymous
        /// </summary>
        public bool IsCloakedUser { 
            get { return isCloakedUser; }
            set { isCloakedUser = value; }
        }
        #endregion

    }

}
