using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Web;
using Interfaces;
using Ninject;
using Ninject.Web.Common;

namespace WindowsDataCenter
{
    public static class Configuration
    {
        public static StandardKernel ConfigureNinject()
        {
            var kernel = new StandardKernel();
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
            var ninjectConfigPath =
                new Uri(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().CodeBase), "NinjectConfig.xml"))
                    .LocalPath;

            if (File.Exists(ninjectConfigPath))
            {
                kernel.Load(ninjectConfigPath);
            }

            var logger = kernel.TryGet<ILogger>();
            if (logger == null)
            {
                kernel.Bind<ILogger>().To<DefaultComponents.DefaultLogger>();
                logger = kernel.Get<ILogger>();
            }

            logger.Fatal("test message - ninject stuff loaded.");

            var repo = kernel.TryGet<IRepository>();
            if (repo == null)
            {
                Debug.WriteLine(File.ReadAllText(ninjectConfigPath));
                logger.Fatal("A type of IRepository must be installed. Exiting.");
                throw new ArgumentNullException("IRepository");
            }
            logger.Trace("Got Repo: {0}", repo.GetType());

            return kernel;
        }
    }
}