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

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

namespace CommunityServer.Galleries.Controls
{
    /// <summary>
    /// Provides the Gallery implementation of SectionRepeater
    /// </summary>
    public class GalleryRepeater : SectionRepeater
    {
		
		#region Public Properties

		[DefaultValue( 1 )]
		public virtual Int32 CurrentPage
		{
			get
			{
				Object state = ViewState["CurrentPage"];
				if(state != null)
					return (Int32)state;
				return 1;
			}
			set
			{ ViewState["CurrentPage"] = value; }
		}

		[DefaultValue( 10 )]
		public virtual Int32 ItemsPerPage
		{
			get
			{
				Object state = ViewState["ItemsPerPage"];
				if(state != null)
					return (Int32)state;
				return 10;
			}
			set
			{ ViewState["ItemsPerPage"] = value; }
		}

		[DefaultValue( 0 )]
		public virtual Int32 TotalGalleries
		{
			get
			{
				Object state = ViewState["TotalGalleries"];
				if(state != null)
					return (Int32)state;
				return 0;
			}
			set
			{ ViewState["TotalGalleries"] = value; }
		}

		[DefaultValue( false )]
		public virtual bool Paginate
		{
			get
			{
				Object state = ViewState["Paginate"];
				if(state != null)
					return (bool)state;
				return false;
			}
			set
			{ ViewState["Paginate"] = value; }
		}

		#endregion


        /// <summary>
        /// Returns the Galleries for the current GroupID
        /// </summary>
        /// <returns></returns>
        protected override ArrayList GetSections()
        {
			ArrayList galleries = Galleries.GetGalleriesByGroupID(GroupID, IgnorePermissions, true, FlushSections);

			if(Paginate)
			{
				// Calculate pagination
				TotalGalleries = galleries.Count;
				int pageCount = galleries.Count;
				pageCount -= (CurrentPage-1)*ItemsPerPage;
				if(pageCount > ItemsPerPage)
					pageCount = ItemsPerPage;

				// Grab the items for the current page
				Gallery[] pageItems = new Gallery[pageCount];
				galleries.CopyTo((CurrentPage-1)*ItemsPerPage, pageItems, 0, pageCount);
				return new ArrayList(pageItems);
			}
			else
				return galleries;
        }

    }
}