package org.projectodd.wunderboss.clojure.ring;

import clojure.java.api.Clojure;
import clojure.lang.IFn;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import org.projectodd.wunderboss.clojure.ClojureLoaderWrapper;

import java.util.concurrent.Callable;

public class RingHandler implements HttpHandler {

    public RingHandler(final ClojureLoaderWrapper runtime, final String ringFn, final String context) {
        this.runtime = runtime;
        //TODO: move interning based on FQ name to a util fn called from clojure
        try {
            runtime.callInLoader(new Callable() {
                @Override
                public Object call() throws Exception {
                    String[] parts = ringFn.split("/");
                    IFn symbol = Clojure.var("clojure.core/symbol");
                    Object nsSym = symbol.invoke(parts[0]);
                    IFn require = Clojure.var("clojure.core/require");
                    require.invoke(nsSym);

                    RingHandler.this.ringFn = Clojure.var("clojure.core/intern").invoke(nsSym, symbol.invoke(parts[1]));


                    require.invoke(symbol.invoke("wunderboss.ring"));
                    RingHandler.this.handlerFn = Clojure.var("wunderboss.ring/handle-request");

                    return null;
                }
            });
        } catch (Throwable e) {
            e.printStackTrace();
            this.ringFn = null;
            this.handlerFn = null;
        }

        this.context = context;
    }

    @Override
    public void handleRequest(final HttpServerExchange exchange) throws Exception {
        if (exchange.isInIoThread()) {
            exchange.dispatch(this);
            return;
        }

        this.runtime.callInLoader(new Callable() {
            @Override
            public Object call() throws Exception {
                return RingHandler.this.handlerFn.invoke(RingHandler.this.ringFn, exchange);
            }
        });

    }


    private final ClojureLoaderWrapper runtime;
    private  Object ringFn;
    private  String context;
    private  IFn handlerFn;

}

@@ -487,99 +487,33 @@
     
-//TODO: move interning based on FQ name to a util fn called from clojure%0A        try %7B%0A    
+this.context = context;%0A%0A
     
@@ -558,36 +558,32 @@
 ) %7B%0A            
-    
 @Override%0A      
@@ -568,36 +568,32 @@
       @Override%0A
-    
             publ
@@ -624,32 +624,117 @@
 Exception %7B%0A    
+            //TODO: move interning based on FQ name to a util fn called from clojure%0A
                 
@@ -765,28 +765,24 @@
 split(%22/%22);%0A
-    
             
@@ -850,20 +850,16 @@
         
-  
-  
 Object n
@@ -890,20 +890,16 @@
 ts%5B0%5D);%0A
-    
         
@@ -965,36 +965,32 @@
                 
-    
 require.invoke(n
@@ -1005,36 +1005,32 @@
                 
-  
-  
 RingHandler.this
@@ -1115,21 +1115,16 @@
 %5B1%5D));%0A%0A
-%0A    
         
@@ -1189,36 +1189,32 @@
                 
-    
 RingHandler.this
@@ -1286,28 +1286,24 @@
             
-  
-  
 return null;
@@ -1307,36 +1307,32 @@
 ll;%0A            
-    
 %7D%0A            %7D)
@@ -1329,189 +1329,10 @@
     
- 
-   %7D);%0A        %7D catch (Throwable e) %7B%0A            e.printStackTrace();%0A            this.ringFn = null;%0A            this.handlerFn = null;%0A        %7D%0A%0A        this.context = context
+%7D)
 ;%0A  
@@ -1854,17 +1854,16 @@
 private 
- 
 Object r
@@ -1881,16 +1881,21 @@
 private 
+final
  String 
@@ -1915,17 +1915,16 @@
 private 
- 
 IFn hand

Remove debug cruft.