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

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

namespace CommunityServer.Controls
{

	// *********************************************************************
	//  DisplayUserWelcome
	//
	/// <summary>
	/// This server control is used to display the user's info, as well
	/// as any anonymous information if they are not logged in.
	/// </summary>
	// ***********************************************************************/
	public class DisplayUserWelcome : TemplatedWebControl
	{

		#region member varaibles
		CSContext csContext = CSContext.Current;
		#endregion

		protected override void AttachChildControls()
		{
			int privateMessageCount = 0;
			string url = "";

			// Only process if the user is signed in
			if (!csContext.Context.Request.IsAuthenticated)
				return;

			Control privateMessages = FindControl("PrivateMessages");

            if(privateMessages != null)
            {

                if(Users.EnablePM(csContext.User))
                {

                    //privateMessageCount = UserMessages.UnreadMessagesCount( csContext.User.UserID );

                    if (privateMessageCount > 0) 
                    {
                        url = string.Format(ResourceManager.GetString("PrivateMessage_Unread"), privateMessageCount);
                    } 
                    else 
                    {
                        url = string.Format(ResourceManager.GetString("PrivateMessages_Messages"), privateMessageCount);
                    }

                    // Set URL
			
                    url = "<a href=\"" + Globals.GetSiteUrls().UserPrivateMessages + "\">" + url + "</a>";

                    // Add to control tree
                    privateMessages.Controls.Add(new LiteralControl( " | " + url));
                }
                else
                {
                    privateMessages.Visible = false;
                }
            }

		}

	}
}