using Elmah;
using Sevomin.Models;
using Sevomin.WebFrontend.Controllers;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Sevomin.WebFrontend
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }

        void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
        {
            if ((e.Exception.GetBaseException() is HttpException
                && ((HttpException)(e.Exception.GetBaseException())).GetHttpCode() == 404
                && e.Context is HttpContext)
                || ((HttpContext)e.Context).Request.Url.ToString().ToLower().Contains(@"favicon.ico".ToLower()))
            {
                e.Dismiss();
            }
        }

        protected void Application_EndRequest()
        {
            if (Context.Response.StatusCode == 404)
            {
                Response.Clear();

                var rd = new RouteData();                
                rd.Values["controller"] = "Base";
                rd.Values["action"] = "Error404";

                IController c = new BaseController();
                c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
            }
        }
    }
}
