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

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

using System.Collections;

namespace CommunityServer.Controls {

    public class ActiveUsers : Label {

        protected override void CreateChildControls() {
            IList list = null;
            Label label = new Label();
            
            Controls.Add(label);

            if (this.GuestMode) {

                int guestCount = UsersOnline.Guests().Count;
                label.Text = string.Format(ResourceManager.GetString("ViewThreads_ActiveGuests"), guestCount);

            } else {

                label.Text = ResourceManager.GetString("ViewThreads_ActiveUsers");
                list = UsersOnline.Members(); //Users.GetUsersOnline(CSContext.Current.SiteSettings.UserOnlineTimeWindow);
            
                for (int i = 0; i < list.Count; i++) {

                    UserOnline userOnline = list[i] as UserOnline;
                    if(userOnline != null)
                    {
                        HtmlAnchor anchor = new HtmlAnchor();
                        anchor.InnerText = userOnline.User.DisplayName;
                        anchor.HRef = Globals.GetSiteUrls().UserProfile( userOnline.UserName );

                        Controls.Add(anchor);

                        Controls.Add(new LiteralControl(", "));
                    }
                }

                // Remove the last comma
                Controls.RemoveAt( Controls.Count - 1 );
            }
        }


		[
		System.ComponentModel.DefaultValue( false ),
		]
		public virtual Boolean GuestMode {
			get {
				Object state = ViewState["GuestMode"];
				if ( state != null ) {
					return (Boolean)state;
				}
				return false;
			}
			set {
				ViewState["GuestMode"] = value;
			}
		}

	}
}


