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

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

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

namespace CommunityServer.Discussions.Controls {

	/// <summary>
	/// Summary description for UserModerationHistory.
	/// </summary>
	public class UserModerationHistory: TemplatedWebControl {

        #region Child Controls
        protected UserModerationCounters moderationCounters;
        protected Repeater auditHistoryRepeater;
		protected ForumPager pager;
		protected CurrentPage currentPage;
        protected Label currentCounterName;
        ModeratorActions action;
        UserModerationAction userAction;
        #endregion

		#region Skin
		protected override String ExternalSkinFileName {
			get {
				return "Moderation/View-UserModerationHistory.ascx";
			}
		}

		protected override void AttachChildControls () {
            currentCounterName = (Label) FindControl( "CurrentCounterName" );
            moderationCounters = (UserModerationCounters) FindControl( "ModerationCounters" );
			auditHistoryRepeater = (Repeater) FindControl( "AuditHistoryRepeater") ;
			currentPage = (CurrentPage) FindControl( "CurrentPage" );
			pager = (ForumPager) FindControl( "Pager" );

			InitializeChildControls();
		}

		private void InitializeChildControls () {
			auditHistoryRepeater.ItemDataBound += new RepeaterItemEventHandler( AuditHistoryRepeater_ItemDataBound );

			pager.PageSize = CSContext.Current.SiteSettings.MembersPerPage;
		}
		#endregion

         protected override void OnLoad (EventArgs e) {
            // Init moderation action from query
            //
            try {
                action = (ModeratorActions) CSContext.Current.GetIntFromQueryString("Action",(int)ModeratorActions.ApprovePost );
            } catch { 
                action = ModeratorActions.ApprovePost;
            }

            userAction = Audit.ModeratorActionToUserAction( action );

            if (!Page.IsPostBack) {
                this.DataBind();
            }

            base.OnLoad( e );
        }

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

            AuditSet auditSet = Audit.GetAuditHistoryForUser( CSContext.Current.UserID, action, pager.PageIndex, pager.PageSize );
            
            if (auditSet.HasResults)
                currentCounterName.Text = string.Format( ResourceManager.GetString( "UserModerationCounters_CurrentCounterTitle" ), Formatter.FormatModeratorAction( action ) );
            else
                currentCounterName.Text = "";

            auditHistoryRepeater.DataSource = auditSet.Records;
            auditHistoryRepeater.DataBind();

            pager.TotalRecords = currentPage.TotalRecords = auditSet.TotalRecords;
            currentPage.TotalPages = pager.CalculateTotalPages();
            currentPage.PageIndex = pager.PageIndex;
        }
        
		private void AuditHistoryRepeater_ItemDataBound (Object sender, RepeaterItemEventArgs e) {
			AuditItem dataItem = (AuditItem) e.Item.DataItem;

			switch (e.Item.ItemType) {
                case ListItemType.Header:
                    HtmlTableCell postHeaderColumn = (HtmlTableCell) e.Item.FindControl( "PostHeaderColumn" );
                    if (postHeaderColumn != null) { 
                        if (userAction == UserModerationAction.ForumAction)
                            // Hide post header column
                            //
                            postHeaderColumn.Visible = false;
                        else
                            // Hide post header column
                            //
                            postHeaderColumn.Visible = true;
                    } 
                    break;

				case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    HtmlTableCell postColumn = (HtmlTableCell) e.Item.FindControl( "PostColumn" );

                    if (postColumn != null) {
                        if (userAction == UserModerationAction.ForumAction) {
                            // Hide post column
                            //
                            postColumn.Visible = false;
                        } else { 
                            // Show post column
                            //
                            postColumn.Visible = true;

                    HyperLink ItemPostLink = (HyperLink)e.Item.FindControl( "ItemPostLink" );
                    if (ItemPostLink != null) {
                        if (dataItem.PostID > 0) {
                            ForumPost post = Posts.GetPost( dataItem.PostID, dataItem.UserID );
                            if (post != null) { 
                                ItemPostLink.NavigateUrl = Globals.GetSiteUrls().ModerationHistory( dataItem.PostID );
                                ItemPostLink.Text = Formatter.CheckStringLength( post.Subject, 18 );
                            } 
                            else { 
                                ItemPostLink.NavigateUrl = Globals.GetSiteUrls().ModerationHistory( dataItem.PostID );
                                ItemPostLink.Text = "#" + dataItem.PostID;
                            }
                        } else { 
                            ItemPostLink.NavigateUrl = null;
                            ItemPostLink.Text = ResourceManager.GetString( "NotAvailable" );
                        }
                    }
                    
                        }
                    }

                    HyperLink ItemUsernameLink = (HyperLink)e.Item.FindControl( "ItemUsernameLink" );
                    if (ItemUsernameLink != null) {
                        if (dataItem.ModeratorID > 0 && dataItem.ModeratorName.Length > 0) {
                            ItemUsernameLink.NavigateUrl = Globals.GetSiteUrls().ControlPanelUserEdit( dataItem.ModeratorID );
                            ItemUsernameLink.Text = dataItem.ModeratorName;
                        } else { 
                            ItemUsernameLink.NavigateUrl = null;
                            ItemUsernameLink.Text = ResourceManager.GetString( "NotAvailable" );
                        }
                    }

                    Label ItemNotes = (Label)e.Item.FindControl( "ItemNotes" );
                    if (ItemNotes != null) {
                        if (Globals.IsNullorEmpty( dataItem.Notes ))
                            ItemNotes.Text = ResourceManager.GetString( "NotAvailable" );
                        else
                            ItemNotes.Text = Globals.HtmlDecode( dataItem.Notes );
                    }

                    Label ItemDate = (Label)e.Item.FindControl( "ItemDate" );
                    if (ItemDate != null) {
                        ItemDate.Text = dataItem.DateModerated.ToString( CSContext.Current.User.Profile.DateFormat + "<br />hh:mm:ss tt");
                    }

					break;
			}
		}

	}
}
