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

using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls 
{

    /// <summary>
    /// Summary description for Pager.
    /// </summary>
    public class ThreadPager : Label, INamingContainer 
    {


        protected override void Render(HtmlTextWriter writer) 
        {
            int totalPages = CalculateTotalPages();

            // Do we have data?
            //
            if (totalPages <= 1)
                return;

            // TODO: Move HTML out of here!
            //
            //            writer.Write("<br>");
			writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass, false);
            AddAttributesToRender(writer);
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            

            writer.Write(CommunityServer.Components.ResourceManager.GetString("Utility_ThreadPager_Page"));

            // Render the paging buttons
            //
            RenderPagingButtons(writer, totalPages);

            writer.RenderEndTag();

        }

        void RenderButtonRange(int start, int end, HtmlTextWriter writer) 
        {

            for (int i = start; i < end; i++) 
            {

                HtmlAnchor anchor = new HtmlAnchor();

                anchor.HRef = ForumUrls.Instance().PostPaged(ThreadID, (i + 1) );
                anchor.InnerText = (i + 1).ToString();

                anchor.RenderControl(writer);

                if (i != (end - 1))
                    writer.Write(", ");

            }

        }

        void RenderPagingButtons(HtmlTextWriter writer, int totalPages) 
        {

            // If we have < 8 pages display all the pages and exit
            //
            if ( totalPages <= 8) 
            {
                RenderButtonRange(0, totalPages, writer);
                return;
            } 
            else 
            {
                RenderButtonRange(0, 2, writer);
                writer.Write(" ... ");
                RenderButtonRange(totalPages - 3, totalPages, writer);
            }

        }


        // *********************************************************************
        //  CalculateTotalPages
        //
        /// <summary>
        /// Static that caculates the total pages available.
        /// </summary>
        /// 
        // ********************************************************************/
        private int CalculateTotalPages() 
        {
            int totalPagesAvailable;
            int pageSize = ForumConfiguration.Instance().PostsPerPage;

            if (Replies == 0)
                return 0;

            // First calculate the division
            //
            totalPagesAvailable = Replies / pageSize;

            // Now do a mod for any remainder
            //
            if ((Replies % pageSize) > 0)
                totalPagesAvailable++;

            return totalPagesAvailable;

        }


        [
        System.ComponentModel.DefaultValue( 0 ),
        ]
        public virtual Int32 Replies 
        {
            get 
            {
                Object state = ViewState["Replies"];
                if ( state != null ) 
                {
                    return (Int32)state;
                }
                return 0;
            }
            set 
            {
                // Increment by 1 to include the post
                ViewState["Replies"] = value + 1;
            }
        }


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

    }
}
