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

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

namespace CommunityServer.Galleries.Components
{
	/// <summary>
	/// Summary description for CSGalleryPage.
	/// </summary>
	public class CSGalleryPage : CommunityServer.Components.CSFormPage
	{
		public CSGalleryPage()
		{
			GalleryConfig = GalleryConfiguration.Instance();
			appKey = CSContext.Current.ApplicationKey;
		}

        private GalleryConfiguration GalleryConfig;
		private string appKey;
      
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad (e);
            if(appKey != null)
            {
                Gallery gallery = Galleries.GetGallery(appKey,true);

                if(gallery != null)
                {
                    SetStyleSheets(gallery);
                    SetAutoSyndication(gallery);
                    SetMetaTags(gallery);
                }
            }
        }


        protected virtual void SetStyleSheets(Gallery gallery)
        {
            Head.AddStyle(CSSPath(gallery.Theme,"style.css"),"screen",Context);
            Head.AddStyle(CSSPath(gallery.Theme,"print.css"),"print",Context);

            if(gallery.SecondaryCSS != null)
                Head.AddStyle(CSSPath(gallery.Theme, gallery.SecondaryCSS),"screen",Context);

            if(GalleryConfig.EnableThemes && gallery.CSSOverride != null)
                Head.AddStyle(GalleryUrls.Instance().CustomCSS(gallery.ApplicationKey),"screen",Context);
        }

        protected string CSSPath(string theme, string fileName)
        {
            if(theme == null || !GalleryConfig.EnableThemes)
                theme = GalleryConfig.DefaultTheme;

            string path = string.Format("{0}{1}/style/{2}",GalleryConfig.ThemeLocation,theme,fileName);
            return this.ResolveUrl(path);
        }

        protected virtual void SetAutoSyndication(Gallery gallery)
        {
			CSContext context = CSContext.Current;
            GalleryUrls urls = GalleryUrls.Instance();
            string title = Globals.HtmlEncode(gallery.Name);

            if(gallery.EnableRss)
            {
				//categories RSS is added in the CategoryListing.cs
				
				string rssPic = Globals.FullPath(urls.Rss(RssType.Pictures, appKey));

				bool isAvailableToPublic = Permissions.ValidatePermissions(gallery, Permission.View, Users.GetAnonymousUser(true));
				if (isAvailableToPublic)
					Head.AddRssAutoDiscovery(title, rssPic, Context);
				else if (context.SiteSettings.EnableSecureSyndication)
					Head.AddRssAutoDiscovery(title, SiteUrls.FormatUrlWithUserNameToken(rssPic, context.User, false), Context);
				
			}

//            if(gallery.EnableAtom)
//            {
//                string atom = Globals.FullPath(bu.Atom(cntx.ApplicationKey));
//                Head.AddAtomDiscovery(title,atom,Context);
//            }
        }

        protected virtual void SetMetaTags(Gallery gallery)
        {
            
        }
	}
}
