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

using System;
using CommunityServer.Controls;
using CommunityServer.Components;
using System.Collections;
using TWC = Telligent.Web.UI;

namespace CommunityServer.Controls
{
	public class FavoritePopupMenu : ServerPopulatedPopupMenu
	{
		CSContext csContext;

		#region Event Handlers

		protected override void OnInit(System.EventArgs e)
		{
			base.OnInit(e);

			AjaxManager.Register(this,"FavoriteAJAX");
			csContext = CSContext.Current;
			this.CacheMenuByParameterString = false;
			this.ResourceName = "FavoritePopupMenu_Text";
			// this.MenuParameterJavaScript = "this";

			if (csContext.User.IsAnonymous)
				this.Visible = false;
		}

		protected override void OnPreRender(System.EventArgs e)
		{
			this.ParameterString = this.UserID.ToString() + ":" + this.PostID.ToString() + ":" + this.SectionID.ToString();

			if (this.ShowBackgroundImageIcon)
				this.Style["background-image"] = "url(" + Globals.GetSkinPath() + "/images/common/favorite.gif)";

			base.OnPreRender(e);
		}

		#endregion

		#region Render Methods

		private string ajaxCommand = "var aid = window.{1}.StartAnimation({1}.LastElement);{1}.LastElement = null;eval('FavoriteAJAX.{0}(\\'{1}\\', \\'{2}\\', new Function(\\'result\\', \\'if (result.error) {{ alert(result.error); }} window.{1}.StopAnimation(' + aid + ');\\'))');";

		protected override void PopulateMenu(string parameterString)
		{
			string[] values = parameterString.Split(':');
			this.UserID = int.Parse(values[0]);
			this.PostID = int.Parse(values[1]);
			this.SectionID = int.Parse(values[2]);

			string onIconUrl = Globals.GetSkinPath() + "/images/common/favorite-on.gif";
			string offIconUrl = Globals.GetSkinPath() + "/images/common/favorite-off.gif";

			CSApplicationData app = ApplicationSet.Applications[this.ApplicationType];

			TWC.PopupMenuItem item;
			if (this.UserID > -1)
			{
				User user = Users.GetUser(this.UserID, false);
				IList users = Favorites.GetUsers(csContext.User.UserID);

				bool isFavorite = false;
				foreach (User u in users)
				{
					if (u.UserID == this.UserID)
					{
						isFavorite = true;
						break;
					}
				}

				item = new TWC.PopupMenuItem();
				if (isFavorite)
				{
					item.IconUrl = onIconUrl;
					item.Text = String.Format(ResourceManager.GetString("FavoritePopupMenu_User_Remove"), user.DisplayName);
					item.OnClickClientFunction = "new Function('" + TWC.WebControlUtils.EncodeForJavaScript(string.Format(ajaxCommand, "RemoveFavoriteUser", this.ClientID, this.UserID)) + "')";
				}
				else
				{
					item.IconUrl = offIconUrl;
					item.Text = String.Format(ResourceManager.GetString("FavoritePopupMenu_User_Add"), user.DisplayName);
					item.OnClickClientFunction = "new Function('" + TWC.WebControlUtils.EncodeForJavaScript(string.Format(ajaxCommand, "AddFavoriteUser", this.ClientID, this.UserID)) + "')";
				}

				menu.MenuItems.Add(item);
			}

			if (this.PostID > -1)
			{
				IList posts = Favorites.GetPosts(csContext.User.UserID);
				
				bool isFavorite = false;
				foreach (Post p in posts)
				{
					if (p.PostID == this.PostID)
					{
						isFavorite = true;
						break;
					}
				}
			
				item = new TWC.PopupMenuItem();
				if (isFavorite)
				{
					item.IconUrl = onIconUrl;
					item.Text = app.RemoveFavoritePostText;
					item.OnClickClientFunction = "new Function('" + TWC.WebControlUtils.EncodeForJavaScript(string.Format(ajaxCommand, "RemoveFavoritePost", this.ClientID, this.PostID)) + "')";
				}
				else
				{
					item.IconUrl = offIconUrl;
					item.Text = app.AddFavoritePostText;
					item.OnClickClientFunction = "new Function('" + TWC.WebControlUtils.EncodeForJavaScript(string.Format(ajaxCommand, "AddFavoritePost", this.ClientID, this.PostID)) + "')";
				}

				menu.MenuItems.Add(item);
			}

			if (this.SectionID > -1)
			{
				IList sections = Favorites.GetSections(csContext.User.UserID);

				bool isFavorite = false;
				foreach (Section s in sections)
				{
					if (s.SectionID == this.SectionID)
					{
						isFavorite = true;
						break;
					}
				}

				item = new TWC.PopupMenuItem();
				if (isFavorite)
				{
					item.IconUrl = onIconUrl;
					item.Text = app.RemoveFavoriteSectionText;
					item.OnClickClientFunction = "new Function('" + TWC.WebControlUtils.EncodeForJavaScript(string.Format(ajaxCommand, "RemoveFavoriteSection", this.ClientID, this.SectionID)) + "')";
				}
				else
				{
					item.IconUrl = offIconUrl;
					item.Text = app.AddFavoriteSectionText;
					item.OnClickClientFunction = "new Function('" + TWC.WebControlUtils.EncodeForJavaScript(string.Format(ajaxCommand, "AddFavoriteSection", this.ClientID, this.SectionID)) + "')";
				}

				menu.MenuItems.Add(item);
			}
		}

		#endregion

		#region Properties
		
		public virtual bool ShowBackgroundImageIcon
		{
			get
			{
				object state = ViewState["ShowBackgroundImageIcon"];
				if (state == null)
					return true;
				else
					return (bool) state;
			}
			set
			{
				ViewState["ShowBackgroundImageIcon"] = value;
			}
		}

		public virtual int UserID
		{
			get
			{
				object state = ViewState["UserID"];
				if (state == null)
					return csContext.UserID;
				else
					return (int) state;
			}
			set
			{
				ViewState["UserID"] = value;
			}
		}

		public virtual int PostID
		{
			get
			{
				object state = ViewState["PostID"];
				if (state == null)
					return csContext.PostID;
				else
					return (int) state;
			}
			set
			{
				ViewState["PostID"] = value;
			}
		}

		public virtual int SectionID
		{
			get
			{
				object state = ViewState["SectionID"];
				if (state == null)
					return csContext.SectionID;
				else
					return (int) state;
			}
			set
			{
				ViewState["SectionID"] = value;
			}
		}

		public virtual ApplicationType ApplicationType
		{
			get
			{
				object state = ViewState["ApplicationType"];
				if (state == null)
					return ApplicationType.Unknown;
				else
					return (ApplicationType) state;
			}
			set
			{
				ViewState["ApplicationType"] = value;
			}
		}

		#endregion

		#region AJAX Methods

		[AjaxMethod(IncludeControlValuesWithCallBack=true)]
		public virtual void AddFavoriteUser(int userID)
		{
			Favorite f = new Favorite();
			f.ApplicationType = this.ApplicationType;
			f.FavoriteType = FavoriteType.User;
			f.ItemID = userID;
			f.SettingsID = csContext.SiteSettings.SettingsID;
			f.UserID = csContext.User.UserID;

			Favorites.AddFavorite(f);
		}

		[AjaxMethod(IncludeControlValuesWithCallBack=true)]
		public virtual void AddFavoritePost(int postID)
		{
			Favorite f = new Favorite();
			f.ApplicationType = this.ApplicationType;
			f.FavoriteType = FavoriteType.Post;
			f.ItemID = postID;
			f.SettingsID = csContext.SiteSettings.SettingsID;
			f.UserID = csContext.User.UserID;

			Favorites.AddFavorite(f);
		}

		[AjaxMethod(IncludeControlValuesWithCallBack=true)]
		public virtual void AddFavoriteSection(int sectionID)
		{
			Favorite f = new Favorite();
			f.ApplicationType = this.ApplicationType;
			f.FavoriteType = FavoriteType.Section;
			f.ItemID = sectionID;
			f.SettingsID = csContext.SiteSettings.SettingsID;
			f.UserID = csContext.User.UserID;

			Favorites.AddFavorite(f);
		}

		[AjaxMethod(IncludeControlValuesWithCallBack=true)]
		public virtual void RemoveFavoriteUser(int userID)
		{
			Favorites.DeleteFavorite(FavoriteType.User, csContext.User.UserID, userID);
		}

		[AjaxMethod(IncludeControlValuesWithCallBack=true)]
		public virtual void RemoveFavoritePost(int postID)
		{
			Favorites.DeleteFavorite(FavoriteType.Post, csContext.User.UserID, postID);
		}

		[AjaxMethod(IncludeControlValuesWithCallBack=true)]
		public virtual void RemoveFavoriteSection(int sectionID)
		{
			Favorites.DeleteFavorite(FavoriteType.Section, csContext.User.UserID, sectionID);
		}

		#endregion
	}
}
