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

using System;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;

namespace CommunityServer.Blogs.Controls
{
	public class BaseBlogLink : CommunityServer.Controls.ListItemLink
	{
	    private int _blogID = 0;
        public int BlogID
        {
            get{return _blogID;}
            set{_blogID = value;}
        }

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

        protected Weblog GetWeblog()
        {
            Weblog weblog = null;

            if(BlogID > 0)
            {
                weblog = Weblogs.GetWeblog(BlogID);
            }
            else if(!Globals.IsNullorEmpty(this.ApplicationKey))
            {
                weblog= Weblogs.GetWeblog(this.ApplicationKey);
            }
            else
            {
                weblog =  Weblogs.GetWeblog(CSContext.Current.ApplicationKey);
            }

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

            return weblog;
        }
	}

	public abstract class BaseBlogSyndicationLink : BaseBlogLink
	{

		protected bool IsAvailableToPublic(Weblog blog)
		{
			return Permissions.ValidatePermissions(blog, Permission.View, Users.GetAnonymousUser(true));
		}

		protected bool IsExternalFeed(Weblog blog)
		{
			return !Globals.IsNullorEmpty(blog.ExternalFeedUrl);
		}

		protected bool SetUrl(Weblog blog, string feedUrl)
		{
			CSContext context = CSContext.Current;
			if (IsExternalFeed(blog))
			{
				this.NavigateUrl = blog.ExternalFeedUrl;
			}
			else
			{
				if (IsAvailableToPublic(blog))
					this.NavigateUrl = feedUrl;
				else if (context.SiteSettings.EnableSecureSyndication)
					this.NavigateUrl = SiteUrls.FormatUrlWithUserNameToken(feedUrl, context.User, false);
				else
					return false;
			}

			return true;
		}

	}

    public class ContactLink : BaseBlogLink
    {
        protected override void OnLoad(EventArgs e)
        {
            Weblog blog = GetWeblog();
            if(blog != null)
            {
                if(blog.ShowContact)
                {
                    this.NavigateUrl = BlogUrls.Instance().ContactForm(blog.ApplicationKey);
                    this.Text = ResourceManager.GetString("Weblog_SubscriptionList_Contact");
                }
                else
                {
                    this.Visible = false;
                }
            }

            base.OnLoad (e);
        }
    }

    public class AboutLink : BaseBlogLink
    {
        protected override void OnLoad(EventArgs e)
        {
            Weblog blog = GetWeblog();
            if(blog != null)
            {
                if(blog.EnableAbout)
                {
                    this.NavigateUrl = BlogUrls.Instance().About(blog.ApplicationKey);
                    this.Text = ResourceManager.GetString("Weblog_BlogLinks_About");
                }
                else
                {
                    this.Visible = false;
                }
            }

            base.OnLoad (e);
        }
    }

	public class RssLink : BaseBlogSyndicationLink
    {
        protected override void OnLoad(EventArgs e)
        {
            Weblog blog = GetWeblog();
            if(blog != null)
            {
				if (blog.EnableRss && SetUrl(blog, BlogUrls.Instance().Rss(blog.ApplicationKey)))
					this.Text = ResourceManager.GetString("Weblog_SubscriptionList_Rss");
				else
                    this.Visible = false;
            }

            base.OnLoad (e);
        }
    }

	public class AtomLink : BaseBlogSyndicationLink
    {
        protected override void OnLoad(EventArgs e)
        {
            Weblog blog = GetWeblog();
            if(blog != null)
            {
				if (blog.EnableAtom && SetUrl(blog, BlogUrls.Instance().Atom(blog.ApplicationKey)))
					this.Text = ResourceManager.GetString("Weblog_SubscriptionList_Atom");
				else
					this.Visible = false;
            }

            base.OnLoad (e);
        }
    }

    public class EmailLink : BaseBlogLink
    {
        protected override void OnLoad(EventArgs e)
        {
            Weblog blog = GetWeblog();
            if(blog != null)
            {
                if(Context.Request.IsAuthenticated)
                {
                    this.NavigateUrl = BlogUrls.Instance().EmailSubscriptions(blog.ApplicationKey);
                    this.Text = ResourceManager.GetString("Weblog_SubscriptionList_Email");
                }
                else
                    this.Visible = false;
            }

            base.OnLoad (e);
        }
    }

	public class ControlPanelLink : BaseBlogLink
	{
		protected override void OnLoad(EventArgs e)
		{
			Weblog blog = GetWeblog();
			if(blog != null)
			{
				if(Context.Request.IsAuthenticated && Permissions.ValidatePermissions(blog,Permission.Post,CSContext.Current.User))
				{
					this.NavigateUrl = BlogUrls.Instance().PostControlPanel(blog);
					this.Text = ResourceManager.GetString("Weblog_Quick_Manage");
				}
				else
					this.Visible = false;
			}

			base.OnLoad (e);
		}
	}

    public class ArchiveLink : BaseBlogLink
    {
        protected override void OnLoad(EventArgs e)
        {
            Weblog blog = GetWeblog();
            if(blog != null)
            {
                    this.NavigateUrl = BlogUrls.Instance().Archive(blog.ApplicationKey);
                    this.Text = ResourceManager.GetString("Weblog_BlogLinks_Archive");
            }
            base.OnLoad (e);
        }
    }

    public class LinkListLink : BaseBlogLink
    {
        protected override void OnLoad(EventArgs e)
        {
            Weblog blog = GetWeblog();
            if(blog != null)
            {
                this.NavigateUrl = BlogUrls.Instance().LinkList(blog.ApplicationKey);
                this.Text = ResourceManager.GetString("Weblog_BlogLinks_LinkList");
            }
            base.OnLoad (e);
        }
    }

    public class HomeLink : BaseBlogLink
    {
        protected override void OnLoad(EventArgs e)
        {
            Weblog blog = GetWeblog();
            if(blog != null)
            {
                this.NavigateUrl = blog.Url;
                this.Text = ResourceManager.GetString("Weblog_BlogLinks_Home");
            }
            base.OnLoad (e);
        }
    }
}
