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

using System;
using System.IO;
using System.Web.UI;

namespace CommunityServer.Components
{
	/// <summary>
	/// Default Page class for Community Server. Enables Action attribute on the forum to be overriden.
	/// </summary>
	public class CSPage : Page
	{
		public CSPage()
		{
		}

//        protected override void OnInit(EventArgs e)
//        {
//            base.OnInit (e);
//            CSContext cntx = CSContext.Current;
//            if(!cntx.IsAuthenticated)
//            {
//                if(cntx.SiteSettings.EnableAnonymousUserTracking)
//                    Users.TrackAnonymousUsers();
//            }
//            else
//            {
//                UsersOnline.IncludeMember(cntx.User);
//
//            }
//        }

        private bool _processed = false;
        private bool _isCallback = false;

        public bool IsCallBack
        {
            get
            {
                if(!_processed)
                {
                    _isCallback = AjaxManager.IsCallBack;
                    if(!_isCallback)
                    {
                        foreach(string key in Request.QueryString.AllKeys)
                        {
                            if(key.IndexOf("Callback") > -1)
                            {
                                _isCallback = true;
                                break;
                            }
                        }
                    }
                    _processed = true;
                }

                return _isCallback;
            }
        }


        #region Render

        /// <summary>
        ///  We override Render to swap out the default HtmlTextWriter for our own. Our own Writer's sole purpose is to 
        /// change the the action attribute on the form tag to the vanity Url. This way all postbacks occur on the vanity url
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {

            if (CSContext.Current.IsUrlReWritten)
            {
                 if (writer is System.Web.UI.Html32TextWriter)
                {
                    writer = new FormFixerHtml32TextWriter(writer.InnerWriter);
                }
                else
                {
                    writer = new FormFixerHtmlTextWriter(writer.InnerWriter);
                }
            }

            base.Render(writer);
        }
        #endregion


	}

    #region FormFixers

    #region FormFixerHtml32TextWriter
    internal class FormFixerHtml32TextWriter : System.Web.UI.Html32TextWriter
    {
        private string _url;

        internal FormFixerHtml32TextWriter(TextWriter writer):base(writer)
        {
            _url = CSContext.Current.RawUrl;
        }

        public override void WriteAttribute(string name, string value, bool encode)
        {
            if (_url != null && string.Compare(name, "action", true) == 0)
            {
                value = _url;
            }
            base.WriteAttribute(name, value, encode);
        }
    }
    #endregion
	
    #region FormFixerHtmlTextWriter
    internal class FormFixerHtmlTextWriter : System.Web.UI.HtmlTextWriter
    {
        private string _url;
        internal FormFixerHtmlTextWriter(TextWriter writer):base(writer)
        {

           _url = CSContext.Current.RawUrl;
        }

        public override void WriteAttribute(string name, string value, bool encode)
        {
            if (_url != null && string.Compare(name, "action", true) == 0)
            {
                value = _url;
            }

            base.WriteAttribute(name, value, encode);
        }
    }
    #endregion

    #endregion
}
