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

using System;
using System.Collections;
using CommunityServer.Components;
using CommunityServer.Blogs.Components;
using CommunityServer.Discussions.Components;
using CommunityServer.Files;
using CommunityServer.Files.Components;
using CommunityServer.Galleries;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Spam
{
	/// <summary>
	/// Base class for all generic spam rules.
	/// Includes built-in checks for which posts to process.
	/// </summary>
	public abstract class SpamRule
	{
		public abstract string Name { get; }
		public abstract string Description { get; }
		public abstract Guid RuleID { get; }
		public abstract ArrayList GetAvailableSettings();
		public abstract int RatePost(Post post, CSPostEventArgs e);

		public virtual bool IsExemptFromSpamRating(Post post, CSPostEventArgs e)
		{
			// Only rate forum, blog, photo, file related content
			if ((e.ApplicationType != ApplicationType.Forum)
				&& (e.ApplicationType != ApplicationType.Weblog)
				&& (e.ApplicationType != ApplicationType.Gallery)
				&& (e.ApplicationType != ApplicationType.FileGallery))
				return true;

			// For forums, only rate posts from non-admins
			ForumPost fp = post as ForumPost;
			if ((fp != null) && (fp.User.IsForumAdministrator))
				return true;

			// For blogs, only rate feedback (comments and trackbacks)
			WeblogPost wp = post as WeblogPost;
			if ((wp != null) && (wp.BlogPostType != BlogPostType.Comment) && (wp.BlogPostType != BlogPostType.Trackback))
				return true;

			// For photos, only rate feedback
			GalleryPost gp = post as GalleryPost;
			if ((gp != null) && (gp.GalleryPostType != GalleryPostType.Comment))
				return true;

			// For files, only rate feedback
			Entry en = post as Entry;
			if ((en != null) && (en.FileGalleryPostType != FileGalleryPostType.Comment))
				return true;

			// Nothing else is exempt
			return false;
		}


		public string TypeName
		{
			get { return GetType().AssemblyQualifiedName; }
		}

		public Hashtable GetConfiguredSettings()
		{
			return SpamRules.GetRuleSettings(RuleID);
		}

		public string GetSettingValue(string key)
		{
			Hashtable settings = GetConfiguredSettings();
			if (settings.ContainsKey(key))
			{
				RuleSetting setting = settings[key] as RuleSetting;
				return setting.Value;
			}

			return null;
		}

		public virtual PassCriteria PassCriteria
		{
			get { return PassCriteria.FirstPass; }
		}

		public virtual ScoreCriteria ScoreCriteria
		{
			get { return ScoreCriteria.AnyScore; }
		}

		public virtual StatusCriteria StatusCriteria
		{
			get { return StatusCriteria.AnyStatus; }
		}
	}
}
