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

using System;
using System.Collections;
using System.Web.Caching;
using System.Xml;
using CommunityServer.Components;
using CommunityServer.Configuration;

namespace CommunityServer.Discussions.Components
{

	/// <summary>
	/// Forum Configuration Settings
	/// </summary>
	[Serializable]
	public class ForumConfiguration : ApplicationConfiguration
	{

		public ForumConfiguration()
		{

		}


        private static readonly Type thisType = typeof(ForumConfiguration);

        public static ForumConfiguration Instance()
        {
            return Instance(true);
        }

		public static ForumConfiguration Instance(bool useCache)
		{
            return Instance(thisType, useCache) as ForumConfiguration;
		}


		#region Public Properties

		private int _DaysPostMarkedAsRead = 7;
		public int DaysPostMarkedAsRead
		{
			get
			{
				return _DaysPostMarkedAsRead;
			}
			set
			{
				_DaysPostMarkedAsRead = value;
			}
		}

		private Int32 _MaxAttachmentSize = 64; // in Kilobytes
		public int MaxAttachmentSize
		{
			get 
			{
				return _MaxAttachmentSize;
			}
			set 
			{
				_MaxAttachmentSize = value;
			}
		}
		
		private string _AllowedAttachmentTypes = "zip;cab;jpg;gif;png;mpg;mpeg;avi;wmv;wma;mp3;ra;rar;rm;sql;txt";
		public string AllowedAttachmentTypes
		{
			get 
			{
				return _AllowedAttachmentTypes;
			}
			set 
			{
				_AllowedAttachmentTypes = value;
			}
		}

        bool _EnableUserPostingAsAnonymous = false;
        public bool EnableUserPostingAsAnonymous 
        {
            get
            {
				
                return _EnableUserPostingAsAnonymous;
            }
            set { _EnableUserPostingAsAnonymous = value;	}
        }

		bool _EnableTagging = true;
		public bool EnableTagging
		{
			get
			{
				return _EnableTagging;
			}
			set
			{
				_EnableTagging = value;
			}
		}

		private ForumAttachmentSettings _fileSettings;
		[ReadFromConfigurationFileOnly()]
		public ForumAttachmentSettings AttachmentSettings
		{
			get{ return _fileSettings;}
			set{ if (value != null) { _fileSettings=value; } }
		}

		bool _EnableForumsRSS = true;
		public bool EnableForumsRSS 
		{
			get 
			{
               
				return _EnableForumsRSS;
			}
			set 
			{
				_EnableForumsRSS = value;
			}
		}

		int _RssDefaultThreadsPerFeed = 25;
		public int RssDefaultThreadsPerFeed 
		{
			get 
			{
				return _RssDefaultThreadsPerFeed;
			}
			set 
			{
				_RssDefaultThreadsPerFeed = value;
			}
		}

		int _RssMaxThreadsPerFeed = 50;
		public int RssMaxThreadsPerFeed 
		{
			get 
			{
                
				return _RssMaxThreadsPerFeed;
			}
			set 
			{
				_RssMaxThreadsPerFeed = value;
			}
		}

		int _RssCacheWindowInSeconds = 90;
		public int RssCacheWindowInSeconds 
		{
			get 
			{
				return _RssCacheWindowInSeconds;
			}
			set 
			{
				_RssCacheWindowInSeconds = value;
			}
		}

		private bool _EnableAttachments = true;
		public bool EnableAttachments 
		{
			get 
			{
				return _EnableAttachments;
			}
			set 
			{
				_EnableAttachments = value;
			}
		} 		
		
		private bool _EnablePostReporting = true;
		public bool EnablePostReporting 
		{
			get 
			{
				return _EnablePostReporting;
			}
			set 
			{
				_EnablePostReporting = value;
			}
		} 		

		private bool _EnableAnonymousReporting = true;
		public bool EnableAnonymousReporting 
		{
			get 
			{

				return _EnableAnonymousReporting;
			}
			set 
			{
				_EnableAnonymousReporting = value;
			}
		} 		
		
		private int _ReportingForum = 3;	// default admin forum for reporting posts.
		public int ReportingForum
		{
			get 
			{
	
				return _ReportingForum;
			}
			set 
			{
				_ReportingForum = value;
			}
		}

		bool _EnablePostPreviewPopup = false;
		public bool EnablePostPreviewPopup 
		{
			get
			{
				return _EnablePostPreviewPopup;
			}
			set 
			{
				_EnablePostPreviewPopup = value;
			}
		}

		bool _EnableInlinedImages = false;
		public bool EnableInlinedImages 
		{
			get
			{ 
				return _EnableInlinedImages;
			}
			set{ _EnableInlinedImages = value; }
		}

		short _InlinedImageWidth = 550;
		public short InlinedImageWidth 
		{
			get
			{
				
				return _InlinedImageWidth;
			}
			set{ _InlinedImageWidth = value;	}
		}

		short _InlinedImageHeight = -1;
		public short InlinedImageHeight 
		{
			get
			{
		
				return _InlinedImageHeight;
			}
			set{ _InlinedImageHeight = value; }
		}

		string _SupportedInlinedImageTypes = "jpg;gif;bmp;pcx;png;pic";
		public string SupportedInlinedImageTypes 
		{
			get
			{
	
				return _SupportedInlinedImageTypes;
			}
			set{ _SupportedInlinedImageTypes = value; }
		}

		bool _EnableThreadStatus = false;
		public bool EnableThreadStatus 
		{
			get
			{
	
				return _EnableThreadStatus;
			}
			set { _EnableThreadStatus = value; }
		}

		int _ThreadsPerPage = 20;
		public int ThreadsPerPage 
		{
			get 
			{
                
				return _ThreadsPerPage;
			}
			set 
			{
				_ThreadsPerPage = value;
			}
		}

		int _PostsPerPage = 15;
		public int PostsPerPage 
		{
			get 
			{

				return _PostsPerPage;
			}
			set 
			{
				_PostsPerPage = value;
			}
		}

		#region Popular Posts

		int _PopularPostLevelPosts = 15;
		public int PopularPostLevelPosts 
		{
			get 
			{
                
				return _PopularPostLevelPosts;
			}
			set 
			{
				_PopularPostLevelPosts = value;
			}
		}

		int _PopularPostLevelViews = 200;
		public int PopularPostLevelViews 
		{
			get 
			{
                
				return _PopularPostLevelViews;
			}
			set 
			{
				_PopularPostLevelViews = value;
			}
		}
        
		int _PopularPostLevelDays = 3;
		public int PopularPostLevelDays 
		{
			get 
			{
                
				return _PopularPostLevelDays;
			}
			set 
			{
				_PopularPostLevelDays = value;
			}
		}
		#endregion


		#region Duplicate Posts
		bool _EnableDuplicatePosts = false;
		public bool EnableDuplicatePosts 
		{
			get 
			{
            
				return _EnableDuplicatePosts;
			}
			set 
			{
				_EnableDuplicatePosts = value;
			}
		}

		int _DuplicatePostIntervalInMinutes = 15;
		public int DuplicatePostIntervalInMinutes 
		{
			get 
			{
				return _DuplicatePostIntervalInMinutes;
			}
			set 
			{
				_DuplicatePostIntervalInMinutes = value;
			}
		}
		#endregion

		int _PostInterval = 15;
		public int PostInterval 
		{
			get 
			{
               
				return _PostInterval;
			}
			set 
			{
				_PostInterval = value;
			}
		}

		#region Post Editing
		bool _DisplayEditNotesInPost = false;
		public bool DisplayEditNotesInPost 
		{
			get 
			{
                
				return _DisplayEditNotesInPost;
			}
			set 
			{
				_DisplayEditNotesInPost = value;
			}
		}

		bool _RequireEditNotes = false;
		public bool RequireEditNotes 
		{
			get 
			{
				
				return _RequireEditNotes;
			}
			set 
			{
				_RequireEditNotes = value;
			}
		}

		int _PostEditBodyAgeInMinutes = 0;
		public int PostEditBodyAgeInMinutes 
		{
			get 
			{
                
				return _PostEditBodyAgeInMinutes;
			}
			set 
			{
				_PostEditBodyAgeInMinutes = value;
			}
		}

		int _PostEditTitleAgeInMinutes = 5;
		public int PostEditTitleAgeInMinutes 
		{
			get 
			{
                
				return _PostEditTitleAgeInMinutes;
			}
			set 
			{
				_PostEditTitleAgeInMinutes = value;
			}
		}

		int _PostDeleteAgeInMinutes = 5;
		public int PostDeleteAgeInMinutes 
		{
			get 
			{
				return _PostDeleteAgeInMinutes;
			}
			set 
			{
				_PostDeleteAgeInMinutes = value;
			}
		}
		#endregion


		#region Post Flooding
		bool _EnableFloodIntervalChecking = false;
		public bool EnableFloodIntervalChecking 
		{
			get
			{
				return _EnableFloodIntervalChecking;
			}
			set 
			{
				_EnableFloodIntervalChecking= value; 
			}
		}
		
		int _MinimumTimeBetweenPosts = 15;
		public int MinimumTimeBetweenPosts 
		{
			get 
			{
				
				return _MinimumTimeBetweenPosts;
			}
			set 
			{
				_MinimumTimeBetweenPosts = value; 
			}
		}
		#endregion
		
		
		#endregion

	    protected override string ConfigSection
	    {
	        get { return "CommunityServer/Forums"; }
	    }

	    protected override void Initialize(XmlNode node)
	    {
            base.Initialize(node);

			if(node != null)
			{
				XmlNode fs = node.SelectSingleNode("AttachmentSettings");
				this.AttachmentSettings =  Serializer.ConvertToObject(fs,typeof(ForumAttachmentSettings)) as ForumAttachmentSettings;

				XmlAttributeCollection attributes = node.Attributes;

				XmlAttribute daysPostMarkedAsRead  = attributes["daysPostMarkedAsRead"];
				if (daysPostMarkedAsRead != null)
					this._DaysPostMarkedAsRead = int.Parse(daysPostMarkedAsRead.Value);

				XmlAttribute maxAttachmentSize  = attributes["maxAttachmentSize"];
				if (maxAttachmentSize != null)
					this._MaxAttachmentSize = int.Parse(maxAttachmentSize.Value);

				XmlAttribute allowedAttachmentTypes  = attributes["allowedAttachmentTypes"];
				if (allowedAttachmentTypes != null)
					this._AllowedAttachmentTypes = allowedAttachmentTypes.Value;

				XmlAttribute displayEditNotesInPost = attributes["displayEditNotesInPost"];
				if (displayEditNotesInPost != null)
					this._DisplayEditNotesInPost = bool.Parse(displayEditNotesInPost.Value);

				XmlAttribute duplicatePostIntervalInMinutes = attributes["duplicatePostIntervalInMinutes"];
				if (duplicatePostIntervalInMinutes != null)
					this._DuplicatePostIntervalInMinutes = int.Parse(duplicatePostIntervalInMinutes.Value);

				XmlAttribute enableAnonymousReporting = attributes["enableAnonymousReporting"];
				if (enableAnonymousReporting != null)
					this._EnableAnonymousReporting = bool.Parse(enableAnonymousReporting.Value);

                XmlAttribute enableUserPostingAsAnonymous = attributes["enableUserPostingAsAnonymous"];
                if(enableUserPostingAsAnonymous != null)
                    this._EnableUserPostingAsAnonymous = bool.Parse(enableUserPostingAsAnonymous.Value);

				XmlAttribute enableTagging = attributes["enableTagging"];
				if(enableTagging != null)
					this._EnableTagging = bool.Parse(enableTagging.Value);

				XmlAttribute enableAttachments = attributes["enableAttachments"];
				if (enableAttachments != null)
					this._EnableAttachments = bool.Parse(enableAttachments.Value);

				XmlAttribute enableDuplicatePosts = attributes["enableDuplicatePosts"];
				if (enableDuplicatePosts != null)
					this._EnableDuplicatePosts = bool.Parse(enableDuplicatePosts.Value);

				XmlAttribute enableFloodIntervalChecking = attributes["enableFloodIntervalChecking"];
				if (enableFloodIntervalChecking != null)
					this._EnableFloodIntervalChecking = bool.Parse(enableFloodIntervalChecking.Value);

				XmlAttribute enableForumsRSS = attributes["enableForumsRSS"];
				if (enableForumsRSS != null)
					this._EnableForumsRSS = bool.Parse(enableForumsRSS.Value);

				XmlAttribute enableInlinedImages = attributes["enableInlinedImages"];
				if (enableInlinedImages != null)
					this._EnableInlinedImages = bool.Parse(enableInlinedImages.Value);

				XmlAttribute enablePostPreviewPopup = attributes["enablePostPreviewPopup"];
				if (enablePostPreviewPopup != null)
					this._EnablePostPreviewPopup = bool.Parse(enablePostPreviewPopup.Value);

				XmlAttribute enablePostReporting = attributes["enablePostReporting"];
				if (enablePostReporting != null)
					this._EnablePostReporting = bool.Parse(enablePostReporting.Value);

				XmlAttribute enableThreadStatus = attributes["enableThreadStatus"];
				if (enableThreadStatus != null)
					this._EnableThreadStatus = bool.Parse(enableThreadStatus.Value);

				XmlAttribute inlinedImageHeight = attributes["inlinedImageHeight"];
				if (inlinedImageHeight != null)
					this._InlinedImageHeight = short.Parse(inlinedImageHeight.Value);

				XmlAttribute inlinedImageWidth = attributes["inlinedImageWidth"];
				if (inlinedImageWidth != null)
					this._InlinedImageWidth = short.Parse(inlinedImageWidth.Value);

				XmlAttribute minimumTimeBetweenPosts = attributes["minimumTimeBetweenPosts"];
				if (minimumTimeBetweenPosts != null)
					this._MinimumTimeBetweenPosts = int.Parse(minimumTimeBetweenPosts.Value);

				XmlAttribute popularPostLevelDays = attributes["popularPostLevelDays"];
				if (popularPostLevelDays != null)
					this._PopularPostLevelDays = int.Parse(popularPostLevelDays.Value);

				XmlAttribute popularPostLevelPosts = attributes["popularPostLevelPosts"];
				if (popularPostLevelPosts != null)
					this._PopularPostLevelPosts = int.Parse(popularPostLevelPosts.Value);

				XmlAttribute popularPostLevelViews = attributes["popularPostLevelViews"];
				if (popularPostLevelViews != null)
					this._PopularPostLevelViews = int.Parse(popularPostLevelViews.Value);

				XmlAttribute postDeleteAgeInMinutes = attributes["postDeleteAgeInMinutes"];
				if (postDeleteAgeInMinutes != null)
					this._PostDeleteAgeInMinutes = int.Parse(postDeleteAgeInMinutes.Value);

				XmlAttribute postEditBodyAgeInMinutes = attributes["postEditBodyAgeInMinutes"];
				if (postEditBodyAgeInMinutes != null)
					this._PostEditBodyAgeInMinutes = int.Parse(postEditBodyAgeInMinutes.Value);

				XmlAttribute postEditTitleAgeInMinutes = attributes["postEditTitleAgeInMinutes"];
				if (postEditTitleAgeInMinutes != null)
					this._PostEditTitleAgeInMinutes = int.Parse(postEditTitleAgeInMinutes.Value);

				XmlAttribute postInterval = attributes["postInterval"];
				if (postInterval != null)
					this._PostInterval = int.Parse(postInterval.Value);

				XmlAttribute postsPerPage = attributes["postsPerPage"];
				if (postsPerPage != null)
					this._PostsPerPage = int.Parse(postsPerPage.Value);

				XmlAttribute reportingForum = attributes["reportingForum"];
				if (reportingForum != null)
					this._ReportingForum = int.Parse(reportingForum.Value);

				XmlAttribute requireEditNotes = attributes["requireEditNotes"];
				if (requireEditNotes != null)
					this._RequireEditNotes = bool.Parse(requireEditNotes.Value);

				XmlAttribute rssCacheWindowInSeconds = attributes["rssCacheWindowInSeconds"];
				if (rssCacheWindowInSeconds != null)
					this._RssCacheWindowInSeconds = int.Parse(rssCacheWindowInSeconds.Value);

				XmlAttribute rssDefaultThreadsPerFeed = attributes["rssDefaultThreadsPerFeed"];
				if (rssDefaultThreadsPerFeed != null)
					this._RssDefaultThreadsPerFeed = int.Parse(rssDefaultThreadsPerFeed.Value);

				XmlAttribute rssMaxThreadsPerFeed = attributes["rssMaxThreadsPerFeed"];
				if (rssMaxThreadsPerFeed != null)
					this._RssMaxThreadsPerFeed = int.Parse(rssMaxThreadsPerFeed.Value);

				XmlAttribute supportedInlinedImageTypes = attributes["supportedInlinedImageTypes"];
				if (supportedInlinedImageTypes != null)
					this._SupportedInlinedImageTypes = supportedInlinedImageTypes.Value;

				XmlAttribute threadsPerPage = attributes["threadsPerPage"];
				if (threadsPerPage != null)
					this._ThreadsPerPage = int.Parse(threadsPerPage.Value);
			}
	    }

	    public override ApplicationType ApplicationType
	    {
	        get { return ApplicationType.Forum; }
	    }
	}
}
