package org.wso2.carbon.uuf.core;

import io.netty.handler.codec.http.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.core.Response;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class App {
    private static final Logger log = LoggerFactory.getLogger(App.class);
    private final String context;
    private final List<Page> pages;
    private final Map<String, Fragment> fragments;
    private final Map<String, Renderable> bindings;

    public App(String context, List<Page> pages, Map<String, Fragment> fragments, Map<String, Renderable> bindings) {
        if (!context.startsWith("/")) {
            throw new IllegalArgumentException("app context must start with a '/'");
        }
        this.context = context;

        // We sort uri so that more wildcard-ed ones go to the bottom.
        Collections.sort(pages, (o1, o2) -> o1.getUriPatten().compareTo(o2.getUriPatten()));
        this.pages = pages;

        this.fragments = fragments;
        this.bindings = bindings;
    }

    public List<Page> getPages() {
        return pages;
    }

    public Map<String, Fragment> getFragments() {
        return fragments;
    }

    public Map<String, Renderable> getBindings() {
        return bindings;
    }

    public String renderPage(HttpRequest request) {
        String pageUri = request.getUri().substring(context.length());
        Optional<Page> servingPage = getPage(pageUri);
        if (!servingPage.isPresent()) {
            throw new UUFException("Requested page '" + pageUri + "' does not exists.", Response.Status.NOT_FOUND);
        }

        Page page = servingPage.get();
        if (log.isDebugEnabled()) {
            log.debug("Page '" + page.toString() + "' is serving.");
        }

        Map<String, Object> model = new HashMap<>();
        model.put("pageUri", pageUri);

        return page.serve(model, bindings, fragments);
    }

    public Optional<Page> getPage(String pageUri) {
        for (Page p : pages) {
            if (p.getUriPatten().match(pageUri)) {
                return Optional.of(p);
            }
        }
        return Optional.empty();
    }

    @Override
    public String toString() {
        return "{\"context\": \"" + context + "\"}";
    }
}

@@ -1124,72 +1124,8 @@
  %7D%0A%0A
-    public List%3CPage%3E getPages() %7B%0A        return pages;%0A    %7D%0A%0A
     
@@ -1207,91 +1207,8 @@
  %7D%0A%0A
-    public Map%3CString, Renderable%3E getBindings() %7B%0A        return bindings;%0A    %7D%0A%0A
     
@@ -1393,17 +1393,16 @@
     if (
-!
 servingP
@@ -1428,131 +1428,8 @@
     
-        throw new UUFException(%22Requested page '%22 + pageUri + %22' does not exists.%22, Response.Status.NOT_FOUND);%0A        %7D%0A%0A
     
@@ -1467,24 +1467,28 @@
 ();%0A        
+   
+ 
 if (log.isDe
@@ -1499,24 +1499,28 @@
 nabled()) %7B%0A
+    
             
@@ -1580,27 +1580,35 @@
 %22);%0A        
+  
+  
 %7D%0A%0A
+    
         Map%3C
@@ -1656,16 +1656,20 @@
         
+ 
+   
 model.pu
@@ -1692,16 +1692,20 @@
 eUri);%0A%0A
+    
         
@@ -1747,24 +1747,167 @@
 fragments);%0A
+        %7D else %7B%0A            throw new UUFException(%22Requested page '%22 + pageUri + %22' does not exists.%22, Response.Status.NOT_FOUND);%0A        %7D%0A
     %7D%0A%0A    p

refactor to get rid of ide warning