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

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

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

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

			Gallery g = GalleryLookUp();
			Permissions.AccessCheck(g,Permission.Post,CSContext.Current.User);
		
            SetLocation();
		}

        public void SetCookie(Gallery g)
        {
            if(g == null)
                return;

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

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

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

        protected virtual Gallery GalleryLookUp()
        {
            if(_gallery == null)
            {
                CSContext context = CSContext.Current;

                if(context.SectionID > 0)
                {
                    _gallery = CommunityServer.Galleries.Galleries.GetGallery(context.SectionID,true) ;
                    SetCookie(_gallery);
                }
				else
                {
                    HttpCookie cookie = context.Context.Request.Cookies[CreateApplicationCookieName(context,"pg")];
                    if(cookie != null)
                    {
                        if(!Globals.IsNullorEmpty(cookie.Values["GalleryID"]))
                        {
                            int gid = Int32.Parse(cookie.Values["GalleryID"]);
							try
							{
								_gallery = CommunityServer.Galleries.Galleries.GetGallery(gid, true); //ignore permissions = false);
							}
							catch{}
                        }
                    }
                }
						
                if(_gallery == null)
                {
                	ArrayList galleries = CommunityServer.Galleries.Galleries.GetGalleries(true,false); //ignore permissions = false);
					galleries = Sections.FilterByAccessControl(galleries,Permission.Post,context.User);

					//Lets keep them in the CP and show the error on the switch page
                    //if(galleries == null || galleries.Count == 0)
                    //    throw new CSException(CSExceptionType.SectionNotFound,"The gallery could not be found");

                    if(galleries != null && galleries.Count == 1)
                    {
                        _gallery = galleries[0] as Gallery;
                        SetCookie(_gallery);
                    }
                }

                if(_gallery == null)
                    Response.Redirect(GalleryUrls.Instance().ControlPanel_Photos_Switch_ReturnUrl(context.RawUrl));

                //throw new CSException(CSExceptionType.SectionNotFound,"The blog could not be found");
            }

            return _gallery;
        }

		private Gallery _gallery = null;
		public Gallery CurrentGallery
		{
			get{ return _gallery;}
		}

		public GalleryPostType FetchGalleryPostType
		{
			get
			{
				string gpt = Request.QueryString["gpt"];
				if(Globals.IsNullorEmpty(gpt))
					return GalleryPostType.Image;


				else
					return (GalleryPostType)Enum.Parse(typeof(GalleryPostType),gpt);
			}
		}

	}
}
