using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Blogs.Components;
using CommunityServer.RollerBlogs.Components;
using CommunityServer.Reader.Components;

namespace CommunityServerWeb.ControlPanel.BlogAdmin
{
	/// <summary>
	/// Summary description for FeedForm.
	/// </summary>
	public class FeedForm : System.Web.UI.Page
	{
		CSContext context = CSContext.Current;

		#region Controls

		protected TextBox FeedUrlTxt;
		protected TextBox FeedTitleTxt;
		protected TextBox FeedIntervalTxt;
		protected TextBox FeedSiteUrlTxt;
		protected CheckBox checkShowFullPost;
		protected CheckBox checkBlogRollAgg;
		protected CheckBox checkBlogAgg;
		protected TextBox FeedExerptSize;
		protected ResourceLinkButton AddFeedButton;

		protected CommunityServer.ControlPanel.Controls.HelpIcon Helpicon1;
		protected CommunityServer.ControlPanel.Controls.FormLabel Formlabel1;
		protected CommunityServer.ControlPanel.Controls.HelpIcon Helpicon2;
		protected CommunityServer.ControlPanel.Controls.FormLabel Formlabel2;
		protected CommunityServer.ControlPanel.Controls.HelpIcon Helpicon3;
		protected CommunityServer.ControlPanel.Controls.FormLabel Formlabel3;
		protected CommunityServer.ControlPanel.Controls.HelpIcon Helpicon4;
		protected CommunityServer.ControlPanel.Controls.FormLabel Formlabel4;
		protected CommunityServer.ControlPanel.Controls.HelpIcon Helpicon5;
		protected CommunityServer.ControlPanel.Controls.FormLabel Formlabel5;

		#endregion

		#region Properties

		private int SectionID
		{
			get
			{
				Object state = ViewState["SectionID"];
				if(state != null)
					return (int)state;
				return -1;
			}
			set { ViewState["SectionID"] = value; }
		}

		private int UrlID
		{
			get
			{
				Object state = ViewState["UrlID"];
				if (state != null)
					return (int) state;
				return -1;
			}
			set
			{
				ViewState["UrlID"] = value; 
			}
		}

		#endregion

		private void Page_Load(object sender, System.EventArgs e)
		{
			SectionID = context.GetIntFromQueryString("SectionID", -1);
			UrlID = context.GetIntFromQueryString("UrlID", -1);

			if (!Page.IsPostBack)
			{
				if (UrlID > 0)
					Bind();
			}
		}

		private void Bind()
		{
			ArrayList blogFeeds = RollerBlogFeeds.GetEnabledFeedsBySectionID(SectionID);
			AddFeedButton.ResourceName = "CP_Save";
			foreach (RollerBlogFeed rbf in blogFeeds)
			{
				if (rbf.UrlId == UrlID)
				{
					FeedTitleTxt.Text = rbf.Title;
					FeedUrlTxt.Text = rbf.Url;
					FeedUrlTxt.ReadOnly = true;
					FeedSiteUrlTxt.Text = rbf.SiteUrl;
					FeedExerptSize.Text = rbf.ExerptSize.ToString();
					FeedIntervalTxt.Text = rbf.IntervalMinutes.ToString();
					checkShowFullPost.Checked = rbf.PostFullText;
					checkBlogAgg.Checked = rbf.IsBlogAggregated;
					checkBlogRollAgg.Checked = rbf.IsBlogRollAggregated;
					break;
				}
			}
		}

		private void AddFeedButton_Click(object sender, EventArgs e)
		{
			if (Page.IsValid)
			{
				RollerBlogFeed rbf = new RollerBlogFeed();

				rbf.Title = FeedTitleTxt.Text;
				rbf.Url = FeedUrlTxt.Text;
				rbf.SiteUrl = FeedSiteUrlTxt.Text;
				rbf.SectionId = SectionID;
				rbf.IntervalMinutes = Convert.ToInt16(FeedIntervalTxt.Text);
				rbf.PostFullText = checkShowFullPost.Checked;
				rbf.ExerptSize = Convert.ToInt16(FeedExerptSize.Text);
				rbf.IsBlogAggregated = this.checkBlogAgg.Checked;
				rbf.IsBlogRollAggregated = this.checkBlogRollAgg.Checked;
				rbf.Enabled = true;
				rbf.State = FeedState.UpdateSuccessful;

				RollerBlogFeeds.AddFeed(rbf);
                    
				Modal.ClosePage(Page,"true") ;
			}
                    
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
			this.AddFeedButton.Click += new EventHandler(AddFeedButton_Click);
		}
		#endregion
	}
}
