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

using System;
using System.Collections;
using System.Web;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;

namespace CommunityServer.ControlPanel.UI
{
	/// <summary>
	/// Summary description for BaseBlogPage.
	/// </summary>
	public class BaseBlogPage : PanelPage
	{
		public BaseBlogPage()
		{
		}

		protected override void Authorize()
		{
			base.Authorize ();

			Weblog w = WeblogLookUp();
			Permissions.AccessCheck(w,Permission.Post,CSContext.Current.User);

            SetLocation();
		}


        public void SetCookie(Weblog w)
        {
            if(w == null)
                return;

            CSContext context = CSContext.Current;
            HttpCookie cookie = context.Context.Request.Cookies[CreateApplicationCookieName(context,"weblog")];
            if(cookie == null)
            {
                cookie = new HttpCookie(CreateApplicationCookieName(context,"weblog")); 
                
            }

            cookie.Values["BlogID"] = w.SectionID.ToString();
            cookie.Expires = DateTime.Now.AddDays(30);

            context.Context.Response.Cookies.Add(cookie);
            
        }

        protected virtual Weblog WeblogLookUp()
        {
            if(_weblog == null)
            {
                CSContext context = CSContext.Current;

                if(context.SectionID > 0)
                {
                    _weblog = Weblogs.GetWeblog(context.SectionID,true,false);
                    SetCookie(_weblog);
                }
                else
                {
                    HttpCookie cookie = context.Context.Request.Cookies[CreateApplicationCookieName(context,"weblog")];
                    if(cookie != null)
                    {
                        if(!Globals.IsNullorEmpty(cookie.Values["BlogID"]))
                        {
                            int bid = Int32.Parse(cookie.Values["BlogID"]);
                            try
                            {
                                _weblog = Weblogs.GetWeblog(bid,true,false);
                            }
                            catch{}
                        }
                    }
                }
						
                if(_weblog == null)
                {
                    ArrayList blogs = Weblogs.GetWeblogs(true,false,false);
                    blogs = Sections.FilterByAccessControl(blogs,Permission.Post,context.User);

                    if(blogs != null && blogs.Count == 1)
                    {
                        _weblog = blogs[0] as Weblog;
                        SetCookie(_weblog);
                    }
                }

                if(_weblog == null)
                    Response.Redirect("switch.aspx");

            }

            return _weblog;
        }

		private Weblog _weblog = null;
		public Weblog CurrentWeblog
		{
			get{ return _weblog;}
		}

		public BlogPostType FetchBlogPostType
		{
			get
			{
				string bpt = Request.QueryString["BPT"];
				if(Globals.IsNullorEmpty(bpt))
					return BlogPostType.Post;
				else
					return (BlogPostType)Enum.Parse(typeof(BlogPostType),bpt);
			}
		}

	}
}
