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

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

namespace CommunityServer.Galleries.Controls
{
	public class BaseGalleryLink : CommunityServer.Controls.ListItemLink
	{

		#region Public Properties

	    private int _sectionID = 0;
        public int SectionID
        {
            get{return _sectionID;}
            set{_sectionID = value;}
        }

        private string _appKey = null;
        public string ApplicationKey
        {
            get{return _appKey;}
            set{_appKey = value;}
        }

		#endregion

        protected Gallery GetGallery()
        {
            Gallery gallery = Galleries.GetGallery() ;

            if(gallery == null)
                this.Visible = false;

            return gallery;
        }
	}

    public class ContactLink : BaseGalleryLink
    {
        protected override void OnPreRender(EventArgs e)
        {
            Gallery gallery = GetGallery();
            if(gallery != null)
            {
                if(gallery.EnableContact)
                {
                    this.NavigateUrl = GalleryUrls.Instance().ContactForm(gallery.ApplicationKey);
                    this.Text = ResourceManager.GetString("Gallery_Links_Contact");
                }
                else
                {
                    this.Visible = false;
                }
            }

            base.OnPreRender (e);
        }
    }

    public class AboutLink : BaseGalleryLink
    {
        protected override void OnPreRender(EventArgs e)
        {
			Gallery gallery = GetGallery();
			if(gallery != null)
            {
                if(gallery.EnableAbout)
                {
                    this.NavigateUrl = GalleryUrls.Instance().About(gallery.ApplicationKey);
                    this.Text = ResourceManager.GetString("Gallery_Links_About");
                }
                else
                {
                    this.Visible = false;
                }
            }

            base.OnPreRender (e);
        }
    }

    public class RssLink : BaseGalleryLink
    {

		protected bool IsAvailableToPublic(Gallery gallery)
		{
			return Permissions.ValidatePermissions(gallery, Permission.View, Users.GetAnonymousUser(true));
		}
		
		protected override void OnPreRender(EventArgs e)
        {
			CSContext context = CSContext.Current;
			Gallery gallery = GetGallery();
			if (gallery != null)
            {
				if (gallery.EnableRss && (IsAvailableToPublic(gallery) || context.SiteSettings.EnableSecureSyndication))
                {
					if (IsAvailableToPublic(gallery))
						this.NavigateUrl = GalleryUrls.Instance().Rss(RssType.Pictures, gallery.ApplicationKey);
					else
						this.NavigateUrl = SiteUrls.FormatUrlWithUserNameToken(GalleryUrls.Instance().Rss(RssType.Pictures, gallery.ApplicationKey), context.User, false);
					
					this.Text = ResourceManager.GetString("Gallery_Links_Rss");
                }
                else
                {
                    this.Visible = false;
                }
            }

            base.OnPreRender (e);
        }
    }

	public class EmailLink : BaseGalleryLink
    {
        protected override void OnPreRender(EventArgs e)
        {
			Gallery gallery = GetGallery();
			if(gallery != null)
            {
                if(Context.Request.IsAuthenticated)
                {
                    this.NavigateUrl = GalleryUrls.Instance().EmailSubscriptions(gallery.ApplicationKey);
                    this.Text = ResourceManager.GetString("Gallery_Links_Email");
                }
                else
                    this.Visible = false;
            }

            base.OnPreRender (e);
        }
    }

    public class HomeLink : BaseGalleryLink
    {
        protected override void OnPreRender(EventArgs e)
        {
			Gallery gallery = GetGallery();
			if(gallery != null)
            {
                this.NavigateUrl = gallery.Url;
                this.Text = ResourceManager.GetString("Gallery_Links_Home");
            }
            base.OnPreRender (e);
        }
    }
}
