//------------------------------------------------------------------------------
// <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 CommunityServer.Components;
using CommunityServer.Controls;

namespace CommunityServer.Discussions.Controls 
{
    // *********************************************************************
    //  ModerationStats
    //
    /// <summary>
    /// This server control provides statistics about moderators and moderator
    /// actions within the forums.
    /// </summary>
    // ********************************************************************/ 
    public class ModerationStats : TemplatedWebControl
    {
        #region Child Controls

        Label TotalModerators;
        Label TotalModeratedPosts;
        Label AverageModerationPerUser;
        Repeater TopModerators;
        Repeater ModerationAction;

        SiteStatistics siteStats;
        CSContext csContext;

        #endregion

        protected override void OnInit(EventArgs e) 
        {
            csContext = CSContext.Current;

            if (SkinName == null)                
                ExternalSkinFileName = "Moderation/Skin-Statistics.ascx";
            else 
                ExternalSkinFileName = SkinName;
			
            base.OnInit(e);
        }
		
        protected override void OnLoad(EventArgs e) 
        {
            if (!Page.IsPostBack)
                DataBind();

            base.OnLoad(e);
        }

        #region DataBind

        public override void DataBind() 
        {
            base.DataBind();
            
            // Get the statistics
            //
            siteStats = csContext.Statistics;

            if (siteStats == null)
                return;

            // Total Moderators
            //
            if (null != TotalModerators)
                TotalModerators.Text = siteStats.TotalModerators.ToString("n0");

            // Total Moderated Posts
            //
            if (null != TotalModeratedPosts)
                TotalModeratedPosts.Text = siteStats.TotalModeratedPosts.ToString("n0");
            
            // Average Moderated Posts
            //
            if (null != AverageModerationPerUser)
                AverageModerationPerUser.Text = siteStats.AverageModeratedPostsPerModerator.ToString("n0");

            // Moderator actions
            //
            if (null != ModerationAction) 
            {
				if (siteStats.ModerationActions.Count <= 0)
				{
					ModerationAction.Visible = false;
				}
				else
				{
					ModerationAction.DataSource = siteStats.ModerationActions;
					ModerationAction.DataBind();
				}
            }

            // Most active moderator
            //
            if (null != TopModerators) 
            {
				if (siteStats.ActiveModerators.Count <= 0)
				{
					TopModerators.Visible = false;
				}
				else
				{
					TopModerators.DataSource = siteStats.ActiveModerators;
					TopModerators.DataBind();
				}
            }
        }

        #endregion
		
        #region Skin
                
        protected override void AttachChildControls() 
        {
            // Bind controls
            //
            TopModerators = (Repeater) FindControl("TopModerators");
            ModerationAction = (Repeater) FindControl("ModerationAction");
            TotalModerators = (Label) FindControl("TotalModerators");
            TotalModeratedPosts = (Label) FindControl("TotalModeratedPosts");
            AverageModerationPerUser = (Label) FindControl("AverageModerationPerUser");
			
            //InitializeChildControls();
        }
        
        /* Not used
        private void InitializeChildControls() 
        {
		
        }*/
		        
        #endregion
    }
}
