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

using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CommunityServer.Components;
using CommunityServer.Blogs.Components;
using CommunityServer.Files.Components;
using CommunityServer.Galleries.Components;
using CommunityServer.Discussions.Components;
using CP = CommunityServer.ControlPanel.Components;
using CommunityServer.ControlPanel.Controls;

namespace CommunityServer.ControlPanel
{
	/// <summary>
	/// Summary description for Setup.
	/// </summary>
	public class Setup : System.Web.UI.Page
	{
		#region Child Controls
		
		protected Literal Version;
		protected Repeater Announcements;
		protected Control BlogAdministrationArea, PhotoAdministrationArea, FileAdministrationArea, ForumAdministrationArea, MembershipArea, ReaderAdministrationArea;
		protected Literal BlogAdministrationDescription, PhotoAdministrationDescription, FileAdministrationDescription, ForumsAdministrationDescription, MembershipDescription, ReaderAdministrationDescription;
		protected HyperLink NewVersionAvailable;

		#endregion

		CSContext csContext = null;
		User currentUser = null;

		override protected void OnInit(EventArgs e)
		{
			this.Response.Cache.SetCacheability(HttpCacheability.NoCache);
			Authorize();	
			this.Load += new System.EventHandler(this.Page_Load);
			base.OnInit(e);
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			csContext = CSContext.Current;
			currentUser = csContext.User;
			if ( !Page.IsPostBack ) 
			{
				this.DataBind();
			}
		}

		protected virtual void Authorize()
		{
			if(!CSContext.Current.IsAuthenticated)
				Response.Redirect(Globals.GetSiteUrls().Login);
			UsersOnline.PrivateLocation();
		}

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

			this.Version.Text = string.Format(CP.ResourceManager.GetString("CP_Dashboard_Version_Description"), SiteStatistics.CommunityServerVersion.ToString()); 
			
			if (CommunityServer.Configuration.CSConfiguration.GetConfig().EnableLatestVersionCheck && SiteStatistics.NewVersionAvailable(SiteStatistics.MostRecentVersion))
			{
				this.NewVersionAvailable.Visible = true;
				this.NewVersionAvailable.Text = CP.ResourceManager.GetString("CP_Dashboard_Version_NewVersionAvailable");
				this.NewVersionAvailable.NavigateUrl = "http://communityserver.org/";
			}
			else
				this.NewVersionAvailable.Visible = false;

			this.Announcements.DataSource = CommunityServer.ControlPanel.Components.AnnouncementsFeed.GetAnnouncements();
			this.Announcements.DataBind();

			#region Blog Administration

			if (ApplicationSet.Applications[ApplicationType.Weblog].Enabled && currentUser.IsBlogAdministrator)
			{
				BlogAdministrationArea.Visible = true;

				int postCount = 0;
				int commentCount = 0;
				ArrayList blogs = CommunityServer.Blogs.Components.Weblogs.GetWeblogs(true, true, false);
				int blogCount = blogs.Count;
				foreach (Weblog blog in blogs)
				{
					postCount += blog.PostCount;
					commentCount += blog.CommentCount;
				}

				BlogAdministrationDescription.Text = string.Format(CP.ResourceManager.GetString("CP_Dashboard_BlogAdministration_Description"), postCount, commentCount, blogCount);
			}
			else
				BlogAdministrationArea.Visible = false;

			#endregion

			#region Photo Administration

			if (ApplicationSet.Applications[ApplicationType.Gallery].Enabled && currentUser.IsGalleryAdministrator)
			{
				PhotoAdministrationArea.Visible = true;

				int postCount = 0;
				int commentCount = 0;
				ArrayList galleries = CommunityServer.Galleries.Galleries.GetGalleries(csContext.User.UserID, true, true, false);
				int galleryCount = galleries.Count;
				foreach (Gallery g in galleries)
				{
					postCount += g.PostCount;
					commentCount += g.CommentCount;
				}

				PhotoAdministrationDescription.Text = string.Format(CP.ResourceManager.GetString("CP_Dashboard_PhotoAdministration_Description"), postCount, commentCount, galleryCount);
			}
			else
				PhotoAdministrationArea.Visible = false;

			#endregion

			#region File Administration

			if (ApplicationSet.Applications[ApplicationType.FileGallery].Enabled && currentUser.IsFileAdministrator)
			{
				FileAdministrationArea.Visible = true;

				int postCount = 0;
				int commentCount = 0;
				Hashtable folders = CommunityServer.Files.Components.Folders.GetFolders(csContext, csContext.UserID, true, true, false, true);
				int folderCount = folders.Count;
				foreach (Folder f in folders.Values)
				{
					postCount += f.TotalThreads;
					commentCount += f.TotalPosts - f.TotalThreads;
				}

				FileAdministrationDescription.Text = string.Format(CP.ResourceManager.GetString("CP_Dashboard_FileAdministration_Description"), postCount, commentCount, folderCount);
			}
			else
				FileAdministrationArea.Visible = false;

			#endregion

			#region Forums Administration

			if (ApplicationSet.Applications[ApplicationType.Forum].Enabled && currentUser.IsForumAdministrator)
			{
				ForumAdministrationArea.Visible = true;

				int threadCount = 0;
				ArrayList forums = CommunityServer.Discussions.Components.Forums.GetForums(true, true, false, false);
				int forumCount = forums.Count - 1; // -1 is to remove private forum
				foreach (Forum f in forums)
				{
					threadCount += Math.Max(0, f.TotalThreads); // by default, total threads = -1
				}

				ForumsAdministrationDescription.Text = string.Format(CP.ResourceManager.GetString("CP_Dashboard_ForumsAdministration_Description"), threadCount, forumCount);
			}
			else
				ForumAdministrationArea.Visible = false;

			#endregion

			#region Membership

			if (currentUser.IsMembershipAdministrator)
			{
				MembershipArea.Visible = true;

				UserQuery query = new UserQuery();
				query.IncludeHiddenUsers = true;
				query.Status = UserAccountStatus.ApprovalPending;

				MembershipDescription.Text = string.Format(CP.ResourceManager.GetString("CP_Dashboard_Membership_Description"), Users.UserCount(query,true).ToString());
			}
			else
				MembershipArea.Visible = false;

			#endregion

			#region Reader Administration

			if (ApplicationSet.Applications[ApplicationType.FeedReader].Enabled && currentUser.IsReaderAdministrator)
			{
				ReaderAdministrationArea.Visible = true;

				int feedCount = CommunityServer.Reader.Components.Feeds.GetAllFeeds(0).Count;
				int userCount = CommunityServer.Reader.Components.Feeds.GetUsers().Count;

				ReaderAdministrationDescription.Text = string.Format(CP.ResourceManager.GetString("CP_Dashboard_ReaderAdministration_Description"), userCount, feedCount);
			}
			else
				ReaderAdministrationArea.Visible = false;

			#endregion
		}
	}
}
