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

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using CommunityServer.Components;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{

	public class GalleryImage : Control
	{

		#region Protected Members

		protected GalleryImageSettings settings = new GalleryImageSettings();
		protected GalleryImageSettings secondarySettings = new GalleryImageSettings();
        protected string imageIDprefix = "SmallThumb";

		#endregion

		#region Public Properties

		private GalleryImageType imageType = GalleryImageType.Thumbnail;
		public GalleryImageType ImageType
		{
			get { return imageType; }
			set { imageType = value; }
		}

		[DefaultValue( "" )]
		private string imageUrl = string.Empty;
		public string ImageUrl
		{
			get { return imageUrl; }
			set { imageUrl = value; }
		}

		[DefaultValue( "" )]
		private string secondaryImageUrl = string.Empty;
		public string SecondaryImageUrl
		{
			get { return secondaryImageUrl; }
			set { secondaryImageUrl = value; }
		}

		[DefaultValue( "" )]
		private string navigateUrl = string.Empty;
		public string NavigateUrl
		{
			get { return navigateUrl; }
			set { navigateUrl = value; }
		}

		[DefaultValue( false )]
		private bool enableSecondaryThumbnail = false;
		public bool EnableSecondaryThumbnail
		{
			get { return enableSecondaryThumbnail; }
			set { enableSecondaryThumbnail = value; }
		}

		[DefaultValue( -1 )]
		private int postID = -1;
		public int PostID
		{
			get { return postID; }
			set
			{
				postID = value;
				galleryPost = null;
			}
		}

		[DefaultValue( true )]
		private bool usePhotoCache = true;
		public bool UsePhotoCache
		{
			get { return usePhotoCache; }
			set { usePhotoCache = value; }
		}

		[DefaultValue( 0 )]
		private int border = 0;
		public int Border
		{
			get { return border; }
			set { border = value; }
		}

		private string cssClass = string.Empty;
		public string CssClass
		{
			get { return cssClass; }
			set { cssClass = value; }
		}

		private string style = string.Empty;
		public string Style
		{
			get { return style; }
			set { style = value; }
		}

		private int width = 100;
		public int Width
		{
			get { return width; }
			set { width = value; }
		}

		private int height = 75;
		public int Height
		{
			get { return height; }
			set { height = value; }
		}

		private int quality = 70;
		public int Quality
		{
			get { return quality; }
			set { quality = value; }
		}

		private GalleryPost galleryPost = null;
		public GalleryPost GalleryPost
		{
			get { return galleryPost; }
			set
			{
				galleryPost = value;
				if(galleryPost != null)
					postID = galleryPost.PostID;
			}
		}

		#endregion

		#region Constructor

		public GalleryImage() { }

		public GalleryImage(GalleryImageType imageType) : this()
		{
			this.ImageType = imageType;
		}

		public GalleryImage(GalleryImageType imageType, GalleryPost galleryPost)
		{
			this.ImageType = imageType;
			this.GalleryPost = galleryPost;
		}

		#endregion

		#region Methods

		protected virtual void LoadPicture()
		{
			if(galleryPost == null)
			{
				CSContext cSContext = CSContext.Current;
				if(postID != -1)
					GalleryPost = GalleryPosts.GetPicture(PostID);
				else if(cSContext.PostID != -1)
					GalleryPost = GalleryPosts.GetPicture(cSContext.PostID);
			}
		}

		public virtual void ProcessPicture()
		{
			// If there is no PostID, then there is nothing to process, return;
			LoadPicture();
			if(ImageType == GalleryImageType.Folder)
				galleryPost = new GalleryPost();
			if(galleryPost == null)
				return;

			// If this is not an image, turn off the large thumbnail popups
			if(EnableSecondaryThumbnail && !galleryPost.IsPicture)
				EnableSecondaryThumbnail = false;

			// Find out which image type, put in the default settings, and scale it
			if(galleryPost.IsPicture || ImageType == GalleryImageType.Folder)
			{
				switch(ImageType)
				{
					case GalleryImageType.Micro:
						settings = galleryPost.Gallery.GetThumbnailSettings();
						//settings = new GalleryImageSettings(((Gallery)galleryPost.Section).ThumbnailX, ((Gallery)galleryPost.Section).ThumbnailY , ((Gallery)galleryPost.Section).ThumbnailQuality, true, false, false);
						//settings.Brightness = ((Gallery)galleryPost.Section).ThumbnailBrightness;
						settings.GalleryImageType = GalleryImageType.Micro;
						ImageUrl = ImageHandling.GetPictureUrl(galleryPost, settings);
						this.Height = settings.Height;
						this.Width = settings.Width;
						break;

					case GalleryImageType.Gallery:
						settings = galleryPost.Gallery.GetSecondaryThumbnailSettings();
						//settings = new GalleryImageSettings(((Gallery)galleryPost.Section).SecondaryThumbnailX, ((Gallery)galleryPost.Section).SecondaryThumbnailY, ((Gallery)galleryPost.Section).SecondaryThumbnailQuality, true, false, false);
						settings.GalleryImageType = GalleryImageType.Gallery;
						ImageUrl = ImageHandling.GetPictureUrl(galleryPost, settings);
						this.Height = settings.Height;
						this.Width = settings.Width;
						break;

					case GalleryImageType.Original:
						settings.OriginalSize = true;
						settings.GalleryImageType = GalleryImageType.Original;
						break;

					case GalleryImageType.Thumbnail:
						if(EnableSecondaryThumbnail)
						{
							secondarySettings = galleryPost.Gallery.GetSecondaryThumbnailSettings();
							//secondarySettings = new GalleryImageSettings(((Gallery)galleryPost.Section).SecondaryThumbnailX, ((Gallery)galleryPost.Section).SecondaryThumbnailY, ((Gallery)galleryPost.Section).SecondaryThumbnailQuality, true, false, false);
							SecondaryImageUrl = ImageHandling.GetPictureUrl(galleryPost, secondarySettings);
						}
						settings = galleryPost.Gallery.GetThumbnailSettings();
						//settings = new GalleryImageSettings(((Gallery)galleryPost.Section).ThumbnailX, ((Gallery)galleryPost.Section).ThumbnailY, ((Gallery)galleryPost.Section).ThumbnailQuality, true, false, false);
						//settings.GalleryImageType = GalleryImageType.Thumbnail;
						//settings.Brightness = ((Gallery)galleryPost.Section).ThumbnailBrightness;
						ImageUrl = ImageHandling.GetPictureUrl(galleryPost, settings);
						this.Height = settings.Height;
						this.Width = settings.Width;
						break;

					case GalleryImageType.SecondaryThumbnail:
						settings = galleryPost.Gallery.GetSecondaryThumbnailSettings();
						//settings = new GalleryImageSettings(((Gallery)galleryPost.Section).SecondaryThumbnailX, ((Gallery)galleryPost.Section).SecondaryThumbnailY, ((Gallery)galleryPost.Section).SecondaryThumbnailQuality, true, false, false);
						//settings.GalleryImageType = GalleryImageType.SecondaryThumbnail;
						ImageUrl = ImageHandling.GetPictureUrl(galleryPost, settings);
						this.Height = settings.Height;
						this.Width = settings.Width;
						break;

					case GalleryImageType.Details:
						settings = galleryPost.Gallery.GetPictureDetailsSettings();
						//settings = new GalleryImageSettings(((Gallery)galleryPost.Section).PictureDetailsX, ((Gallery)galleryPost.Section).PictureDetailsY, ((Gallery)galleryPost.Section).PictureDetailsQuality, true, false, false);
						//settings.GalleryImageType = GalleryImageType.Details;
						ImageUrl = ImageHandling.GetPictureUrl(galleryPost, settings);
						this.Height = settings.Height;
						this.Width = settings.Width;
						break;

					case GalleryImageType.Folder:
						settings.Width = 60;
						settings.Height = 56;
						settings.GalleryImageType = GalleryImageType.Folder;
						ImageUrl = GalleryUrls.Instance().DefaultHighlightImage;
						this.Height = settings.Height;
						this.Width = settings.Width;
						break;

					case GalleryImageType.Slideshow:
						settings = galleryPost.Gallery.GetSlideshowSettings();
						//settings = new GalleryImageSettings(((Gallery)galleryPost.Section).SlideshowX, ((Gallery)galleryPost.Section).SlideshowY, ((Gallery)galleryPost.Section).SlideshowQuality, true, false, false);
						//settings.GalleryImageType = GalleryImageType.Slideshow;
						ImageUrl = ImageHandling.GetPictureUrl(galleryPost, settings);
						this.Height = settings.Height;
						this.Width = settings.Width;
						break;

					case GalleryImageType.Other:
						settings = new GalleryImageSettings(width, height, quality, true);
						settings.GalleryImageType = GalleryImageType.Other;
						ImageUrl = ImageHandling.GetPictureUrl(galleryPost, settings);
						this.Height = settings.Height;
						this.Width = settings.Width;
						break;

					default:
						ImageUrl = "";
						break;
				}

				if(settings.OriginalSize == true)
				{
					ImageUrl = GalleryUrls.Instance().PictureUrl(galleryPost);
					this.Height = galleryPost.Attachment.Height;
					this.Width = galleryPost.Attachment.Width;
				}
			}
			else
			{
				settings = new GalleryImageSettings(39, 48, 100, false, false, false);
				ImageUrl = Globals.GetSiteUrls().Locations["galleries"] + "default_file_image.gif";
			}

		}

		#endregion

		#region Event Overrides

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			PostID = CSContext.Current.PostID;
		}

		protected override void OnPreRender(EventArgs e)
		{
			try
			{
				ProcessPicture();
			}
			catch{}
			base.OnPreRender(e);
		}

		protected override void Render(HtmlTextWriter writer)
		{
			if(galleryPost == null)
				return;

			HtmlAnchor a = new HtmlAnchor();
			HtmlImage img = new HtmlImage();

			bool secondaryPopup = false;

			if(EnableSecondaryThumbnail)
			{
				Gallery gallery = galleryPost.Section as Gallery;
				if((gallery.ThumbnailX != gallery.SecondaryThumbnailX) || (gallery.ThumbnailY != gallery.SecondaryThumbnailY))
					secondaryPopup = true;
			}

			if(NavigateUrl != string.Empty)
			{
				a.HRef = NavigateUrl;
			}

			img.Src = ImageUrl;
			img.Alt = galleryPost.Subject.Replace("\"", "\\\"");

			if (Width > 0)
				img.Width = settings.Width;

			if (Height > 0)
				img.Height = settings.Height;

			img.Border = Border;

			if(cssClass != string.Empty)
				img.Attributes["Class"] = CssClass;
			if(style != string.Empty)
				img.Attributes["Style"] = style;

			if(EnableSecondaryThumbnail && !secondaryPopup)
			{
				img.Attributes["onmouseover"] = "javascript:this.src='" + SecondaryImageUrl + "';";
				img.Attributes["onmouseout"] = "javascript:this.src='" + ImageUrl + "';";
			}
			else if(EnableSecondaryThumbnail && secondaryPopup)
				img.Attributes["onmouseover"] = "ToggleGalleryThumbnail(" + galleryPost.PostID.ToString() + ");";

			img.Attributes["id"] = this.ClientID + "_" + imageIDprefix + galleryPost.PostID.ToString();

			// If we're doing a Matte rendering we have some special switch logic
			if (ImageType == GalleryImageType.Gallery) {
				
				img.Attributes["style"] = "border:4px solid white;";
				RenderMatte(img, a).RenderControl(writer);

			} else if (ImageType == GalleryImageType.Details || ImageType == GalleryImageType.Slideshow || ImageType == GalleryImageType.SecondaryThumbnail) {

				img.Attributes["style"] = "border:4px solid white;";
				RenderBorder(img, a).RenderControl(writer);

			} else {

				if (ImageType == GalleryImageType.Micro)
					img.Attributes["style"] = "border:3px solid white;";

				if(NavigateUrl != string.Empty) {
					a.Controls.Add(img);
					a.RenderControl(writer);
				}
				else
					img.RenderControl(writer);

			}

			if(secondaryPopup)
				RenderSecondaryThumbnail(writer);
		}

		#endregion

		#region Render Helpers

		private HtmlTableCell MatteTableCell(string className, string imgSrc) 
		{
			HtmlTableCell td = new HtmlTableCell();

			td.Attributes["class"] = className;

			if (imgSrc != null) {
				HtmlImage img = new HtmlImage();

				img.Src = imgSrc;
				img.Attributes["style"] = "border-width:0px;";
				td.Controls.Add(img);
			}

			return td;
		}

		private HtmlTable RenderBorder(HtmlImage img, HtmlAnchor anchor) {
			HtmlTable t = new HtmlTable();
			HtmlTableRow tr;
			HtmlTableCell td;

			// table properties
			t.Border = 0;
			t.CellPadding = 0;
			t.CellSpacing = 0;
			t.Attributes["class"] = "photo-frame";

			// row #1
			tr = new HtmlTableRow();
			tr.Cells.Add(MatteTableCell("topx--", null));
			tr.Cells.Add(MatteTableCell("top-x-", null));
			tr.Cells.Add(MatteTableCell("top--x", null));
			t.Rows.Add(tr);

			// row #2
			tr = new HtmlTableRow();
			tr.Cells.Add(MatteTableCell("midx--", null));
			td = new HtmlTableCell();
			td.BgColor = "white";
			anchor.Controls.Add(img);
			td.Controls.Add( anchor );
			tr.Cells.Add(td);
			tr.Cells.Add(MatteTableCell("mid--x", null));
			t.Rows.Add(tr);

			// row #3
			tr = new HtmlTableRow();
			tr.Cells.Add(MatteTableCell("botx--", null));
			tr.Cells.Add(MatteTableCell("bot-x-", null));
			tr.Cells.Add(MatteTableCell("bot--x", null));
			t.Rows.Add(tr);

			return t;

		}

		private HtmlTable RenderMatte(HtmlImage img, HtmlAnchor anchor) {
			HtmlTable t = new HtmlTable();
			HtmlTableRow tr;
			HtmlTableCell td;

			// table properties
			t.Border = 0;
			t.CellPadding = 0;
			t.CellSpacing = 0;
			t.Attributes["class"] = "album-frame";

			// row #1
			tr = new HtmlTableRow();
			tr.Cells.Add(MatteTableCell("topx----", null));
			tr.Cells.Add(MatteTableCell("top-x---", ThemeManager.SkinPath() + "/images/alblum-mtl.gif"));
			tr.Cells.Add(MatteTableCell("top--x--", null));
			tr.Cells.Add(MatteTableCell("top---x-", ThemeManager.SkinPath() + "/images/alblum-mtr.gif"));
			tr.Cells.Add(MatteTableCell("top-----", null));
			t.Rows.Add(tr);

			// row #2
			tr = new HtmlTableRow();
			tr.Cells.Add(MatteTableCell("mtpx----", ThemeManager.SkinPath() + "/images/alblum-mltl.gif"));
			td = new HtmlTableCell();
			td.ColSpan = 3;
			td.VAlign = "center";
			td.Align = "center";
			td.BgColor = "white";
			td.RowSpan = 3;
			anchor.Controls.Add(img);
			td.Controls.Add( anchor );
			tr.Cells.Add(td);
			tr.Cells.Add(MatteTableCell("mtp----x", ThemeManager.SkinPath() + "/images/alblum-mrtr.gif"));
			t.Rows.Add(tr);

			// row #3
			tr = new HtmlTableRow();
			tr.Cells.Add(MatteTableCell("midx----", null));
			tr.Cells.Add(MatteTableCell("mid----x", null));
			t.Rows.Add(tr);

			// row #4
			tr = new HtmlTableRow();
			tr.Cells.Add(MatteTableCell("mbtx----", ThemeManager.SkinPath() + "/images/alblum-mlbl.gif"));
			tr.Cells.Add(MatteTableCell("mbt----x", ThemeManager.SkinPath() + "/images/alblum-mrbr.gif"));
			t.Rows.Add(tr);

			// row #5
			tr = new HtmlTableRow();
			tr.Cells.Add(MatteTableCell("botx----", null));
			tr.Cells.Add(MatteTableCell("bot-x---", ThemeManager.SkinPath() + "/images/alblum-mbl.gif"));
			tr.Cells.Add(MatteTableCell("bot--x--", null));
			tr.Cells.Add(MatteTableCell("bot---x-", ThemeManager.SkinPath() + "/images/alblum-mbr.gif"));
			tr.Cells.Add(MatteTableCell("bot----x", null));
			t.Rows.Add(tr);

			return t;
			
		}

		protected void RenderSecondaryThumbnail(HtmlTextWriter writer)
		{
			writer.Write("<div id='SecondaryThumbDiv{0}' class='secondaryThumbnailHidden'>", galleryPost.PostID );

			if(NavigateUrl != string.Empty)
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Href, NavigateUrl);
				writer.RenderBeginTag(HtmlTextWriterTag.A);
			}

			writer.AddAttribute(HtmlTextWriterAttribute.Src, SecondaryImageUrl);
			writer.AddAttribute(HtmlTextWriterAttribute.Alt, galleryPost.Subject.Replace("\"", "\\\""));
			writer.AddAttribute(HtmlTextWriterAttribute.Width, secondarySettings.Width.ToString());
			writer.AddAttribute(HtmlTextWriterAttribute.Height, secondarySettings.Height.ToString());
			writer.AddAttribute(HtmlTextWriterAttribute.Border, Border.ToString());

			writer.AddAttribute(HtmlTextWriterAttribute.Id, "SecondaryThumb" + galleryPost.PostID.ToString());
			writer.AddAttribute("onmouseout", "ToggleGalleryThumbnail(" + galleryPost.PostID.ToString() + ");");

			writer.RenderBeginTag(HtmlTextWriterTag.Img);

			writer.RenderEndTag();

			if(NavigateUrl != string.Empty)
				writer.RenderEndTag();

			writer.Write("</div>");
		}

		#endregion

	}

}
