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

using System;
using System.Web.Caching;

namespace CommunityServer.Components
{
	/// <summary>
	/// Summary description for PostAttachments.
	/// </summary>
	public class PostAttachments
	{
		private PostAttachments()
		{
		
		}

        public static PostAttachmentMetaData GetPostAttachtmentMetaData(int postID)
        {
            return GetPostAttachtmentMetaData(postID,true);
        }

        public static PostAttachmentMetaData GetPostAttachtmentMetaData(int postID, bool cacheable)
        {
            PostAttachmentMetaData pmd = null;
            if(cacheable)
                pmd = CSCache.Get("PostAttachmentMetaData-" + postID.ToString()) as PostAttachmentMetaData;

            if(pmd == null)
            {
                CommonDataProvider dp = CommonDataProvider.Instance();
                pmd =  dp.GetPostAttachment(postID,true);

                if(cacheable && pmd != null)
                    CSCache.Insert("PostAttachmentMetaData-" + postID.ToString(),pmd,CSCache.MinuteFactor*3);
            }

            return pmd;
          
        }

        public static PostAttachment GetPostAttachtment(int postID)
        {
            return GetPostAttachtment(postID,true);
        }

        public static PostAttachment GetPostAttachtment(int postID, bool cacheable)
        {
            PostAttachment pa = null;

            if(cacheable)
                pa = CSCache.Get("PostAttachment-" + postID.ToString()) as PostAttachment;

            if(pa == null)
            {
                // Create Instance of the CommonDataProvider
                CommonDataProvider dp = CommonDataProvider.Instance();
                pa = dp.GetPostAttachment(postID,false);

                if(cacheable && pa != null)
                    CSCache.Insert("PostAttachment-" + postID.ToString(), pa, CSCache.MinuteFactor*1,CacheItemPriority.Low);
            }

            return pa;
        }

        public static void Add(Post post, PostAttachment attachment)
        {
            CommonDataProvider dp = CommonDataProvider.Instance();
            dp.AddPostAttachment(post,attachment);
			CSCache.Remove("PostAttachment-" + post.PostID.ToString());
			CSCache.Remove("PostAttachmentMetaData-" + post.PostID.ToString());
        }

        public static void AddTemporary(Guid temporaryID, PostAttachment attachment)
        {
            CommonDataProvider dp = CommonDataProvider.Instance();
            dp.AddTemporaryPostAttachment(temporaryID,attachment);
        }

		public static PostAttachment ToggleTemporary(Guid temporaryID, string name, bool clearContent, int postID, int sectionID, int userID)
		{
			return ToggleTemporary(temporaryID,name,clearContent,postID,sectionID,userID, 0, 0);
		}
		public static PostAttachment ToggleTemporary(Guid temporaryID, string name, bool clearContent, int postID, int sectionID, int userID, int height,int width)
		{
			return ToggleTemporary(temporaryID,name,clearContent,postID,sectionID,userID, height, width, false);
		}	
		public static PostAttachment ToggleTemporary(Guid temporaryID, string name, bool clearContent, int postID, int sectionID, int userID, int height,int width, bool deleteTempOnly)
			{
            CommonDataProvider dp = CommonDataProvider.Instance();
            PostAttachment pa = dp.ToggleTemporaryPostAttachment(temporaryID,name,clearContent,postID,sectionID,userID, height, width, deleteTempOnly);
			CSCache.Remove("PostAttachment-" + postID.ToString());
			CSCache.Remove("PostAttachmentMetaData-" + postID.ToString());
			return pa;
        }

        public static void Delete(int postID)
        {
            CommonDataProvider dp = CommonDataProvider.Instance();
            dp.DeletePostAttachment(postID);
			CSCache.Remove("PostAttachment-" + postID.ToString());
			CSCache.Remove("PostAttachmentMetaData-" + postID.ToString());
        }
	}
}
