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

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Reporting;
using CA = ComponentArt.Web.UI;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Tools.Reports
{
	/// <summary>
	/// Summary description for BlogActivityReport.
	/// </summary>
	public class BlogActivityReport : BaseToolsPage
	{
		#region Members

		protected CA.Grid Grid1;
		protected DropDownList PageList;
		protected ResourceLinkButton FilterButton;
		protected Modal Modal1;
		protected ActivityQueryControl BlogActivityQueryControl1;
		protected Label blogCountLabel;
		protected Label enabledBlogCountLabel;
		protected Label disabledBlogCountLabel;
		protected Label rangeTotalBlogs;
		int recordCount = 0;
		string BegReportDate;
		protected CommunityServer.ControlPanel.Controls.ControlPanelSelectedNavigation SelectedNavigation1;
		protected CommunityServer.ControlPanel.Controls.ResourceControl RegionTitle;
		protected CommunityServer.Controls.MPContent DescriptionRegion;
		protected CommunityServer.ControlPanel.Controls.ResourceControl TotalBlogs;
		protected CommunityServer.ControlPanel.Controls.ResourceControl TotalEnabledBlogs;
		protected CommunityServer.ControlPanel.Controls.ResourceControl TotalDisabledBlogs;
		protected CommunityServer.ControlPanel.Controls.ResourceControl RangeTotal;
		protected CommunityServer.Controls.MPContent TaskRegion;
		protected CommunityServer.Controls.MPContainer MPContainer;
		protected CommunityServer.ControlPanel.Controls.ResourceControl Summary;
		string EndReportDate;

		protected CommunityServer.Controls.StatusMessage Status;

		#endregion

		override protected void OnInit(EventArgs e)
		{
			if (Telligent.Registration.CommunityServer.IsStandard)
			{
				Grid1.PageIndexChanged += new CA.Grid.PageIndexChangedEventHandler(OnPageIndexChanged);
				Grid1.NeedRebind += new CA.Grid.NeedRebindEventHandler(OnNeedRebind);
				Grid1.NeedDataSource += new CA.Grid.NeedDataSourceEventHandler(OnNeedDataSource);
				Grid1.ItemDataBound += new CA.Grid.ItemDataBoundEventHandler(Grid1_ItemDataBound);

				this.Load += new EventHandler(this.Page_Load);
			}
			else
			{
				Status.Success = false;
				Status.IsLicenseMessage = true;
				Status.Text = CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_Tools_BlogActivityReport_LicenseNotAvailable");
				Status.Visible = true;
			}

			//Taken from the BaseGridControl Helper function - In the future this page should inherit from that base class
			foreach(CA.GridColumn gc in Grid1.Levels[0].Columns)
			{
				if(gc.Visible)
				{
					gc.SortedDataCellCssClass = "SortedDataCell";
					//Allows for mock resource manager calls in heading text
					//eg HeadingText="ResourceManager.CP_Photos_GridCol_Name"
					if(gc.HeadingText.StartsWith("ResourceManager."))
						gc.HeadingText = ResourceManager.GetString(gc.HeadingText.Replace("ResourceManager.",""));
			
				}
			}

			base.OnInit(e);
		}

		private void Page_Load(object sender, EventArgs e)
		{
			if(!Page.IsPostBack && !this.IsCallBack)
			{
				CSContext context = CSContext.Current;
				if(context.QueryString["csv"] != null && context.QueryString["csv"].ToString() == "1")
					BuildExcelDoc();
				else
					Bind();
			}
		}

		private void Bind()
		{
			BlogActivitySummaryResultSet basrs = BuildReportData();

			Grid1.DataSource = basrs.Records;
			this.recordCount = basrs.TotalRecords;
			blogCountLabel.Text = basrs.TotalBlogs.ToString("#,###");
			enabledBlogCountLabel.Text = basrs.TotalEnabledBlogs.ToString("#,###");
			disabledBlogCountLabel.Text = basrs.TotalDisabledBlogs.ToString("#,###");
			rangeTotalBlogs.Text = basrs.TotalRecords.ToString("#,###");
			Grid1.DataBind();
			Grid1.RecordCount = recordCount; //recordCount;
		}

		private BlogActivitySummaryResultSet BuildReportData()
		{
			CSContext context = CSContext.Current;
			BegReportDate = "1/1/1900";
			EndReportDate = "1/1/1900";
			int startRecord;
			int endRecord;
			Grid1.PageSize = 20;

			startRecord = (Grid1.CurrentPageIndex * Grid1.PageSize) + 1;
			endRecord = ((Grid1.CurrentPageIndex + 1) * Grid1.PageSize);

			if (Globals.IsDate(context.QueryString["brd"]))
			{
				BegReportDate = context.QueryString["brd"];
				BlogActivityQueryControl1.BegSearchReportDatePicker = DateTime.Parse(BegReportDate);
				BlogActivityQueryControl1.BegSearchReportDateCalendar = DateTime.Parse(BegReportDate);
			}
			if (Globals.IsDate(context.QueryString["erd"]))
			{
				EndReportDate = context.QueryString["erd"];
				BlogActivityQueryControl1.EndSearchReportDatePicker = DateTime.Parse(EndReportDate);
				BlogActivityQueryControl1.BegSearchReportDateCalendar = DateTime.Parse(EndReportDate);
			}

			BlogActivitySummaryResultSet basrs = new BlogActivitySummaryResultSet();
			
			basrs = ReportingSqlDataProvider.GetBlogActivitySummaryResults(startRecord, endRecord, BegReportDate, EndReportDate);

			return basrs;
		}

		#region Events

		public void OnPageIndexChanged(object sender, CA.GridPageIndexChangedEventArgs args)
		{
			Grid1.CurrentPageIndex = args.NewIndex;
		}

		public void OnNeedDataSource(object sender, EventArgs oArgs)
		{
			BlogActivitySummaryResultSet basrs = BuildReportData();
			Grid1.DataSource = basrs.Records;
			this.recordCount = basrs.TotalRecords;
			rangeTotalBlogs.Text = recordCount.ToString();
		}

		public void OnNeedRebind(object sender, EventArgs oArgs)
		{
			Grid1.DataBind();
			Grid1.RecordCount = recordCount;
			rangeTotalBlogs.Text = recordCount.ToString();
		}

		#endregion

		private void Grid1_ItemDataBound(object sender, CA.GridItemDataBoundEventArgs e)
		{
			HyperLink link = (HyperLink)e.Content.FindControl("blogHyperLink");
			link.NavigateUrl = link.NavigateUrl + e.Item["ApplicationKey"] + "/";
			link.Text = e.Item["ApplicationKey"].ToString();
		}

		private void InitializeComponent()
		{

		}

		private void BuildExcelDoc()
		{
			CSContext context = CSContext.Current;
			BegReportDate = "1/1/1900";
			EndReportDate = "1/1/1900";
			if (Globals.IsDate(context.QueryString["brd"]))
				BegReportDate = context.QueryString["brd"];
			if (Globals.IsDate(context.QueryString["erd"]))
				EndReportDate = context.QueryString["erd"];
			string CSV = "";
			CSV = ReportingSqlDataProvider.GetBlogActivitySummaryCSV(0, 0, BegReportDate, EndReportDate);
			Response.ContentType = "Application/x-msexcel";
			Response.AddHeader("content-disposition", "attachment; filename=\"BlogActivityReport.csv\"");
			Response.Write(CSV);
			Response.End();
		}
	}
}
