//------------------------------------------------------------------------------
// <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.Discussions.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.ControlPanel.Controls;
using StatusMessage = CommunityServer.ControlPanel.Controls.StatusMessage;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;

namespace CommunityServer.ControlPanel.Forums
{
	/// <summary>
	/// Summary description for Posts.
	/// </summary>
	public class PostOptions : BaseForumPage
	{
		#region Members

		// General
		protected YesNoRadioButtonList EnableForumsRSS;
		protected YesNoRadioButtonList EnableThreadStatus;
		protected TextBox ThreadsPerPage;
		protected TextBox PostsPerPage;
		protected RequiredFieldValidator ThreadsPerPageValidator;
		protected RequiredFieldValidator PostsPerPageValidator;
		protected YesNoRadioButtonList EnablePostPreviewPopup;
        protected YesNoRadioButtonList PostAsAnonymousUser;
		protected TextBox RSSDefaultThreadsPerFeed;
		protected RequiredFieldValidator RSSDefaultThreadsPerFeedValidator;
		protected YesNoRadioButtonList Tagging;

		// Editing
		protected YesNoRadioButtonList RequireEditNotes;
		protected YesNoRadioButtonList DisplayEditNotes;
		protected TextBox PostEditBodyAgeInMinutes;
		protected RequiredFieldValidator PostEditBodyAgeInMinutesValidator;

		// Attachments
		protected YesNoRadioButtonList EnableAttachments;
		protected TextBox AllowedAttachmentTypes;
		protected TextBox MaxAttachmentSize;
		protected YesNoRadioButtonList EnableInlinedImages;
		protected TextBox InlinedImageWidth;
		protected TextBox InlinedImageHeight;
		protected TextBox SupportedInlinedImageTypes;
		protected RequiredFieldValidator MaxAttachmentSizeValidator;
		protected RequiredFieldValidator InlinedImageWidthValidator;

		// Dups - Flooding
		protected YesNoRadioButtonList AllowDuplicatePosts;
		protected TextBox DuplicatePostIntervalInMinutes;
		protected RequiredFieldValidator DuplicatePostIntervalInMinutesValidator;
		protected YesNoRadioButtonList EnableFloodInterval;
		protected TextBox PostInterval;
		protected RequiredFieldValidator PostIntervalValidator;
		
		// Popular Posts
		protected TextBox PopularPostThresholdPosts;
		protected RequiredFieldValidator PopularPostThresholdPostsValidator;
		protected TextBox PopularPostThresholdViews;
		protected RequiredFieldValidator PopularPostThresholdViewsValidator;
		protected TextBox PopularPostThresholdDays;
		protected RequiredFieldValidator PopularPostThresholdDaysValidator;

		protected TextBox DaysPostMarkedAsRead;
		protected RequiredFieldValidator DaysPostMarkedAsReadValidator;

		protected StatusMessage Status;
		protected ResourceLinkButton SaveButton;

		protected FileOnlyStatusMessage FOStatus;
		protected CommunityServer.ControlPanel.Controls.ControlPanelSelectedNavigation SelectedNavigation1;
		protected CommunityServer.Controls.MPContent DescriptionRegion;
		protected CommunityServer.Controls.MPContent TaskRegion;
		protected CommunityServer.Controls.MPContainer MPContainer;
		protected PlaceHolder OptionHolder;


		#endregion

		override protected void OnInit(EventArgs e)
		{

			this.SaveButton.Click += new EventHandler(SaveButton_Click);
			this.Load += new EventHandler(this.Page_Load);

			base.OnInit(e);
		}


		private void Page_Load(object sender, EventArgs e)
		{
			if ( !Page.IsPostBack ) 
			{
				this.Bind();
			}
		}


		protected void Bind()
		{
			ForumConfiguration forumConfig = ForumConfiguration.Instance(false);

			Status.Visible = false;
			this.SaveButton.Text = ResourceManager.GetString( "Save" );

			if (forumConfig.FileOnly)
			{
				FOStatus.Visible = true;
				OptionHolder.Visible = false;
			}
			else
			{
				EnableForumsRSS.SelectedValue = forumConfig.EnableForumsRSS;
				EnableThreadStatus.SelectedValue = forumConfig.EnableThreadStatus;
				ThreadsPerPage.Text = forumConfig.ThreadsPerPage.ToString();
				PostsPerPage.Text = forumConfig.PostsPerPage.ToString();
				EnablePostPreviewPopup.SelectedValue = forumConfig.EnablePostPreviewPopup;
				RSSDefaultThreadsPerFeed.Text = forumConfig.RssDefaultThreadsPerFeed.ToString();

				RequireEditNotes.SelectedValue = forumConfig.RequireEditNotes;
				DisplayEditNotes.SelectedValue = forumConfig.DisplayEditNotesInPost;
				PostEditBodyAgeInMinutes.Text = forumConfig.PostEditBodyAgeInMinutes.ToString();

				EnableAttachments.SelectedValue = forumConfig.EnableAttachments;
				AllowedAttachmentTypes.Text = forumConfig.AllowedAttachmentTypes.ToString();
				MaxAttachmentSize.Text = forumConfig.MaxAttachmentSize.ToString();
				EnableInlinedImages.SelectedValue = forumConfig.EnableInlinedImages;
				InlinedImageWidth.Text = forumConfig.InlinedImageHeight.ToString();
				InlinedImageHeight.Text = forumConfig.InlinedImageWidth.ToString();
				SupportedInlinedImageTypes.Text = forumConfig.SupportedInlinedImageTypes;

				AllowDuplicatePosts.SelectedValue = forumConfig.EnableDuplicatePosts;
				DuplicatePostIntervalInMinutes.Text = forumConfig.DuplicatePostIntervalInMinutes.ToString();
				EnableFloodInterval.SelectedValue = forumConfig.EnableFloodIntervalChecking;
				PostInterval.Text = forumConfig.PostInterval.ToString();

				PopularPostThresholdPosts.Text = forumConfig.PopularPostLevelPosts.ToString();
				PopularPostThresholdViews.Text = forumConfig.PopularPostLevelViews.ToString();
				PopularPostThresholdDays.Text = forumConfig.PopularPostLevelDays.ToString();

				DaysPostMarkedAsRead.Text = forumConfig.DaysPostMarkedAsRead.ToString();
                PostAsAnonymousUser.SelectedValue = forumConfig.EnableUserPostingAsAnonymous;
				Tagging.SelectedValue = forumConfig.EnableTagging;
			}
		}

		private void SaveButton_Click(object sender, EventArgs e) 
		{
			ForumConfiguration forumConfig = ForumConfiguration.Instance(false);

			if (!forumConfig.FileOnly)
			{
				if(Page.IsValid)
				{
					SaveSettings(forumConfig);
					// Save Forum Config
					forumConfig.Save();

					Status.Success = true;
					Status.ResourceName = "Admin_SiteSettings_StatusSuccess";

					DataBind();
				}
				else
				{
					Status.Success = false;
					Status.ResourceName = "Admin_SiteSettings_StatusFailed";
				}

				Status.Visible = true;
			}
		}

		private void InitializeComponent()
		{
		
		}


		protected virtual void SaveSettings(ForumConfiguration forumConfig) 
		{

forumConfig.EnableForumsRSS = EnableForumsRSS.SelectedValue;
			forumConfig.EnableThreadStatus = EnableThreadStatus.SelectedValue;
			forumConfig.EnablePostPreviewPopup = EnablePostPreviewPopup.SelectedValue;

			try 
			{ 
				forumConfig.RssDefaultThreadsPerFeed = Int32.Parse( RSSDefaultThreadsPerFeed.Text ); 
				if(forumConfig.RssDefaultThreadsPerFeed < 1)
					RSSDefaultThreadsPerFeedValidator.IsValid = false;
			}
			catch { RSSDefaultThreadsPerFeedValidator.IsValid = false; }


			try 
			{ 
				forumConfig.ThreadsPerPage = Int32.Parse( ThreadsPerPage.Text ); 
				if(forumConfig.ThreadsPerPage < 1)
					ThreadsPerPageValidator.IsValid = false;
			}
			catch { ThreadsPerPageValidator.IsValid = false; }
			
			try 
			{ 
				forumConfig.PostsPerPage = Int32.Parse( PostsPerPage.Text ); 
				if(forumConfig.PostsPerPage < 1)
					PostsPerPageValidator.IsValid = false;
			}
			catch { PostsPerPageValidator.IsValid = false; }


			forumConfig.RequireEditNotes = RequireEditNotes.SelectedValue;
			forumConfig.DisplayEditNotesInPost = DisplayEditNotes.SelectedValue;
			try 
			{ 
				forumConfig.PostEditBodyAgeInMinutes = Int32.Parse( PostEditBodyAgeInMinutes.Text ); 
				if(Int32.Parse( PostEditBodyAgeInMinutes.Text ) < 0)
					PostEditBodyAgeInMinutesValidator.IsValid = false;
			}
			catch { PostEditBodyAgeInMinutesValidator.IsValid = false; }

            forumConfig.EnableUserPostingAsAnonymous = PostAsAnonymousUser.SelectedValue;
			forumConfig.EnableTagging = Tagging.SelectedValue;
			forumConfig.EnableAttachments = EnableAttachments.SelectedValue;
			forumConfig.AllowedAttachmentTypes = AllowedAttachmentTypes.Text;
			forumConfig.EnableInlinedImages = EnableInlinedImages.SelectedValue;
			forumConfig.SupportedInlinedImageTypes = SupportedInlinedImageTypes.Text;
			try 
			{ 
				forumConfig.MaxAttachmentSize = Int32.Parse( MaxAttachmentSize.Text ); 
				if(Int32.Parse( MaxAttachmentSize.Text ) < 0)
					MaxAttachmentSizeValidator.IsValid = false;
			}
			catch { MaxAttachmentSizeValidator.IsValid = false; }
			try 
			{ 
				forumConfig.InlinedImageWidth = Int16.Parse(InlinedImageWidth.Text); 
				if(Int32.Parse( InlinedImageWidth.Text ) < -1)
					InlinedImageWidthValidator.IsValid = false;
			}
			catch { InlinedImageWidthValidator.IsValid = false; }
			try 
			{ 
				forumConfig.InlinedImageHeight = Int16.Parse(InlinedImageHeight.Text); 
				if(Int32.Parse( InlinedImageHeight.Text ) < -1)
					InlinedImageWidthValidator.IsValid = false;
			}
			catch { InlinedImageWidthValidator.IsValid = false; }



			forumConfig.EnableDuplicatePosts = AllowDuplicatePosts.SelectedValue;
			forumConfig.EnableFloodIntervalChecking = EnableFloodInterval.SelectedValue;
			try 
			{ 
				forumConfig.DuplicatePostIntervalInMinutes = Int32.Parse( DuplicatePostIntervalInMinutes.Text ); 
				if(Int32.Parse( DuplicatePostIntervalInMinutes.Text ) < 0)
					DuplicatePostIntervalInMinutesValidator.IsValid = false;
			}
			catch { DuplicatePostIntervalInMinutesValidator.IsValid = false; }
			try 
			{ 
				forumConfig.PostInterval = Int32.Parse( PostInterval.Text ); 
				if(Int32.Parse( PostInterval.Text ) < 1)
					PostIntervalValidator.IsValid = false;
			}
			catch { PostIntervalValidator.IsValid = false; }

			
			try 
			{ 
				forumConfig.PopularPostLevelPosts = Int32.Parse( PopularPostThresholdPosts.Text ); 

				if(Int32.Parse( PopularPostThresholdPosts.Text ) < 1)
					PopularPostThresholdPostsValidator.IsValid = false;
			}
			catch { PopularPostThresholdPostsValidator.IsValid = false; }
			try 
			{ 
				forumConfig.PopularPostLevelViews = Int32.Parse( PopularPostThresholdViews.Text ); 

				if(Int32.Parse( PopularPostThresholdViews.Text ) < 1)
					PopularPostThresholdViewsValidator.IsValid = false;
			}
			catch { PopularPostThresholdViewsValidator.IsValid = false; }
			try 
			{ 
				forumConfig.PopularPostLevelDays = Int32.Parse( PopularPostThresholdDays.Text ); 
				
				if(Int32.Parse( PopularPostThresholdDays.Text ) < 1)
					PopularPostThresholdDaysValidator.IsValid = false;
			}
			catch { PopularPostThresholdDaysValidator.IsValid = false; }

			try
			{
				forumConfig.DaysPostMarkedAsRead = Int32.Parse( DaysPostMarkedAsRead.Text );

				if(Int32.Parse( DaysPostMarkedAsRead.Text ) < 1)
					DaysPostMarkedAsReadValidator.IsValid = false;
			}
			catch { DaysPostMarkedAsReadValidator.IsValid = false; }
		}

	}
}
