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

using System;
using System.Collections;
using System.Web.UI;
using CommunityServer.Components;

namespace CommunityServer.Controls
{
	public class FavoriteLinkButton : Control
	{
	    CSContext csContext;
		string ajaxCommand;

		#region Event Handlers

		protected override void OnInit(EventArgs e)
		{
			AjaxManager.Register(this,"FavoriteLinkButtonAJAX");
			csContext = CSContext.Current;

			// 0 - method, 1 - parameter, 2- clientID
			ajaxCommand = "this.style.cursor='progress';this.onclick=new Function('return false;');FavoriteLinkButtonAJAX.{0}('{2}',{1},new Function('res','var link=document.getElementById(\\'{2}\\');link.style.cursor=\\'\\';if(res.error) {{ alert(res.error); }} else {{  link.innerHTML=res.value[0];link.onclick=new Function(res.value[1]); }}')); return false;";

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

		#endregion

		#region Render Method

		protected override void Render(HtmlTextWriter writer)
		{
			string text;
			string onclick;

			CSApplicationData app = ApplicationSet.Applications[this.ApplicationType];
			bool isFavorite = false;

			switch (this.FavoriteType)
			{
				case FavoriteType.Post:
					
					#region Define Post Settings

			        IList posts = Favorites.GetPosts(csContext.User.UserID);
				
					isFavorite = false;
					foreach (Post p in posts)
					{
						if (p.PostID == this.PostID)
						{
							isFavorite = true;
							break;
						}
					}
			
					if (isFavorite)
					{
						text = String.Format(ResourceManager.GetString("FavoritePopupMenu_Post_Remove"), app.PostName.ToLower());
						onclick = string.Format(ajaxCommand, "RemoveFavoritePost", this.PostID, this.ClientID);
					}
					else
					{
						text = String.Format(ResourceManager.GetString("FavoritePopupMenu_Post_Add"), app.PostName.ToLower());
						onclick = string.Format(ajaxCommand, "AddFavoritePost", this.PostID, this.ClientID);
					}

					#endregion

					break;

				case FavoriteType.Section:

					#region Define Section Settings

					IList sections = Favorites.GetSections(csContext.User.UserID);

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

					if (isFavorite)
					{
						text = String.Format(ResourceManager.GetString("FavoritePopupMenu_Section_Remove"), app.SectionName.ToLower());
						onclick = string.Format(ajaxCommand, "RemoveFavoriteSection", this.SectionID, this.ClientID);
					}
					else
					{
						text = String.Format(ResourceManager.GetString("FavoritePopupMenu_Section_Add"), app.SectionName.ToLower());
						onclick = string.Format(ajaxCommand, "AddFavoriteSection", this.SectionID, this.ClientID);
					}

					#endregion

					break;

				case FavoriteType.User:

					#region Define User Settings

					User user = Users.GetUser(this.UserID, false);
					IList users = Favorites.GetUsers(csContext.User.UserID);

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

					if (isFavorite)
					{
						text = String.Format(ResourceManager.GetString("FavoritePopupMenu_User_Remove"), user.DisplayName);
						onclick = string.Format(ajaxCommand, "RemoveFavoriteUser", this.UserID, this.ClientID);
					}
					else
					{
						text = String.Format(ResourceManager.GetString("FavoritePopupMenu_User_Add"), user.DisplayName);
						onclick = string.Format(ajaxCommand, "AddFavoriteUser", this.UserID, this.ClientID);
					}

					#endregion

					break;

				default:
					return;
			}

			writer.Write("<a href=\"#\" cssClass=\"");
			writer.Write(this.CssClass);
			writer.Write("\" id=\"");
			writer.Write(this.ClientID);
			writer.Write("\" onclick=\"");
			writer.Write(onclick);
			writer.Write("\">");
			writer.Write(text);
			writer.Write("</a>");
		}		

		#endregion

		#region Properties
		
		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;
			}
		}

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

		public virtual string CssClass
		{
			get
			{
				string state = (string) ViewState["CssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["CssClass"] = value;
			}
		}

		#endregion

		#region AJAX Methods

		[AjaxMethod(IncludeControlValuesWithCallBack=true)]
		public virtual string[] 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);

			string[] result = new string[2];
			User user = Users.GetUser(this.UserID, false);

			result[0] = String.Format(ResourceManager.GetString("FavoritePopupMenu_User_Remove"), user.DisplayName);
			result[1] = string.Format(ajaxCommand, "RemoveFavoriteUser", this.UserID, this.ClientID);

			return result;
		}

		[AjaxMethod(IncludeControlValuesWithCallBack=true)]
		public virtual string[] 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);

			CSApplicationData app = ApplicationSet.Applications[this.ApplicationType];
			string[] result = new string[2];

			result[0] = String.Format(ResourceManager.GetString("FavoritePopupMenu_Post_Remove"), app.PostName.ToLower());
			result[1] = string.Format(ajaxCommand, "RemoveFavoritePost", this.PostID, this.ClientID);

			return result;
		}

		[AjaxMethod(IncludeControlValuesWithCallBack=true)]
		public virtual string[] 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);

			CSApplicationData app = ApplicationSet.Applications[this.ApplicationType];
			string[] result = new string[2];

			result[0] = String.Format(ResourceManager.GetString("FavoritePopupMenu_Section_Remove"), app.SectionName.ToLower());
			result[1] = string.Format(ajaxCommand, "RemoveFavoriteSection", this.SectionID, this.ClientID);

			return result;
		}

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

			string[] result = new string[2];
			User user = Users.GetUser(this.UserID, false);

			result[0] = String.Format(ResourceManager.GetString("FavoritePopupMenu_User_Add"), user.DisplayName);
			result[1] = string.Format(ajaxCommand, "AddFavoriteUser", this.UserID, this.ClientID);

			return result;
		}

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

			CSApplicationData app = ApplicationSet.Applications[this.ApplicationType];
			string[] result = new string[2];

			result[0] = String.Format(ResourceManager.GetString("FavoritePopupMenu_Post_Add"), app.PostName.ToLower());
			result[1] = string.Format(ajaxCommand, "AddFavoritePost", this.PostID, this.ClientID);

			return result;
		}

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

			CSApplicationData app = ApplicationSet.Applications[this.ApplicationType];
			string[] result = new string[2];

			result[0] = String.Format(ResourceManager.GetString("FavoritePopupMenu_Section_Add"), app.SectionName.ToLower());
			result[1] = string.Format(ajaxCommand, "AddFavoriteSection", this.SectionID, this.ClientID);

			return result;
		}

		#endregion
	}
}
