using Microsoft.Owin.Cors;
using Owin;
using System.Web.Http.Routing;
using System.Web.Http;
using Microsoft.Owin.StaticFiles;
using Microsoft.Owin.FileSystems;

namespace Project.OWIN
{
    public class Startup
    {
        public void Configuration(IAppBuilder appBuilder)
        {
            //var di = new System.IO.DirectoryInfo(@".\View");
            //di = new System.IO.DirectoryInfo(@"D:\Source\WebDev\SVELT\svelte-start-app\public");

            //if (di.Exists == false) di.Create();    
            //// Serve static files
            //var options = new FileServerOptions
            //{
            //    EnableDefaultFiles = true,
            //    FileSystem = new PhysicalFileSystem(di.FullName)
            //};
            //appBuilder.UseFileServer(options);

            // Configure Web API for self-host
            //HttpConfiguration config = new HttpConfiguration();
            //config.Routes.MapHttpRoute(
            //    name: "DefaultApi",
            //    routeTemplate: "api/{controller}/{id}",
            //    defaults: new { id = RouteParameter.Optional }
            //);

            //config.Routes.MapHttpRoute(
            //      name: "ControlApi",
            //      routeTemplate: "api/{controller}/{action}/{id}",
            //      defaults: new { id = RouteParameter.Optional }
            //  );

            // Configure Web API for Self-Host
            HttpConfiguration config = new HttpConfiguration();
            config.MapHttpAttributeRoutes();


            ////메인파일 처리 방법
            IHttpRoute userrouter =
                config.Routes.CreateRoute("ctrl/{controller}/{action}/{id}",
                new { id = RouteParameter.Optional },
                null);

            //메인파일 처리 방법
            IHttpRoute defaultRoute =
                config.Routes.CreateRoute("api/{controller}/{id}",
                new { id = RouteParameter.Optional },
                null);


            config.Routes.Add("defaultRoute", defaultRoute);
            config.Routes.Add("userrouter", userrouter);
            appBuilder.UseWebApi(config);


            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(
                new System.Net.Http.Headers.MediaTypeHeaderValue("application/json")
            );
            // Remove the XML formatter
            config.Formatters.Remove(config.Formatters.XmlFormatter);
            appBuilder.UseCors(CorsOptions.AllowAll);
            // appBuilder.UseWebApi(config);
        }
    }
}

