using System;
using System.IO;
using System.Reflection;
using System.Web.Http;
using DalSoft.WebApi.HelpPage;
using Microsoft.Owin.FileSystems;
using Microsoft.Owin.StaticFiles;
using Ninject.Web.Common.OwinHost;
using Ninject.Web.WebApi.OwinHost;
using Owin;

namespace WindowsDataCenter
{
    public class StartOwin
    {
        public void Configuration(IAppBuilder appBuilder)
        {
            var config = new HttpConfiguration();
            config.MapHttpAttributeRoutes();

            config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
            //JSONP formatter for cross-domain requests.
            config.Formatters.Insert(0, new JsonpFormatter());

            var staticFilePaths = new Uri(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().CodeBase), "www"))
                .LocalPath;
            var fileSystem = new PhysicalFileSystem(staticFilePaths);
            var options = new FileServerOptions
            {
                EnableDefaultFiles = true,
                FileSystem = fileSystem
            };
            options.StaticFileOptions.FileSystem = fileSystem;
            options.StaticFileOptions.ServeUnknownFileTypes = true;
            options.DefaultFilesOptions.DefaultFileNames = new[]
            {
                "index.html"
            };

            config.EnsureInitialized();
            appBuilder.UseNinjectMiddleware(()=>NinjectHelper.GetInstance().Kernel).UseNinjectWebApi(config);
            //appBuilder.UseWebApi(config);
#if DEBUG
            //Add the help-pages extension only in Debug mode.
            appBuilder.UseWebApiHelpPage(config, "help-pages");
#endif

            appBuilder.UseFileServer(options);
        }
    }
}