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

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Blogs.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;

namespace CommunityServer.ControlPanel.Blogs
{

	public class SyndicationOptionsPage : BaseBlogPage
	{

        protected CheckBox EnableRss;
        protected CheckBox EnableAtom;
		protected CheckBox EnableRssComments;
        protected CheckBox EnableTagsRss;
        protected CheckBox SyndicateExternalLinks;
		protected TextBox ExternalSyndicationFeed;
		protected Href PrivateRSS;
		protected Href PrivateAtom;
		protected ConfigOKStatusMessage Status;
        protected ResourceLinkButton SaveButton;

	
		private void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
                EnableRss.Checked = this.CurrentWeblog.EnableRss;
                EnableAtom.Checked = this.CurrentWeblog.EnableAtom;
                EnableTagsRss.Checked = this.CurrentWeblog.EnableTagsRss;
                SyndicateExternalLinks.Checked = this.CurrentWeblog.SyndicateExternalLinks;
				ExternalSyndicationFeed.Text = this.CurrentWeblog.ExternalFeedUrl;
		
				PrivateRSS.NavigateUrl = Globals.FullPath(BlogUrls.Instance().RssPrivate(this.CurrentWeblog.ApplicationKey));
				PrivateRSS.Text = PrivateRSS.NavigateUrl;
				PrivateAtom.NavigateUrl = Globals.FullPath(BlogUrls.Instance().AtomPrivate(this.CurrentWeblog.ApplicationKey));
				PrivateAtom.Text = PrivateAtom.NavigateUrl;

				// make sure comments are enabled before allowing RSS comments
				if (CurrentWeblog.EnableCommentsDefault == false)
				{
					EnableRssComments.Checked = false;
					EnableRssComments.Enabled = false;
				}
				else
					EnableRssComments.Checked = this.CurrentWeblog.EnableRssCommentsDefault;
			}
		}

		override protected void OnInit(EventArgs e)
		{
			this.Load += new EventHandler(this.Page_Load);
			base.OnInit(e);
			this.SaveButton.Click += new EventHandler(this.SaveButton_Click);
		}

        private void SaveButton_Click(object sender, EventArgs e)
        {
            if(Page.IsValid)
            {
                
                CurrentWeblog.EnableRss = EnableRss.Checked;
                CurrentWeblog.EnableAtom = EnableAtom.Checked;
                CurrentWeblog.SyndicateExternalLinks = SyndicateExternalLinks.Checked;
                CurrentWeblog.EnableTagsRss = EnableTagsRss.Checked;
				CurrentWeblog.ExternalFeedUrl = ExternalSyndicationFeed.Text.Trim();
				
				// make sure comments are enabled before allowing RSS comments
				if (CurrentWeblog.EnableCommentsDefault == false)
					CurrentWeblog.EnableRssCommentsDefault = false;
				else
					CurrentWeblog.EnableRssCommentsDefault = EnableRssComments.Checked;

            	Weblogs.Update(CurrentWeblog);

				Status.Success = true;
				Status.Visible = true;
            }
        }
	}
}
