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

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

namespace CommunityServer.Controls 
{
    // *********************************************************************
    //  Message
    //
    /// <summary>
    /// Renders the appropriate error message passed in by a query string id value.
    /// </summary>
    // ********************************************************************/ 
    public class Message : TemplatedWebControl
    {
        #region Member vars

        Label title;
        Label body;

        Components.Message message;
        CSExceptionType exceptionType;
        CSContext csContext = CSContext.Current;

        #endregion

        protected override void OnInit(EventArgs e) 
        {
            if (SkinName == null)                
                ExternalSkinFileName = "Skin-Message.ascx";
            else 
                ExternalSkinFileName = SkinName;
			
            exceptionType = (CSExceptionType) csContext.MessageID;

            message = ResourceManager.GetMessage( exceptionType );

            base.OnInit(e);
        }
		
        protected override void OnLoad(EventArgs e) 
        {
            if (!Page.IsPostBack)
                DataBind();

            base.OnLoad(e);
        }

        #region DataBind

        public override void DataBind() 
        {
            base.DataBind();
            
            string name  = csContext.SiteSettings.SiteName;
			SiteUrls urls = Globals.GetSiteUrls();

            // Do some processing on the messages
            message.Body = message.Body.Replace("[HomeUrl]", "<a href=\"" + urls.ForumsHome + "\">" + name + CommunityServer.Components.ResourceManager.GetString("A_Home") +"</a>");
            message.Body = message.Body.Replace("[LoginUrl]", "<a href=\"" + urls.Login + "\">" + name + CommunityServer.Components.ResourceManager.GetString("A_Login") +"</a>");
            message.Body = message.Body.Replace("[ProfileUrl]", "<a href=\"" + urls.UserEditProfile + "\">" + CommunityServer.Components.ResourceManager.GetString("ViewUserProfile_Title") + "</a>");
			message.Body = message.Body.Replace("[AdminEmail]", "<a href=\"" + csContext.SiteSettings.AdminEmailAddress + "\">" + csContext.SiteSettings.AdminEmailAddress + "</a>");

            if (title != null) 
                title.Text = message.Title;

            if (body != null) 
                body.Text = message.Body;
        }
		
        #endregion		
		
        #region Skin
                
        protected override void AttachChildControls() 
        {
            title = (Label) FindControl("MessageTitle");
            body = (Label) FindControl("MessageBody");			
        }

        #endregion
    }
}



