using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

using BrnShop.Core;
using BrnShop.Services;
using BrnShop.Web.Framework;

namespace BrnShop.Web
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute("Category",
                            "list/{filterAttr}-{cateId}-{brandId}-{filterPrice}-{onlyStock}-{sortColumn}-{sortDirection}-{page}.html",
                            new { controller = "Catalog", action = "Category" },
                            new[] { "BrnShop.Web.Controllers" });
            routes.MapRoute("ShortCategory",
                            "list/{cateId}.html",
                            new { controller = "Catalog", action = "Category" },
                            new[] { "BrnShop.Web.Controllers" });
            routes.MapRoute("Product",
                            "{pid}.html",
                            new { controller = "Catalog", action = "Product" },
                            new[] { "BrnShop.Web.Controllers" });
            routes.MapRoute("Search",
                            "search",
                            new { controller = "Catalog", action = "Search" },
                            new[] { "BrnShop.Web.Controllers" });
            routes.MapRoute("Default", 
                            "{controller}/{action}", 
                            new { controller = "Home", action = "Index" },
                            new[] { "BrnShop.Web.Controllers" });
        }
        
        protected void Application_Start()
        {
            //将默认视图引擎替换为ThemeRazorViewEngine引擎
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new ThemeRazorViewEngine());

            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);

            //启动事件机制
            BSPEvent.Start();
            //服务器宕机启动后重置在线用户表
            if (Environment.TickCount > 0 && Environment.TickCount < 900000)
                OnlineUsers.ResetOnlineUserTable();
        }
    }
}