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

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

using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls 
{
    // *********************************************************************
    //  ModerationCounters
    //
    /// <summary>
    /// This control renders user moderation counters.
    /// </summary>
    // ********************************************************************/ 	
    public class UserModerationCounters : TemplatedWebControl
    {
        #region Child Controls

        HyperLink ApprovedPostsLink;
        HyperLink EditedPostsLink;
        HyperLink MovedPostsLink;
        HyperLink DeletedPostsLink;
        HyperLink LockedPostLink;
        HyperLink UnlockedPostLink;
        HyperLink MergedPostLink;
        HyperLink SplitedPostLink;
        HyperLink UnApprovedPostLink;
        HyperLink PostIsAnnouncementLink;
        HyperLink PostIsNotAnnoucementLink;

        HyperLink BannedLink;
        HyperLink UnBannedLink;
        HyperLink ModeratedLink;
        HyperLink UnmoderatedLink;
        HyperLink ResetPasswdLink;
        HyperLink ChangePasswdLink;
        HyperLink EditedLink;

        AuditSummary summary = null;
        CSContext csContext = CSContext.Current;
        string linkFormat = "{0} ({1})";

        #endregion

        protected override void OnInit(EventArgs e) 
        {
            if (SkinName == null)                
                ExternalSkinFileName = "Moderation/Skin-UserModerationCounters.ascx";
            else 
                ExternalSkinFileName = SkinName;
			
            base.OnInit(e);
        }
		
        protected override void OnLoad(EventArgs e) 
        {
            if (csContext.UserID <= 0)
                return;

            summary = Audit.GetUserAuditSummary( csContext.UserID );
            if (summary == null)
                return;

            if (!Page.IsPostBack)
                DataBind();

            base.OnLoad(e);
        }

        #region DataBind

        public override void DataBind() 
        {
            base.DataBind();
            
            if (null != ApprovedPostsLink) 
            {
                ApprovedPostsLink.NavigateUrl = null;
                ApprovedPostsLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.ApprovePost ), summary[ModeratorActions.ApprovePost].ToString() );
                if (summary[ModeratorActions.ApprovePost] > 0)
                    ApprovedPostsLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.ApprovePost );
            }

            if (null != EditedPostsLink) 
            {
                EditedPostsLink.NavigateUrl = null;
                EditedPostsLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.EditPost ), summary[ModeratorActions.EditPost].ToString() );
                if (summary[ModeratorActions.EditPost] > 0)
                    EditedPostsLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.EditPost );
            }

            if (null != MovedPostsLink) 
            {
                MovedPostsLink.NavigateUrl = null;
                MovedPostsLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.MovePost ), summary[ModeratorActions.MovePost].ToString() );
                if (summary[ModeratorActions.MovePost] > 0)
                    MovedPostsLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.MovePost );
            }

            if (null != DeletedPostsLink) 
            {
                DeletedPostsLink.NavigateUrl = null;
                DeletedPostsLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.DeletePost ), summary[ModeratorActions.DeletePost].ToString() );
                if (summary[ModeratorActions.DeletePost] > 0)
                    DeletedPostsLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.DeletePost );
            }

            if (null != LockedPostLink) 
            {
                LockedPostLink.NavigateUrl = null;
                LockedPostLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.LockPost ), summary[ModeratorActions.LockPost].ToString() );
                if (summary[ModeratorActions.LockPost] > 0)
                    LockedPostLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.LockPost );
            }

            if (null != UnlockedPostLink) 
            {
                UnlockedPostLink.NavigateUrl = null;
                UnlockedPostLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.UnlockPost ), summary[ModeratorActions.UnlockPost].ToString() );
                if (summary[ModeratorActions.UnlockPost] > 0)
                    UnlockedPostLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.UnlockPost );
            }

            if (null != MergedPostLink) 
            {
                MergedPostLink.NavigateUrl = null;
                MergedPostLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.MergePost ), summary[ModeratorActions.MergePost].ToString() );
                if (summary[ModeratorActions.MergePost] > 0)
                    MergedPostLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.MergePost );
            }

            if (null != SplitedPostLink) 
            {
                SplitedPostLink.NavigateUrl = null;
                SplitedPostLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.SplitPost ), summary[ModeratorActions.SplitPost].ToString() );
                if (summary[ModeratorActions.SplitPost] > 0)
                    SplitedPostLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.SplitPost );
            }

            if (null != UnApprovedPostLink) 
            {
                UnApprovedPostLink.NavigateUrl = null;
                UnApprovedPostLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.UnApprovePost ), summary[ModeratorActions.UnApprovePost].ToString() );
                if (summary[ModeratorActions.UnApprovePost] > 0)
                    UnApprovedPostLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.UnApprovePost );
            }

            if (null != BannedLink) 
            {
                BannedLink.NavigateUrl = null;
                BannedLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.BanUser ), summary[ModeratorActions.BanUser].ToString() );
                if (summary[ModeratorActions.BanUser] > 0)
                    BannedLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.BanUser );
            }

            if (null != UnBannedLink) 
            {
                UnBannedLink.NavigateUrl = null;
                UnBannedLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.UnbanUser ), summary[ModeratorActions.UnbanUser].ToString() );
                if (summary[ModeratorActions.UnbanUser] > 0)
                    UnBannedLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.UnbanUser );
            }

            if (null != ModeratedLink) 
            {
                ModeratedLink.NavigateUrl = null;
                ModeratedLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.ModerateUser ), summary[ModeratorActions.ModerateUser].ToString() );
                if (summary[ModeratorActions.ModerateUser] > 0)
                    ModeratedLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.ModerateUser );
            }

            if (null != UnmoderatedLink) 
            {
                UnmoderatedLink.NavigateUrl = null;
                UnmoderatedLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.UnmoderateUser ), summary[ModeratorActions.UnmoderateUser].ToString() );
                if (summary[ModeratorActions.UnmoderateUser] > 0)
                    UnmoderatedLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.UnmoderateUser );
            }

            if (null != ResetPasswdLink) 
            {
                ResetPasswdLink.NavigateUrl = null;
                ResetPasswdLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.ResetPassword ), summary[ModeratorActions.ResetPassword].ToString() );
                if (summary[ModeratorActions.ResetPassword] > 0)
                    ResetPasswdLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.ResetPassword );
            }

            if (null != ChangePasswdLink) 
            {
                ChangePasswdLink.NavigateUrl = null;
                ChangePasswdLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.ChangePassword ), summary[ModeratorActions.ChangePassword].ToString() );
                if (summary[ModeratorActions.ChangePassword] > 0)
                    ChangePasswdLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.ChangePassword );
            }

            if (null != EditedLink) 
            {
                EditedLink.NavigateUrl = null;
                EditedLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.EditUser ), summary[ModeratorActions.EditUser].ToString() );
                if (summary[ModeratorActions.EditUser] > 0)
                    EditedLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.EditUser );
            }

            if (null != PostIsAnnouncementLink) 
            {
                PostIsAnnouncementLink.NavigateUrl = null;
                PostIsAnnouncementLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.PostIsAnnouncement ), summary[ModeratorActions.PostIsAnnouncement].ToString() );
                if (summary[ModeratorActions.PostIsAnnouncement] > 0)
                    PostIsAnnouncementLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.PostIsAnnouncement );
            }

            if (null != PostIsNotAnnoucementLink) 
            {
                PostIsNotAnnoucementLink.NavigateUrl = null;
                PostIsNotAnnoucementLink.Text = string.Format( linkFormat, Formatter.FormatModeratorAction( ModeratorActions.PostIsNotAnnoucement ), summary[ModeratorActions.PostIsNotAnnoucement].ToString() );
                if (summary[ModeratorActions.PostIsNotAnnoucement] > 0)
                    PostIsNotAnnoucementLink.NavigateUrl = SiteUrls.Instance().UserModerationHistory( csContext.UserID, (int) ModeratorActions.PostIsNotAnnoucement );
            }
        }
		
        #endregion		
		
        #region Skin
                
        protected override void AttachChildControls() 
        {            
            // Message actions controls
            //
            ApprovedPostsLink = (HyperLink) FindControl( "ApprovedPostsLink" );
            EditedPostsLink = (HyperLink) FindControl( "EditedPostsLink" );
            MovedPostsLink = (HyperLink) FindControl( "MovedPostsLink" );
            DeletedPostsLink = (HyperLink) FindControl( "DeletedPostsLink" );
            LockedPostLink = (HyperLink) FindControl( "LockedPostLink" );
            UnlockedPostLink = (HyperLink) FindControl( "UnlockedPostLink" );
            MergedPostLink = (HyperLink) FindControl( "MergedPostLink" );
            SplitedPostLink = (HyperLink) FindControl( "SplitedPostLink" );
            UnApprovedPostLink = (HyperLink) FindControl( "UnApprovedPostLink" );
            
            // Forum actions controls
            //
            BannedLink = (HyperLink) FindControl( "BannedLink" );
            UnBannedLink = (HyperLink) FindControl( "UnBannedLink" );
            ModeratedLink = (HyperLink) FindControl( "ModeratedLink" );
            UnmoderatedLink = (HyperLink) FindControl( "UnmoderatedLink" );
            ResetPasswdLink = (HyperLink) FindControl( "ResetPasswdLink" );
            ChangePasswdLink = (HyperLink) FindControl( "ChangePasswdLink" );
            EditedLink = (HyperLink) FindControl( "EditedLink" );
            PostIsAnnouncementLink = (HyperLink) FindControl( "PostIsAnnouncementLink" );
            PostIsNotAnnoucementLink = (HyperLink) FindControl( "PostIsNotAnnoucementLink" );
			
            //InitializeChildControls();
        }
        
        /* Not used
        private void InitializeChildControls() 
        {		
        }*/
		        
        #endregion
    }
}
