//------------------------------------------------------------------------------
// <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.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Galleries;
using CommunityServer.Galleries.Components;
using CA = ComponentArt.Web.UI;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Photos
{
	/// <summary>
	///		Summary description for ReferralsListControl.
	/// </summary>
	public class ReferralsListControl : BaseGalleryGridControl
	{

		#region Child Controls

		protected DropDownList filterPost;
		protected ResourceControl ResourceControl3;
		protected CA.Grid Grid1;

		#endregion

		private void Page_Load(object sender, EventArgs e)
		{
			if(!Page.IsPostBack && !GalleryPage.IsCallBack)
			{
				ConfigurePage();
				buildGrid();
				Grid.DataBind();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
			this.filterPost.SelectedIndexChanged += new EventHandler(this.filterPost_SelectedIndexChanged);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new EventHandler(this.Page_Load);

		}
		#endregion

		protected override void buildGrid()
		{
			CSContext context = CSContext.Current;

			int pid = context.GetIntFromQueryString("pid",-1);

			GalleryThreadQuery query = new GalleryThreadQuery();
			query.SectionID = this.CurrentGallery.SectionID;
			query.ApplicationPostType = GalleryPostType.Image;
            
			if(this.GridMode == CA.GridRunningMode.Callback)
			{
				query.PageSize = this.DefaultPageSize;
				query.PageIndex = Grid.CurrentPageIndex;
				Grid.ManualPaging = true;
			}
			else
			{
				query.PageSize = int.MaxValue;
				Grid.ManualPaging = false;
			}

			Referral r = new Referral();
			r.SectionID = this.CurrentGallery.SectionID;
			r.PostID = pid;

			ReferralSet rs = CommunityServer.Components.Referrals.GetReferrals(r, query.PageSize, Grid.CurrentPageIndex);

			foreach (Referral r2 in rs.Referrals)
			{
				r2.ViewPostURL = GalleryUrls.Instance().ViewPicture(this.CurrentGallery.ApplicationKey, GalleryPosts.GetPicture(r2.PostID));
				r2.Url = Context.Server.HtmlEncode(r2.Url);
			}

			Grid.DataSource = rs.Referrals;
			this.RecordCount = rs.TotalRecords;
		}

		void ConfigurePage()
		{
			// Retrieve list of blog posts
			GalleryThreadQuery query = new GalleryThreadQuery();
			query.SectionID = this.CurrentGallery.SectionID;
			query.ApplicationPostType = GalleryPostType.Image;
			query.GalleryThreadType = GalleryThreadType.Recent;
			query.IncludeCategories = false;
			query.PublishedFilter = GalleryPostPublishedFilter.All;
			query.PageSize = int.MaxValue;
			query.PageSize = 20; //Do we want to return all values to the dropdown
			query.SortOrder = SortOrder.Descending;
			query.SortBy = GalleryThreadSortBy.PictureDate;
			ThreadSet threads = GalleryPosts.GetPictures(query,false);

			// Insert default "All" item
			filterPost.Items.Add(new ListItem(ResourceManager.GetString("CP_Photos_CategoryFilterAll"), "-1"));

			foreach (GalleryPost post in threads.Threads)
			{
				filterPost.Items.Add(new ListItem(Globals.HtmlDecode(post.Subject), Convert.ToString(post.PostID)));
			}

			// Check if preselected values of the filter DropDownLists were passed in the URL
			CSContext context = CSContext.Current;
			ListItem li = null;

			int pid = context.GetIntFromQueryString("pid",-1);

			if(pid > -1)
			{
				li = filterPost.Items.FindByValue(pid.ToString());
				if(li != null)
				{
					li.Selected = true;
                
				}
				this.GridMode = CA.GridRunningMode.Client;
			}
			else
			{
				this.GridMode = CA.GridRunningMode.Callback;    
			}

		}

		protected override void ConfigureGrid()
		{
			this.AddGridHrefTemplate("## DataItem.GetMember('Url').Text ##", "Url") ;
			this.AddGridHrefTemplate("## DataItem.GetMember('ViewPostURL').Text ##", "Title") ;
			this.AddGridPagerTemplate("Title","Url","LastDate", null);
		}

		private void filterPost_SelectedIndexChanged(object sender, EventArgs e)
		{
			string url  = "{0}?pid={1}";
			url = string.Format(url, Request.Path, filterPost.SelectedValue);
			Response.Redirect(url);
		
		}
	}
}
