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

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

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

namespace CommunityServer.Discussions.Controls 
{

    public class MarkAllRead : Button 
    {
        CSContext csContext = CSContext.Current;

        public MarkAllRead() 
        {
            Text = ResourceManager.GetString("MarkAllRead_Threads");
            Click += new EventHandler(MarkAllRead_Click);
        }


        // *********************************************************************
        //  CreateChildControls
        //
        /// <summary>
        /// This event handler adds the children controls.
        /// </summary>
        // ***********************************************************************/
        protected override void Render(HtmlTextWriter writer) 
        {

            // If the user is already authenticated we have no work to do
            if (!Page.Request.IsAuthenticated)
                return;

            if (csContext.SectionID < 0)
                return;

            base.Render(writer);

        }

        public void MarkAllRead_Click(Object sender, EventArgs e) 
        {

            // Mark all fourms as read
            //
			if ((csContext.GroupID == 0) && (csContext.SectionID == 0) )
				Forums.MarkAllForumsRead(csContext.User.UserID, 0, 0, false);

				// Mark all forums in a particular forum group as read
				//
			else if ( (csContext.GroupID > 0) && (csContext.SectionID == 0) )
				Forums.MarkAllForumsRead(csContext.User.UserID, csContext.GroupID, 0, false);


				// Mark all threads in a forum as read
				//
			else if ( csContext.SectionID > 0 )
			{
				Forums.MarkAllForumsRead(csContext.User.UserID, 0, csContext.SectionID, true);

				// Flush the current threads read status
				Threads.GetReadThreads(csContext.SectionID, csContext.User.UserID, true);
			}

			// Flush the sections read status
			Sections.GetSectionsRead(csContext.User.UserID, true);
			Page.DataBind();
        }

    }
}
