﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Http;
using Izenda.AdHoc;
using System.Configuration;
using System.Web.Compilation;

namespace IzendaHours
{

        public class SpecificFileRouterConstraint : IRouteConstraint
        {
            private string extensionToBeRouted = null;
            private string fileToBeRouted = null;

            public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
            {
                return !String.IsNullOrEmpty(extensionToBeRouted) && !String.IsNullOrEmpty(fileToBeRouted) && values[extensionToBeRouted] != null && values[extensionToBeRouted].ToString().ToLower().Contains(fileToBeRouted);
            }

            public SpecificFileRouterConstraint() { }

            public SpecificFileRouterConstraint(string extension, string fileName)
            {
                extensionToBeRouted = extension.ToLower();
                fileToBeRouted = fileName.ToLower();
            }
        }

        public class IzendaResourceConstraint : IRouteConstraint
        {
            private string extensionToBeRouted = null;

            public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
            {
                if (String.IsNullOrEmpty(extensionToBeRouted) || values[extensionToBeRouted] == null)
                {
                    return false;
                }
                string requestUrl = values[extensionToBeRouted].ToString().ToLower();
                bool result = false;
                if (extensionToBeRouted == "js")
                {
                    result = result || requestUrl.Contains("reportviewerfilters.js");
                    result = result || requestUrl.Contains("reportlist.js");
                    result = result || requestUrl.Contains("data-sources.js");
                    result = result || requestUrl.Contains("data-sources-preview.js");
                    result = result || requestUrl.Contains("charts.js");
                    result = result || requestUrl.Contains("datasources-search.js");
                    result = result || requestUrl.Contains("jquery-1.6.1.min.js");
                    result = result || requestUrl.Contains("jquery-ui-1.8.13.custom.min.js");
                    result = result || requestUrl.Contains("elrte.full.js");
                    result = result || requestUrl.Contains("elrte.ru.js");
                    result = result || requestUrl.Contains("fieldproperties.js");
                    result = result || requestUrl.Contains("shrinkable-grid.js");
                }
                if (extensionToBeRouted == "css")
                {
                    result = result || requestUrl.Contains("tabs.css");
                    result = result || requestUrl.Contains("report.css");
                    result = result || requestUrl.Contains("filters.css");
                    result = result || requestUrl.Contains("base.css");
                    result = result || requestUrl.Contains("report-list-modal.css");
                    result = result || requestUrl.Contains("data-sources.css");
                    result = result || requestUrl.Contains("charts.css");
                    result = result || requestUrl.Contains("jquery-ui-1.8.13.custom.css");
                    result = result || requestUrl.Contains("elrte.min.css");
                    result = result || requestUrl.Contains("elrte-inner.css");
                    result = result || requestUrl.Contains("shrinkable-grid.css");
                }
                if (extensionToBeRouted == "png")
                {
                    result = result || requestUrl.Contains("elrtebg.png");
                    result = result || requestUrl.Contains("elrte-toolbar.png");
                }
                if (extensionToBeRouted == "gif")
                {
                    result = result || requestUrl.Contains("elrte/images/pixel.gif");
                }
                return result;
            }

            public IzendaResourceConstraint() { }

            public IzendaResourceConstraint(string extension)
            {
                extensionToBeRouted = extension.ToLower();
            }
        }
        public class MvcApplication : System.Web.HttpApplication
        {
            void Application_AcquireRequestState(object sender, EventArgs e)
            {
                IzendaHours.CustomAdHocConfig.InitializeReporting();
            }
            protected void Application_Start()
            {

                RouteTable.Routes.MapPageRoute("rs.aspx", "{*aspx}", "~/Reporting/rs.aspx", false, null, new RouteValueDictionary { { "aspx", new SpecificFileRouterConstraint("aspx", "rs.aspx") } });
                AreaRegistration.RegisterAllAreas();
                        GlobalConfiguration.Configure(WebApiConfig.Register);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
                routes.MapRoute("IzendaResources", "Resources/{*resource}", new { controller = "IzendaStaticResources", action = "Index" });
                routes.MapRoute("IzendaJsResources", "{*js}", new { controller = "IzendaStaticResources", action = "Index" }, new { irc = new IzendaResourceConstraint("js") });
                routes.MapRoute("IzendaCssResources", "{*css}", new { controller = "IzendaStaticResources", action = "Index" }, new { irc = new IzendaResourceConstraint("css") });
                routes.MapRoute("IzendaPngResources", "{*png}", new { controller = "IzendaStaticResources", action = "Index" }, new { irc = new IzendaResourceConstraint("png") });
                routes.MapRoute("IzendaGifResources", "{*gif}", new { controller = "IzendaStaticResources", action = "Index" }, new { irc = new IzendaResourceConstraint("gif") });
                routes.MapRoute("IzendaReporting", "{controller}/{action}/{id}", new { controller = "Reporting", id = UrlParameter.Optional });
                routes.MapRoute("StarterKitDefault", "{controller}/{action}/{id}", new { controller = "Reporting", action = "ReportList", id = UrlParameter.Optional });
                routes.MapRoute("HomeDefault", "{*pathInfo}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
            }
        }
    }

