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

using System;
using System.Xml;
using CommunityServer.Components;
using CommunityServer.Configuration;

namespace CommunityServer.Reader.Components
{
	/// <summary>
	/// Summary description for ReaderConfiguration.
	/// </summary>
	[Serializable]
	public class ReaderConfiguration : ApplicationConfiguration
	{

        public static bool ValidateUser(User user)
        {
            return user.IsReaderAdministrator || user.IsInRoles(Instance().Roles);            
        }

		#region Private Members


		private int _lastModifiedInterval = 60;
		private int _truncationLength = 150;
		private int _pageSize = 25;
		private int _rssSize = 25;
		private bool _rollerenabled = true;
        private string[] _roles;


		#endregion


		#region Instance() Method

        private static readonly Type thisType = typeof(ReaderConfiguration);

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

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


		#endregion

        
		#region Constructor(s)


		public ReaderConfiguration()
		{
			
		}


		#endregion


		#region Public Properties

        public bool FeedReaderEnabled
        {
            get{return this.Enabled;}
            set{this.Enabled = value;}
        }

        public bool RollerEnabled
        {
            get{return _rollerenabled;}
            set{_rollerenabled = value;}
        }
		
		public int LastModifiedInterval
		{
			get { return _lastModifiedInterval; }
			set { _lastModifiedInterval = value; }
		}


		public int TruncationLength
		{
			get { return _truncationLength; }
			set { _truncationLength = value; }
		}

		public int PageSize
		{
			get { return _pageSize; }
			set { _pageSize = value; }
		}

		public int RSSSize
		{
			get { return _rssSize; }
			set { _rssSize = value; }
		}

        public string[] Roles
        {
            get{return _roles;}
            set{_roles = value;}
        }

		#endregion

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

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

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


				// RollerEnabled
                XmlAttribute enableBlogRoller = attcol["enableBlogRoller"];
                if(enableBlogRoller != null)
                    this.RollerEnabled = bool.Parse(enableBlogRoller.Value);

                // LastModifiedInterval Attribute
                XmlAttribute lastModifiedInterval = attcol["LastModifiedInterval"];
                if (lastModifiedInterval != null)
                    this._lastModifiedInterval = Int32.Parse(lastModifiedInterval.Value);

                XmlAttribute roles = node.Attributes["roles"];
                if(roles != null)
                    this.Roles = roles.Value.Split(';');
                else
                    this.Roles = new string[]{CSConfiguration.GetConfig().RolesConfiguration.ReaderAdministrator};

					
                // Blog Roller Attributes
                XmlNode br = node.SelectSingleNode("BlogRoller");
					
                if (br != null)
                {
                    // TruncationLength Attribute
                    XmlAttribute truncationLength = br.Attributes["TruncationLength"];
                    if (truncationLength != null)
                        this._truncationLength = Int32.Parse(truncationLength.Value);

					// Page Size
					XmlAttribute pageSize = br.Attributes["PageSize"];
					if (pageSize != null)
						this._pageSize = Int32.Parse(pageSize.Value);

					// RSS Size
					XmlAttribute rssSize = br.Attributes["RSSSize"];
					if (rssSize != null)
						this._rssSize = Int32.Parse(rssSize.Value);
                }
            }
	    }

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