using System;
using System.Web;
using System.IO;
using System.Web.Hosting;
using System.Threading;
using System.Runtime.Remoting.Lifetime;


namespace Fivestops.Oxide
{
    public class OxideApplicationHost : MarshalByRefObject
    {
        // tell the GC that we don't ever want to be collected.
        public override object InitializeLifetimeService ()
        {
            return (null);
        }

        public string Path
        {
            get
            {
                return (AppDomain.CurrentDomain.GetData (".appPath").ToString());
            }
        }

        public string VPath
        {
            get
            {
                return (AppDomain.CurrentDomain.GetData (".appVPath").ToString());
            }
        }

        private void DispatchRequest (object stateObject)
        {
            OxideWorkerRequest owrRequest = (OxideWorkerRequest) stateObject;

            HttpRuntime.ProcessRequest (owrRequest);
        }

        public void HandleRequest (IntPtr request)
        {
            OxideWorkerRequest owrRequest = new OxideWorkerRequest (
                    new CausalityRequest (request));

            Fivestops.Oxide.CThreadPool.QueueUserWorkItem (
                    new WaitCallback (this.DispatchRequest), owrRequest);
        }

        public static OxideApplicationHost CreateApplicationHost (
                string virtualdir, string basedir)
        {
            OxideApplicationHost o = (OxideApplicationHost) 
                ApplicationHost.CreateApplicationHost (
                        typeof (OxideApplicationHost), virtualdir, basedir);

            return (o);
        }
    }
}
