﻿using AutoMapper;
using RestCMS.WebAPI.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace RestCMS.WebAPI
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            MappingConfiguration.SetMappings();

            // I'd rather use attribute routing, but Microsoft doesn't let you control routing order across controllers (only within a controller)
            // So for now, it has to be done here.

            config.Routes.MapHttpRoute(
                name: "Pages Field Values",
                routeTemplate: "pages/{id}/fieldvalues",
                defaults: new { controller = "Pages", action = "FieldValues" }
            );

            config.Routes.MapHttpRoute(
                name: "Pages",
                routeTemplate: "pages/{id}",
                defaults: new { controller = "Pages", id = RouteParameter.Optional },
                constraints: new { id = @"\d*" }
            );

            config.Routes.MapHttpRoute(
                name: "Sites",
                routeTemplate: "sites/{id}",
                defaults: new { controller = "Pages", id = RouteParameter.Optional },
                constraints: new {id = Constants.RegularExpressions.Guid}
            );

            config.Routes.MapHttpRoute(
                name: "TempFiles",
                routeTemplate: "tempfiles",
                defaults: new { controller = "TempFiles" }
            );
            
            config.Routes.MapHttpRoute(
                name: "User Defined Entities",
                routeTemplate: "{userdefinedentity}/{id}",
                defaults: new { controller = "Entities", id = RouteParameter.Optional },
                constraints: new { id = @"\d*" }
            );

            // Web API routes
            config.MapHttpAttributeRoutes();
        }
    }
}
