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

using System;
using System.ComponentModel;
using System.Web.UI;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Enables a generic layout to be applied to multiple pages/controls. It expects to find ONE instance
	/// of a control BodyTemplate. Any controls added to the InnerTemplate ITemplate will be rendered in the 
	/// BodyTemplate control
	/// </summary>
	public class LayoutTemplate : TemplatedWebControl
	{
		public LayoutTemplate()
		{
			//
			// TODO: Add constructor logic here
			//
		}

        #region Members
        

        [
        Browsable( false ),
        DefaultValue( null ),
            Description( "TODO SkinTemplate Description" ),
        PersistenceMode( PersistenceMode.InnerProperty ),
        ]
        public ITemplate InnerTemplate 
        {
            get 
            {
                return _innerTemplate;
            }
            set 
            {
                _innerTemplate = value;
                ChildControlsCreated = false;
            }
        }
        private ITemplate _innerTemplate;

        #endregion

        /// <summary>
        /// We attempt to locate the BodyTemplate control which should be included in every LayoutTemplate. 
        /// 
        /// If we find it and the InnerTemplate is not null, we will instantate InnerTemplate with in the
        /// BodyTemplate
        /// </summary>
        protected override void AttachChildControls()
        {
            //First try to find it by name. 
            BodyTemplate body = FindControl("BodyTemplate") as BodyTemplate;
            if(body == null && this.Controls.Count > 0)
            {
                //If not found and we have at least one control, look through this controls children
                //The first control should by ASP.LayoutTemplate.ascx
				body = RecurseChildren(this);
            }

			if(body != null && InnerTemplate != null)
				InnerTemplate.InstantiateIn(body);
        }

        /// <summary>
        /// No Being Span
        /// </summary>
        /// <param name="writer"></param>
        public override void RenderBeginTag(System.Web.UI.HtmlTextWriter writer)
        {
            //we don't need a span tag
        }

        /// <summary>
        /// No End Span
        /// </summary>
        /// <param name="writer"></param>
        public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
        {
            //we don't need a span tag
        }


		protected virtual BodyTemplate RecurseChildren(Control parent)
		{
			foreach(Control cntrl in parent.Controls)
			{
				if(cntrl is BodyTemplate)
					return cntrl as BodyTemplate;
				else
				{
					BodyTemplate body = RecurseChildren(cntrl);
					if(body != null)
						return body;
				}
			}
			return null;
		}

	}
}
