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

using System;
using System.IO;
using System.Web;
using CommunityServer.Components;
using CommunityServer.Configuration;

namespace CommunityServer.Galleries.Components
{
	/// <summary>
	/// Summary description for GalleryCacheSettings.
	/// </summary>
	[System.Xml.Serialization.XmlRoot("CacheSettings")]
    public class GalleryCacheSettings : AttachmentSettings
	{

		public GalleryCacheSettings()
		{
			this.EnableDataStoreStorage = false;
			this.EnableDirectLinks = false;
			this.EnableFileSystemStorage = true;
			this.Extensions = "all";
			this.FileSystemStorageLocation = "~/photos/cache";
		}


		public override void DeleteAttachmentFromDisk(int postID)
		{
			GalleryPost p = GalleryPosts.GetPicture(postID);
			DeleteAttachmentFromDisk(p);
		}

		/// <summary>
		/// Override the base due to multiple files per post due to sizes
		/// </summary>
		/// <param name="p"></param>
		public override void DeleteAttachmentFromDisk(Post p)
		{
			throw new NotImplementedException();
		}

		#region Cache Cleanup


		public void LegacyCleanStorage(int SectionID)
		{
			// Delete all of the files except those which might be thumbnails or large thumbnails
			CSContext csContext = CSContext.Current;
			FileInfo[] files = (new DirectoryInfo(CalculateFileSystemStorageLocation())).GetFiles(csContext.SiteSettings.SettingsID + "." + SectionID + ".*");
			foreach(FileInfo file in files)
			{
				string filename = file.Name.Replace(csContext.SiteSettings.SettingsID + "." + SectionID + ".", "").Replace(file.Extension, "");
				string[] parts = filename.Split('.', 'x');
				string type = string.Empty;
				if(parts.Length > 1)
					type = parts[parts.Length - 1];
				if((type != "thumb") && (type != "secondarythumb") && (type != "watermark"))
					file.Delete();
			}
		}

		/// <summary>
		/// Deletes the post from the cache folder based on CS2.0 and prior naming
		/// </summary>
		/// <param name="SectionID"></param>
		/// <param name="PostID"></param>
		public void LegacyClearStorage(int SectionID , int PostID)
		{
			FileInfo[] files = (new DirectoryInfo(CalculateFileSystemStorageLocation())).GetFiles(CSContext.Current.SettingsID + "." + SectionID + "." + PostID + ".*");
			foreach(FileInfo file in files)
				file.Delete();
		}

		public void LegacyClearStorage(int SectionID)
		{
			// Delete all of the files
			CSContext csContext = CSContext.Current;
			FileInfo[] files = (new DirectoryInfo(CalculateFileSystemStorageLocation())).GetFiles(csContext.SettingsID + "." + SectionID + ".*");
			foreach(FileInfo file in files)
			{
				string filename = file.Name.Replace(csContext.SettingsID + "." + SectionID + ".", "").Replace(file.Extension, "");
				string[] parts = filename.Split('.', 'x');
				string type = string.Empty;
				if(parts.Length > 1)
					type = parts[parts.Length - 1];
				if(type != "watermark")
					file.Delete();
			}
		}

		#endregion

		#region File Names

		#region Watermark

		public string GetLocalWatermarkFileName(int sectionID)
		{
			return Path.Combine(CalculateFileSystemStorageLocation(),GetWatermarkFileName(sectionID)) ;
		}

		private string GetWatermarkFileName(int sectionID)
		{
			return string.Format("{0}.{1}.watermark.jpg", CSContext.Current.SettingsID, sectionID) ;
		}

		#endregion

		#region Secondary Thumbnail

		public string GetLocalSecondaryThumbnailFileName(GalleryPost post)
		{
			return Path.Combine(CalculateFileSystemStorageLocation(), GetSecondaryThumbnailFileName(post));
		}
		public string GetSecondaryThumbnailFileName(GalleryPost post)
		{
			return string.Format("{0}.{1}.{2}.{3}.secondarythumb.jpg", post.Section.SettingsID, post.Gallery.SectionID, post.PostID,  Encode(post.Attachment.FileName)); 
		}

		#endregion

		#region Thumbnail

		public string GetLocalThumbnailFileName(GalleryPost post)
		{
			return Path.Combine(CalculateFileSystemStorageLocation(), GetThumbnailFileName(post));
		}
		public string GetThumbnailFileName(GalleryPost post)
		{
			return string.Format("{0}.{1}.{2}.{3}.thumb.jpg", post.Section.SettingsID, post.SectionID, post.PostID,  Encode(post.Attachment.FileName));
		}

		#endregion

		#region GetFileName

		protected override string GetFileName()
		{
			CSContext csContext = CSContext.Current;
			Gallery g = Galleries.GetGallery(csContext.ApplicationKey) ;
			return GetHiddenFileName(g.SectionID,csContext.PostID);
		}


		public string GetLocalFileName(GalleryPost post, GalleryImageSettings settings)
		{
			switch(settings.GalleryImageType)
			{
				case GalleryImageType.Thumbnail:
					return GetLocalThumbnailFileName(post);
				case GalleryImageType.SecondaryThumbnail:
					return GetLocalSecondaryThumbnailFileName(post);
				default:
					return GetLocalFileName(post, settings.Width, settings.Height);
			}
			
		}

		public string GetLocalFileName(GalleryPost post, int width, int height)
		{
			return Path.Combine(CalculateFileSystemStorageLocation(), GetFileName(post, width, height));
		}
		public string GetFileName(GalleryPost post, int width, int height)
		{
			return string.Format("{0}.{1}.{2}.{3}x{4}.{5}.jpg", post.Section.SettingsID, post.SectionID , post.PostID, width ,height,  Encode(post.Attachment.FileName));
		}
		
		/// <summary>
		/// Gives you a valid file name to write to the cache (used for temp attachements)
		/// </summary>
		public string GetLocalFileName(int sectionID, string fileName)
		{
			return Path.Combine(CalculateFileSystemStorageLocation(),GetFileName(sectionID, fileName)) ;
		}
		public string GetFileName(int sectionID, string fileName)
		{
			return string.Format("{0}.{1}",sectionID  , Encode(fileName));
		}

		#endregion
		
		#endregion
	}
}
