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

using System;
using System.ComponentModel;
using System.Web.UI;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{
	/// <summary>
	/// Summary description for BlogRatePost.
	/// </summary>
	public class GalleryRatePost : RatingControl
	{
		public event EventHandler RatingsChanged;
		private CSContext cSContext;

		#region Public Properties

		[DefaultValue( null )]
		public virtual string ApplicationKey
		{
			get
			{
				Object state = ViewState["ApplicationKey"];
				if(state != null)
					return (string)state;
				return null;
			}
			set { ViewState["ApplicationKey"] = value; }
		}

		[DefaultValue( -1 )]
		public virtual int PictureID
		{
			get
			{
				Object state = ViewState["PictureID"];
				if(state != null)
					return (int)state;
				return -1;
			}
			set { ViewState["PictureID"] = value; }
		}

		private GalleryPost galleryPost;
		public GalleryPost Post
		{
			get
			{
				if(galleryPost == null)
					galleryPost = GalleryPosts.GetPicture(PictureID);
				return galleryPost;
			}
			set { galleryPost = value; }
		}

		#endregion

		#region RatingControl Implementation

		private double _currentRating = -1;
		public override double CurrentRating
		{
			get
			{
				if (_currentRating == -1)
					_currentRating = this.Post.RatingAverage;

				return _currentRating;
			}
			set
			{
				_currentRating = value;
			}
		}

		private int _currentVotes = -1;
		public override int CurrentVotes
		{
			get
			{
				if (_currentVotes == -1)
					_currentVotes = this.Post.TotalRatings;

				return _currentVotes;
			}
			set
			{
				_currentVotes = value;
			}
		}

		private string _viewDetailsScript = String.Empty;
		public override string ViewDetailsScript
		{
			get
			{
				return _viewDetailsScript;
			}
			set
			{
				_viewDetailsScript = value;
			}
		}

		public override bool UserCanRate 
		{ 
			get
			{
				if (cSContext.User.IsAnonymous)
					return false;

				GalleryPermission permission = Post.Section.ResolvePermission( cSContext.User ) as GalleryPermission;
				if(!permission.Vote)
					return false;

				return true;
			}
		}

		public override void Rate(double value)
		{
			int currentRating = Ratings.GetRating(RatingType.Thread, this.Post.ThreadID, cSContext.User.UserID);

			Rating rating = new Rating();
			rating.User = cSContext.User;
			rating.Value = (int) value;
			rating.RatingType = RatingType.Thread;
			rating.ItemID = this.Post.ThreadID;
			Ratings.Rate(rating);

			if (currentRating == -1)
			{
				_currentVotes = this.Post.TotalRatings + 1;
				_currentRating = (this.Post.RatingSum + value) / (this.Post.TotalRatings + 1);
			}
			else
				_currentRating = ((this.Post.RatingSum - currentRating) + value) / this.Post.TotalRatings;

			OnRatingsChange(new EventArgs());
		}

		#endregion

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			cSContext = CSContext.Current;
			ApplicationKey = cSContext.ApplicationKey;
			PictureID = cSContext.PostID;
		}

		protected override void Render(HtmlTextWriter writer)
		{
			if(this.PictureID != -1)
			{
				base.Render(writer) ;
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			if(this.PictureID != -1)
			{
				base.OnPreRender(e) ;
			}
		}

		protected void OnRatingsChange(EventArgs e)
		{
			if(RatingsChanged != null)
				RatingsChanged(this,e);
		}
	}
}
