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

using System;
using System.Collections;
using CommunityServer.Components;
using CommunityServer.Galleries.Controls;

namespace CommunityServer.Galleries.Components
{
	/// <summary>
	/// Generates an XML feed by a specific category
	/// </summary>
	public class GallerySlideshowWriter : BaseSyndicationWriter
	{

		protected int CategoryID = -1;
		protected int MainImageWidth = -1;
		protected int MainImageHeight = -1;

		public GallerySlideshowWriter(Gallery gallery, string baseUrl) : base(new ArrayList() , gallery, baseUrl)
		{

		}

		public GallerySlideshowWriter(Gallery gallery, string baseUrl, int categoryId, int mainImageWidth, int mainImageHeight) : base(new ArrayList() , gallery, baseUrl)
		{
			CategoryID = categoryId;
			MainImageWidth = mainImageWidth;
			MainImageHeight = mainImageHeight;
		}

		protected override string EnclosureUrl(PostAttachmentMetaData pa)
		{
			throw new NotImplementedException();
		}

		protected override void Build()
		{
			StartDocument();
			
			if(CurrentSection != null)
				BuildSection(CurrentSection);
			else
			{
				ArrayList galleries = Galleries.GetGalleries();
				galleries.Sort(new SectionSorter(SectionSortBy.LastPost));

				foreach(Gallery g in galleries)
					BuildSection(g);
			}

			EndDocument();
		}

		protected virtual void BuildSection(Section g)
		{
			ArrayList categories;
			if(CategoryID == -1)
			{
				categories = PostCategories.GetCategories(g.SectionID, true, false, CategoryID);

				PostCategory root = new PostCategory();
				//root.Name = "\\";
				root.Name = g.Name;
				root.Description = g.Description; 
				root.IsEnabled = true;
				root.SectionID = g.SectionID;

				categories.Add(root);
			}
			else
			{
				categories = new ArrayList();
				PostCategory pc = PostCategories.GetCategory(CategoryID, g.SectionID, false);
				if(pc != null)
					categories.Add(pc) ;
			}
			PostCategory.SortBy = g.CategorySortBy;
			categories.Sort();
			foreach(PostCategory c in categories)
			{
				WriteAlbum(Galleries.GetGallery(g.SectionID) , c);
			}

		}
		/// <summary>
		/// Output the gallery base element.
		/// </summary>
		protected virtual void StartDocument()
		{
			this.WriteStartElement("gallery");
		}

		protected virtual void WriteAlbum(Gallery g, PostCategory c)
		{
			string CategoryPrefix = "";
			GalleryThreadQuery query = new GalleryThreadQuery();
			query.SectionID = c.SectionID;
			query.PageSize = int.MaxValue;
			if(c.CategoryID > 0)
			{
				query.CategoryID = c.CategoryID;
				CategoryPrefix = string.Format("{0} : ", g.Name);
			}
			else
				query.UncategorizedOnly = true;

			query.ApplicationPostType = GalleryPostType.Image;
			query.PublishedFilter = GalleryPostPublishedFilter.Published;

			query.SortBy = g.ThreadSortBy;
			query.SortOrder = g.ThreadSortOrder;

			// Set the datasource and bind
			ThreadSet ts = GalleryPosts.GetPictures(query);
			if(ts.Threads.Count > 0)
			{
				StartAlbum(CategoryPrefix, c);
				
				foreach(GalleryPost p in ts.Threads)
				{
					WriteItem(p);
				}
				EndAlbum();
			}
		}

		protected virtual void StartAlbum(string CategoryPrefix, PostCategory c)
		{
			this.WriteStartElement("album");
			this.WriteAttributeString("title", string.Concat(CategoryPrefix, c.Name) ) ;
			this.WriteAttributeString("description", c.Description) ;
			
			GalleryPostCategory category = GalleryPostCategory.Parse(c); 
			GalleryPost p;

			if(category.HasFeaturedPhoto)
				p = (GalleryPost)category.FeaturedPost;
			else 
				p = GalleryPosts.GetFirstPicture(c.SectionID, c.CategoryID);
			if(p != null)
			{
			
				GalleryImage image = new GalleryImage(GalleryImageType.Other, p);
				GalleryImageSettings settings = new GalleryImageSettings(50,40, 70, true, false, false);
				ImageHandling.GetMaintainedRatio(p, settings);
				if(image != null)
				{
					string imageUrl;
					if(GalleryConfiguration.Instance().AttachmentSettings.EnableDirectLinks)
					{
						image.Height = settings.Height;
						image.Width = settings.Width;
						image.ProcessPicture() ;
						imageUrl = image.ImageUrl;
					}
					else
					{
						imageUrl = GalleryUrls.Instance().PictureUrl(p, GalleryImageType.Other, settings.Width, settings.Height); 
					}
					this.WriteAttributeString("tn", imageUrl ) ;
				}
			}
		
		}

		protected virtual void EndAlbum()
		{
			this.WriteEndElement();
		}

		protected virtual void WriteItem(GalleryPost p)
		{

			GalleryImageType imageType;
			GalleryImageSettings settings = null;
			if(MainImageHeight > 0 && MainImageWidth > 0)
			{
				imageType = GalleryImageType.Other;
				settings = new GalleryImageSettings(MainImageWidth, MainImageHeight, 70, true, false, false);
				ImageHandling.GetMaintainedRatio(p, settings);
			}
			else
			{
				imageType = GalleryImageType.Slideshow;
				settings = p.Gallery.GetSlideshowSettings();
				ImageHandling.GetMaintainedRatio(p, settings);
			}


			GalleryImage image = new GalleryImage(imageType, p);

			if(image == null)
				return;

			this.WriteStartElement("img");
			
			string imageUrl;
			if(GalleryConfiguration.Instance().AttachmentSettings.EnableDirectLinks)
			{
				if(settings != null)
				{
					image.Height = settings.Height;
					image.Width = settings.Width;
				}
				image.ProcessPicture() ;
				imageUrl = image.ImageUrl;
			}
			else
			{
				imageUrl = GalleryUrls.Instance().PictureUrl(p, imageType, settings.Width, settings.Height); 
			}
			this.WriteAttributeString("src", imageUrl ) ;

			string caption = p.Name;
			if(p.ForceExcerpt.Length > 0)
				caption = string.Format("{0} - {1}", caption, p.ForceExcerpt);
			this.WriteAttributeString("caption", caption) ;

			this.WriteAttributeString("link", p.ViewPictureURL) ;

			this.WriteAttributeString("tn", p.ThumbnailURL) ;
			
			this.WriteEndElement();

		}
		
		/// <summary>
		/// Write the end tag for a gallery element.
		/// </summary>
		protected void EndDocument()
		{
			this.WriteEndElement();
		}
	}
}
