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

using System;
using System.Collections;
using System.Xml;
using CommunityServer.Components;

namespace CommunityServer.Blogs.Components
{
	/// <summary>
	/// Summary description for WeblogConfiguration.
	/// </summary>
	[Serializable]
    public class WeblogConfiguration : ThemedApplicationKeyApplicationConfiguration
	{
        private static readonly Type thisType = typeof(WeblogConfiguration);
		protected override string ThemeLocationDefault() { return "~/Themes/Blogs/"; }


		#region Instance

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

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

		#endregion

		#region Overridden Base Class Members
		protected override string ConfigSection
		{
			get { return "CommunityServer/Weblog"; }
		}

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

		#endregion

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


			if(node != null)
			{
				XmlAttributeCollection attcol = node.Attributes;


				XmlAttribute postSize = attcol["aggregatePostSize"];
				if(postSize != null)
					this._aggregatePostSize = Int32.Parse(postSize.Value);

				XmlAttribute blogDirs = attcol["createDirectories"];
				if(blogDirs != null)
					this._createBlogDirectories = bool.Parse(blogDirs.Value);

				XmlAttribute spc = attcol["servicePostCountLimit"];
				if(spc != null)
					this._servicePostCountLimit = Int32.Parse(spc.Value);

				XmlAttribute apc = attcol["aggregatePostCount"];
				if(apc != null)
					this._aggregatePostCount = Int32.Parse(apc.Value);

				XmlAttribute ipc = attcol["individualPostCount"];
				if(ipc != null)
					this._individualPostCount = Int32.Parse(ipc.Value);

				XmlAttribute cr = attcol["enableCommentRedirects"];
				if(cr != null)
					this._enableCommentRedirects = bool.Parse(cr.Value);

				XmlAttribute snf = attcol["authenticateOnSectionNotFound"];
				if(snf != null)
					this._authenticateOnSectionNotFound = bool.Parse(snf.Value);

                XmlAttribute dsf = attcol["dateScoreFactor"];
                if(dsf != null)
                    this.DateScoreFactor = double.Parse(dsf.Value);

                XmlAttribute vsf = attcol["viewScoreFactor"];
                if(vsf != null)
                    this.ViewScoreFactor = double.Parse(vsf.Value);

                XmlAttribute rsf = attcol["ratingScoreFactor"];
                if(rsf != null)
                    this.RatingScoreFactor = double.Parse(rsf.Value);

                XmlAttribute csf = attcol["commentScoreFactor"];
                if(csf != null)
                    this.CommentScoreFactor = double.Parse(csf.Value);

                XmlAttribute sp = attcol["scorePosts"];
                if(sp != null)
                    this._scorePosts = bool.Parse(sp.Value);

                XmlAttribute pts = attcol["postsToScore"];
                if(pts != null)
                    this.PostsToScore = int.Parse(pts.Value);

                XmlAttribute securityFilter = attcol["disableSecurityFilter"];
                if(securityFilter != null)
                    this._disableSecurityFilter = bool.Parse(securityFilter.Value);
                   
				XmlNode fs = node.SelectSingleNode("AttachmentSettings");
				this.AttachmentSettings =  Serializer.ConvertToObject(fs,typeof(WeblogAttachmentSettings)) as WeblogAttachmentSettings;


				XmlNode dps = node.SelectSingleNode("DefaultPingServices");
				if (dps != null && dps.HasChildNodes)
				{
					ArrayList pingServices = new ArrayList(dps.ChildNodes.Count);
					for (int i = 0; i < dps.ChildNodes.Count; i++)
					{
						if (dps.ChildNodes[i].NodeType == XmlNodeType.Element)
						{
							XmlAttribute url = dps.ChildNodes[i].Attributes["url"];
							if (url != null)
								pingServices.Add(url.Value.Trim());
						}
					}

					// Copy ArrayList values to string array
					this.DefaultPingServices = new string[pingServices.Count];
					for (int i = 0; i < pingServices.Count; i++)
					{
						this.DefaultPingServices[i] = pingServices[i] as string;
					}
				}
					

			}
		}

		#endregion

		#region Properties

        private bool _scorePosts;

        public bool ScorePosts
        {
            get{return _scorePosts;}
            set{_scorePosts = value;}
        }

        private int _postsToScore = 250;
        public int PostsToScore
        {
            get{ return _postsToScore;}
            set{_postsToScore = value;}
        }

        private double _dateScoreFactor = 2.5;
        public double DateScoreFactor
        {
            get{ return _dateScoreFactor;}
            set{_dateScoreFactor = value;}
        }

        private double _ratingScoreFactor = 0.75;
        public double RatingScoreFactor
        {
            get{ return _ratingScoreFactor;}
            set{_ratingScoreFactor = value;}
        }

        private double _viewScoreFactor = 0.5;
        public double ViewScoreFactor
        {
            get{ return _viewScoreFactor;}
            set{_viewScoreFactor = value;}
        }

        private double _commentScoreFactor = 0.75;
        public double CommentScoreFactor
        {
            get{ return _commentScoreFactor;}
            set{_commentScoreFactor = value;}
        }

        private int _defaultGroupID = -1;
        public int DefaultGroupID
        {
            get{ return _defaultGroupID;}
            set{_defaultGroupID = value;}
        }


		private bool _enableAutoCreate = false;
        public bool EnableAutoCreate
        {
            get{return _enableAutoCreate;}
            set{_enableAutoCreate = value;}
        }


		private bool _authenticateOnSectionNotFound = true;
		public bool AuthenticateOnSectionNotFound
		{
			get{return _authenticateOnSectionNotFound;}
			set{_authenticateOnSectionNotFound = value;}
		}


        private WeblogAttachmentSettings _fileSettings;
        [ReadFromConfigurationFileOnly]
        public WeblogAttachmentSettings AttachmentSettings
        {
            get{ return _fileSettings;}
            set{_fileSettings=value;}
        }


		private bool _enableCommentRedirects = true; 
        public bool EnableCommentRedirects
        {
            get{return _enableCommentRedirects;}
        }

        
		private int _aggregatePostSize = -1;
		public int AggregatePostSize
		{
			get{return _aggregatePostSize;}
            set{_aggregatePostSize = value;}
		}

		public bool TruncateAggregatePost
		{
			get{return AggregatePostSize > 0; }
		}

		
		bool _createBlogDirectories = true;
		public bool CreateBlogDirectories
		{
			get{ return _createBlogDirectories; }
            set{_createBlogDirectories = value;}
		}

		
		private int _servicePostCountLimit = 25;
		public int ServicePostCountLimit
		{
			get{return _servicePostCountLimit;}
            set{_servicePostCountLimit = value;}
		}

		
		private int _aggregatePostCount = 25;
		public int AggregatePostCount
		{
			get { return _aggregatePostCount;}
            set {_aggregatePostCount = value;}
		}

		
		private int _individualPostCount = 15;
		public int IndividualPostCount
		{
			get { return _individualPostCount;}
            set {_individualPostCount = value;}
		}
		
        
		private bool _enableRss = true;
        public bool EnableRss
        {
            get{return _enableRss;}
            set{_enableRss = value;}
        }


		private bool _enableAtom = true;
		public bool EnableAtom
        {
            get{return _enableAtom;}
            set{_enableAtom = value;}
        }

		
		private string[] _defaultPingServices = new string[0];
		/// <summary>
		/// Property DefaultPingServices (string)
		/// </summary>
		public string[] DefaultPingServices
		{
			get {  return this._defaultPingServices; }
			set { _defaultPingServices = value;}
		}

		
		private bool _enableCrossPosting = false;
		public bool EnableCrossPosting
		{
			get{return _enableCrossPosting;}
			set{_enableCrossPosting = value;}
		}


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

		private string[] _defaultAggregateTags = new string[0];
		/// <summary>
		/// Property DefaultAggregateTags (string)
		/// </summary>
		public string[] DefaultAggregateTags
		{
			get { return this._defaultAggregateTags; }
			set { _defaultAggregateTags = value; }
		}

        public bool HasAggregateTags
        {
            get { return DefaultAggregateTags != null && DefaultAggregateTags.Length > 0;}
        }

		private bool _enableRawHeadEditing = false;
		public bool EnableRawHeadEditing
		{
			get{return _enableRawHeadEditing;}
			set{_enableRawHeadEditing = value;}
		}

        private bool _disableSecurityFilter = false;
        [ReadFromConfigurationFileOnly]
        public bool DisableSecurityFilter
        {
            get{return _disableSecurityFilter;}
            //set{_disableSecurityFilter = value;}
        }

		#endregion

	}
}
