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

using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Controls;

namespace CommunityServer.Discussions.Controls 
{
    /// <summary>
    /// This server control is used to display all the members of the current forum.
    /// </summary>
    [
    ParseChildren(true)
    ]
    public class ForumMembersView : TemplatedWebControl
    {
        #region Child Controls

		protected UserSearch UserSearchControl;
		protected Repeater userList;
        protected ForumLinkButtonPager pager;
        protected CurrentPage currentPage;
        protected Label sectionTitle;
        protected Label sectionDescription;

        #endregion

        protected override void OnInit(EventArgs e) 
        {
            if (SkinName == null)                
                ExternalSkinFileName = "View-ForumMembers.ascx";
            else 
                ExternalSkinFileName = SkinName;
			
            base.OnInit(e);
        }
		
        protected override void OnLoad(EventArgs e) 
        {

			base.OnLoad(e);

			if (!this.Page.IsPostBack)
			{
				this.EnsureChildControls();
				CSContext cntx = CSContext.Current;
				UserSearchControl.Visible = cntx.SiteSettings.EnablePublicAdvancedMemberSearch;
				pager.PageSize = cntx.SiteSettings.MembersPerPage;

				// Don't DataBind the first time this page loadss. Only when the UserSearchControl.SearchButton has been clicked.
				if (cntx.QueryString["search"] != null)
					DataBind();
			}
		}


		#region DataBind

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

            PopulateUserList();
        }
		

		private void UserList_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				User user = e.Item.DataItem as User;
				if(user != null)
				{
					// Username
					HyperLink Username = e.Item.FindControl("Username") as HyperLink;
					if(Username != null)
					{
						Username.Text = user.DisplayName;
						Username.NavigateUrl = Globals.GetSiteUrls().UserProfile(user.Username);
					}

					// Location
					Literal Location = e.Item.FindControl("Location") as Literal;
					if(Location != null)
					{
						if (!Globals.IsNullorEmpty(user.Profile.Location))
						{
							Location.Text = user.Profile.Location;
						}
						else
						{
							HtmlContainerControl LocationDiv = e.Item.FindControl("LocationDiv") as HtmlContainerControl;
							if (LocationDiv != null)
								LocationDiv.Visible = false;
							else
								Location.Visible = false;
						}
					}
					
					// Private Messages
					UserImageButtons PrivateMessages = e.Item.FindControl("PrivateMessages") as UserImageButtons;
					if(PrivateMessages != null)
						PrivateMessages.User = user;

					// Home Page
					UserImageButtons HomePage = e.Item.FindControl("HomePage") as UserImageButtons;
					if(HomePage != null)
						HomePage.User = user;

					// Weblog
					UserImageButtons Weblog = e.Item.FindControl("Weblog") as UserImageButtons;
					if(Weblog != null)
						Weblog.User = user;

					// Date Created
					CSContext cntx = CSContext.Current; 
					Literal DateCreated = e.Item.FindControl("DateCreated") as Literal;
					if(DateCreated != null)
						DateCreated.Text = user.DateCreated.ToString(cntx.User.Profile.DateFormat);

					// Date Created
					Literal LastActivity = e.Item.FindControl("LastActivity") as Literal;
					if(LastActivity != null)
						LastActivity.Text = user.LastActivity.ToString(cntx.User.Profile.DateFormat);

					// Posts
					HyperLink PostCount = e.Item.FindControl("PostCount") as HyperLink;
					if(PostCount != null)
					{
						if (user.TotalPosts > 0)
						{
							PostCount.Text = Formatter.FormatNumber(user.TotalPosts);
							PostCount.NavigateUrl = Globals.GetSiteUrls().SearchByUser(user.UserID);
						}
						else
						{
							PostCount.Text = "-";
						}
					}

				}
			}

		}

        #endregion		
		
        #region Skin
        protected override void AttachChildControls() 
        {            
            userList = FindControl("UserList") as Repeater;
            pager = FindControl("Pager") as ForumLinkButtonPager;
            currentPage = FindControl("CurrentPage") as CurrentPage;
			UserSearchControl = FindControl("UserSearchControl") as UserSearch;

            // Set the title and description if they haven't been set by a derived class
            if (sectionTitle == null && sectionDescription == null) 
            {
                sectionTitle = (Label) FindControl("SectionTitle");
                if (sectionTitle != null)
                    sectionTitle.Text = ResourceManager.GetString("CommunityServerMembers_Inline1");

                sectionDescription = (Label) FindControl("SectionDescription");
                if (sectionDescription != null)
                    sectionDescription.Text = ResourceManager.GetString("CommunityServerMembers_Inline2");
            }
			
            InitializeChildControls();
        }

		private void InitializeChildControls() 
        {
            if (pager != null)
                pager.IndexChanged +=new EventHandler(PageIndex_Changed);

			if (userList != null)
				userList.ItemDataBound +=new RepeaterItemEventHandler(UserList_ItemDataBound);

        }
		        
        #endregion

        #region Event Handlers

        private void PageIndex_Changed(Object sender, EventArgs e) 
        {
            DataBind();
        }

		
        #endregion

        #region Helpers

        public virtual void PopulateUserList()
        {
			CSContext context = CSContext.Current;
		
			int pageIndex = 0;
			int pageSize = 999999999;
			if (pager != null)
			{
				pageIndex = pager.PageIndex;
				pageSize = context.SiteSettings.MembersPerPage;
				pager.PageSize = pageSize;
			}

			UserSet userSet = UserSearchControl.GetUsersAndBindControl(pageIndex, pageSize, null);

            if (userList != null)
            {
                userList.DataSource = userSet.Users;
                userList.DataBind();
            }

            if (pager != null)
                pager.TotalRecords = currentPage.TotalRecords = userSet.TotalRecords;

            if (currentPage != null)
            {
                currentPage.TotalPages = pager.CalculateTotalPages();
                currentPage.PageIndex = pager.PageIndex;
            }
        }

        #endregion

	}
}
