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

namespace CommunityServer.Files.Controls
{
	/// <summary>
	/// Summary description for InlineTagEditor.
	/// </summary>
	public class InlineTagEditor : CommunityServer.Controls.InlineTagEditor
	{
		CSContext csContext = CSContext.Current;

		public override void SaveTags(int postID, string[] tags)
		{
			if (this.IsEditable(postID))
			{
				Entry post = Entries.GetEntry(postID, false);
				post.Categories = tags;
				Entries.Update(post);
				PostCategories.ExpireCategoryCache(this.Folder.SectionID);
			}
		}

		public override string GetUrl(string tag)
		{
			return FileGalleryUrls.Instance().TagsBrowser(this.Folder.ApplicationKey, new string[] { tag });
		}

		public override bool IsEditable (int postID)
		{
			return IsEditable();
		}

		public override bool IsEditable()
		{
			return Permissions.ValidatePermissions(this.Folder, Permission.Administer, csContext.User);
		}

		private Folder folder = null;
		public virtual Folder Folder
		{
			get
			{
				if (folder == null)
				{
					folder = Folders.GetFolder(csContext.ApplicationKey);
					Permissions.AccessCheck(folder,Permission.View,csContext.User);
				}

				return folder;
			}
		}

		string[] _availableTags = null;
		public override string[] AvailableTags 
		{ 
			get
			{
				if (_availableTags == null)
				{
					ArrayList allTags = PostCategories.GetCategories(this.Folder.SectionID, true, false);
					ArrayList availableTags = new ArrayList();
					foreach (PostCategory pc in allTags)
					{
						availableTags.Add(pc.Name);
					}

					_availableTags = (string[]) availableTags.ToArray(typeof(string));
				}

				return _availableTags;
			}
		}
	}
}
