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

using System.Collections;
using System.Text.RegularExpressions;

namespace CommunityServer.Components
{
    public class CSUrlReWriter : UrlReWriteProvider
    {
        private static Regex ReWriteFilter = null;

        static CSUrlReWriter()
        {
            //ReWriterFilter enables us to mark sections of the SiteUrls.config as non rewritable. This is particularly helpful
            //when moving the weblogs and gallery to the root of the site. Otherwise, there is a very likely chance /user /admin /utility
            // /search will be mistaken treated as a blog or gallery.
            ReWriteFilter = new Regex(SiteUrls.Instance().LocationFilter, RegexOptions.IgnoreCase|RegexOptions.Compiled);
        }


        public override string GetRewrittenUrl(string path, string queryString)
        {
            string newPath = null;
            //If we do not match a filter, continue one
            if(!ReWriteFilter.IsMatch(path))
            {
                Location location = SiteUrls.Instance().LocationSet.FindLocationByPath(path);
                if(location != null)
                {
                    newPath = location.ReWriteUrl(path,queryString);
                }
            }

            return newPath;
        }

        public override void RewriteUrl(System.Web.HttpContext context, string path, string queryString)
        {
            context.RewritePath(path, null, queryString);
        }
    }
}