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

using System;
using CommunityServer.Components;
using CommunityServer.Files;
using CommunityServer.Files.Components;

namespace CommunityServer.Spam
{
	/// <summary>
	/// Base class for all file specific spam rules.
	/// Includes built-in checks for which file posts to process.
	/// </summary>
	public abstract class FileSpamRule : SpamRule
	{
		/// <summary>
		/// Responsible for spam-rating a post
		/// </summary>
		/// <param name="post"></param>
		/// <param name="e"></param>
		/// <returns></returns>
		public override int RatePost(Post post, CSPostEventArgs e)
		{
			// Can we, and should we rate this post?
			Entry entry = post as Entry;
			if ((entry == null) || (IsExemptFromSpamRating(post, e)))
				return 0;

			return CalculateSpamScore(entry, e);
		}


		/// <summary>
		/// Responsible for determining if this post should be spam-rated
		/// </summary>
		/// <param name="post"></param>
		/// <param name="e"></param>
		/// <returns></returns>
		public override bool IsExemptFromSpamRating(Post post, CSPostEventArgs e)
		{
			// Only rate file related content
			if (e.ApplicationType != ApplicationType.FileGallery)
				return true;

			// Is this a file post?
			Entry entry = post as Entry;
			if (entry == null)
				return true;

			// Only check comments
			if (entry.FileGalleryPostType != FileGalleryPostType.Comment)
				return true;

			// Nothing else is exempt
			return false;
		}


		/// <summary>
		/// Responsible for determining the number of spam-points to assign to this post
		/// </summary>
		/// <param name="entry"></param>
		/// <param name="e"></param>
		/// <returns></returns>
		public virtual int CalculateSpamScore(Entry entry, CSPostEventArgs e)
		{
			return 0;
		}
	}
}
