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

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

namespace CommunityServer.Controls
{
    [PersistChildren(true)]
    [ParseChildren(false)]
    public class AdPart : ContentPart
    {
        private AdsConfiguration adConfig = AdsConfiguration.Instance(true);
        private CSContext context = CSContext.Current;

        protected override void OnLoad(EventArgs e)
        {
            
            if(AdsConfiguration.DisplayAd(context.User))
            {
                base.OnLoad (e);
            }
            else if(adConfig.Enabled && context.User.IsAdministrator)
            {
                this.DefaultContentForEditorOnly = true;
                this.EditDefaultConent = false;
                if(hasChildren)
                {
                    this.Text = "<div style=\"text-align:center\">Ad via Inline control</div>";
                }
                else
                {
                    this.Text = "<div style=\"text-align:center\">Ad PlaceHolder</div>";   
                }
                

                base.OnLoad (e);
            }
            else
            {
                this.Visible = false;
                this.Enabled = false;
                this.Controls.Clear();
            }
        }

        protected override string EditUrl()
        {
            return Contents.EditAdUrl();
        }


        private bool hasChildren = false;
        protected override void AddParsedSubObject(object obj)
        {
            if(adConfig.AllowInlineControl)
            {
                if(!hasChildren && obj != null && obj is Ads)
                    hasChildren = true;

                base.AddParsedSubObject (obj);
            }
        }

    }
}