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

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

using CommunityServer.Components;
using CommunityServer.Controls;

namespace CommunityServer.Discussions.Controls 
{
    // *********************************************************************
    //  WhoIsOnlineView
    //
    /// <summary>
    /// This server control is used to display users that are currently 
    /// browsing the forums.
    /// </summary>
    /// 
    // ********************************************************************/
    public class WhoIsOnlineView : TemplatedWebControl
    {
        #region Child Controls
        
        Label forumName;
        Label forumDescription;
        Repeater usersRepeater;
        Repeater guestsRepeater;
        Literal userCount;
        Literal guestUsers;
        
        CSContext csContext = CSContext.Current;
        
        #endregion

        protected override void OnInit(EventArgs e) 
        {
            if (SkinName == null)                
                ExternalSkinFileName = "View-WhoIsOnline.ascx";
            else 
                ExternalSkinFileName = SkinName;
			
            base.OnInit(e);
        }
		
        protected override void OnLoad(EventArgs e) 
        {
            UsersOnline.SetLocation("Who Is Online");

           DataBind();

            base.OnLoad(e);
        }

        #region DataBind

        public override void DataBind() 
        {
            base.DataBind();
            
            // Display title
            //
            if (forumName != null)
                forumName.Text = ResourceManager.GetString("WhoIsOnlineView_Title");

            if (forumDescription != null)
                forumDescription.Text = ResourceManager.GetString("WhoIsOnlineView_Description");

            // Get stats
            //
            SiteStatistics siteStats = csContext.Statistics;         
            ArrayList users = UsersOnline.Members();// Users.GetUsersOnline( csContext.SiteSettings.UserOnlineTimeWindow );
            ArrayList guests = UsersOnline.Guests();// Users.GetGuestsOnline( csContext.SiteSettings.AnonymousUserOnlineTimeWindow );

            users.Sort(new OnlineUserComparer());
            guests.Sort(new OnlineUserComparer());
            
            // Display members
            //
            if (usersRepeater != null)
            {
                usersRepeater.DataSource = users;
                usersRepeater.DataBind();
            }
   
            // Display guests
            //
            if (guestsRepeater != null)
            {
                guestsRepeater.DataSource = guests;
                guestsRepeater.DataBind();
            }

            // Display counters
            //
            if (null != userCount)
                userCount.Text = String.Format(CommunityServer.Components.ResourceManager.GetString("WhoIsOnlineView_UsersOnlineCount"), users.Count.ToString("n0"), siteStats.TotalUsers.ToString("n0")); 

            if (null != guestUsers)
                guestUsers.Text = String.Format(CommunityServer.Components.ResourceManager.GetString("WhoIsOnlineView_GuestUsers"), guests.Count.ToString("n0"));
        }

        private class OnlineUserComparer : IComparer
        {
            #region IComparer Members

            public int Compare(object x, object y)
            {
                UserOnline ux = x as UserOnline;
                UserOnline uy = y as UserOnline;

                return uy.LastActivity.CompareTo(ux.LastActivity);
            }

            #endregion
        }
		
        #endregion		
		
        #region Skin
                
        protected override void AttachChildControls() 
        {
            forumName = (Label) FindControl("ForumName");
            forumDescription = (Label) FindControl("ForumDescription");
            usersRepeater = (Repeater) FindControl("MembersOnlineRepeater");
            guestsRepeater = (Repeater) FindControl("GuestsOnlineRepeater");
            userCount = (Literal) FindControl("UsersOnlineCount");
            guestUsers = (Literal) FindControl("GuestUsers");
        }
		        
        #endregion
    }
}