using Owin;
using Swashbuckle.Application;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http.Formatting;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using Unity;
using System.Web.Http.Cors;
using Microsoft.Owin;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace FLY.Thick.FilmCasting.WebAPI
{
    public class Startup
    {
        IUnityContainer container;

        public Startup(IUnityContainer container)
        {
            this.container = container;
        }

        public void Configuration(IAppBuilder builder)
        {
            HttpConfiguration config = new HttpConfiguration();

            //跨域配置
            config.EnableCors(new EnableCorsAttribute("*", "*", "*"));


            //设置路由
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}",
                defaults: new { id = RouteParameter.Optional }
            );

            //设置 Swagger， 这个API 说明书
            config
                .EnableSwagger(c => {
                    #region 配置API文档说明
                    var basePath = AppContext.BaseDirectory;
                    c.IncludeXmlComments(Path.Combine(basePath, "FLY.Thick.FilmCasting.WebAPI.xml"));
                    c.IncludeXmlComments(Path.Combine(basePath, "FLY.Thick.FilmCasting.xml"));
                    c.IncludeXmlComments(Path.Combine(basePath, "FLY.Thick.Base.xml"));
                    c.IncludeXmlComments(Path.Combine(basePath, "MISC.xml"));
                    c.IncludeXmlComments(Path.Combine(basePath, "FLY.OBJComponents.xml"));
                    #endregion
                    c.SingleApiVersion("v1", "流延测厚仪API");
                    c.UseFullTypeNameInSchemaIds();

                })
                .EnableSwaggerUi();

            //清除xml格式，使用json格式
            config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
            config.Formatters.Add(new JsonMediaTypeFormatter());

            //设置依赖注入的解析器
            config.DependencyResolver = new Unity.AspNet.WebApi.UnityDependencyResolver(container);

            //启动WebAPI
            builder.UseWebApi(config);

        }
    }

}
