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

using System;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Files.Components;
using System.Web.UI;
using System.Collections;

namespace CommunityServer.Files.Controls
{

	public class EntryDetails : FileGalleryTemplatedWebControl
	{
		CSContext csContext = CSContext.Current;

		#region Child Controls

		private IText Name;
		private IText Description;
		private IText PostAuthor;
		private IText ViewCount;
		private IText DownloadCount;
		private IText DateAdded;
		private HyperLink Download, Edit, Delete;
		private IText FileSize;
		private CommunityServer.Controls.FavoritePopupMenu FavoritePopupMenu;
		private CommunityServer.Controls.ContactPopupMenu ContactPopupMenu;
		private CommunityServer.Controls.ITrackedThread SubscribeButton;
		private InlineTagEditorPanel InlineTagEditorPanel = null;

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			Name = FindText( "Name" );
			Description = FindText( "Description" );
			PostAuthor = FindText("PostAuthor");
			ViewCount = FindText( "ViewCount" );
			DownloadCount = FindText( "DownloadCount" );
			DateAdded = FindText( "DateAdded" );
			Download = (HyperLink)FindControl( "Download" );
			Edit = (HyperLink)FindControl( "Edit" );
			Delete = (HyperLink)FindControl( "Delete" );
			FileSize = FindText( "FileSize" );
			FavoritePopupMenu = (CommunityServer.Controls.FavoritePopupMenu) FindControl( "FavoritePopupMenu" );
			ContactPopupMenu = (CommunityServer.Controls.ContactPopupMenu) FindControl("ContactPopupMenu");
			SubscribeButton =  FindControl("SubscribeButton") as ITrackedThread;
			InlineTagEditorPanel = FindControl("InlineTagEditorPanel") as InlineTagEditorPanel;
		}

		#endregion

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			this.DataBind();
		}

		public override void DataBind()
		{
			base.DataBind();

			Views.AddWebCount(CurrentEntry.PostID);
			User author = Users.GetUser(CurrentEntry.AuthorID, false);

			// Main information
			if (Name != null)
				Name.Text = CurrentEntry.Subject;

			if (Description != null)
				Description.Text = CurrentEntry.RenderedBody(PostTarget.Web);

			// Statistic properties
			if(PostAuthor != null)
			{
				if(author.IsAnonymous)
					PostAuthor.Text = CurrentEntry.Username;
				else
					PostAuthor.Text = "<a href='" + Globals.GetSiteUrls().UserProfile(author.UserID) + "'>" + author.DisplayName + "</a>";
			}

			if (ViewCount != null)
				ViewCount.Text = CurrentEntry.Views.ToString();

			if (DownloadCount != null)
				DownloadCount.Text = CurrentEntry.Downloads.ToString();

			if (SubscribeButton != null)
			{
				SubscribeButton.IsTracked = CurrentEntry.GetIsTracked(CurrentUser.UserID);
				SubscribeButton.ThreadID = CurrentEntry.ThreadID;
			}

			// Date added
			User user = csContext.User;
			if(!user.IsAnonymous)
				DateAdded.Text = user.GetTimezone(CurrentEntry.PostDate).ToString(user.Profile.DateFormat);
			else
				DateAdded.Text = CurrentEntry.PostDate.ToString(csContext.SiteSettings.DateFormat);

			// Download stuff
			
			if (Download != null)
			{
				Download.Visible = true;
				Download.NavigateUrl = FileGalleryUrls.Instance().Download(CurrentEntry);
			}

			if (Edit != null)
			{
				if (Permissions.ValidatePermissions(Folders.GetFolder(csContext.ApplicationKey), Permission.Edit, user, CurrentEntry))
				{
					Edit.Visible = true;
					Edit.NavigateUrl = FileGalleryUrls.Instance().EntryAdmin(csContext.ApplicationKey, CurrentEntry.PostID);
				}
				else
				{
					Edit.Visible = false;
				}
			}

			if (Delete != null)
			{
				if (Permissions.ValidatePermissions(Folders.GetFolder(csContext.ApplicationKey), Permission.Delete, user, CurrentEntry))
				{
					Delete.Visible = true;
					Delete.NavigateUrl = FileGalleryUrls.Instance().DeleteEntry(csContext.ApplicationKey, CurrentEntry.PostID);
				}
				else
				{
					Delete.Visible = false;
				}
			}

			if (FavoritePopupMenu != null)
			{
				FavoritePopupMenu.UserID = CurrentEntry.AuthorID;
				FavoritePopupMenu.SectionID = CurrentEntry.SectionID;
				FavoritePopupMenu.PostID = CurrentEntry.PostID;
				FavoritePopupMenu.ApplicationType = ApplicationType.FileGallery;
			}

			if (ContactPopupMenu != null)
			{
				if (author.IsAnonymous)
					ContactPopupMenu.Visible = false;
				else
					ContactPopupMenu.UserID = CurrentEntry.AuthorID;
			}

			if (InlineTagEditorPanel != null)
			{
				InlineTagEditorPanel.PostID = CurrentEntry.PostID;

				ArrayList categories = Entries.GetEntryCategories(CurrentEntry);
				string[] tags = new string[categories.Count];
				for (int i = 0; i < categories.Count; i++)
					tags[i] = ((PostCategory) categories[i]).Name;

				InlineTagEditorPanel.SelectedTags = tags;
			}

			// File size
			if (FileSize != null)
			{
				if (CurrentEntry.FileSize >= 0)
				{
					if(CurrentEntry.FileSize < 1024)
						FileSize.Text = CurrentEntry.FileSize.ToString() + " bytes";
					else if(CurrentEntry.FileSize < 1024*1024)
						FileSize.Text = ((double)CurrentEntry.FileSize / 1024).ToString("0.#") + "kB";
					else
						FileSize.Text = ((double)CurrentEntry.FileSize / (1024*1024)).ToString("0.#") + "MB";
				}
				else
					FileSize.Text = "Unknown";
			}

		}
	}
}
