package com.wikigraph.main;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.wikigraph.*;
import org.json.JSONArray;
import spark.Request;
import spark.Response;
import spark.Route;
import spark.template.freemarker.FreeMarkerRoute;
import com.wikigraph.wikidump.ArticleNameResolver;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static spark.Spark.get;
import static spark.Spark.staticFileLocation;

public class RunWebSiteMode implements RunMode {
  @Override
  public void run(String[] args) {
    if (args.length != 1) {
      System.out.println("Requires 1 argument: [baseDir]");
      System.exit(1);
    }
    ArticleNameResolver redirects = new ArticleNameResolver(new File(args[0]));
    final ArticleGraph articleGraph = new ArticleGraph(new WikidumpArticleReader(args[0]), redirects);

    staticFileLocation("static");

    get(new FreeMarkerRoute("/") {
      @Override
      public Object handle(Request request, Response response) {
        Map<String, Object> templateValues = new HashMap<String, Object>();
        return modelAndView(templateValues, "index.ftl");
      }
    });

    get(new Route("/page") {
      @Override
      public Object handle(Request request, Response response) {
        String pageName = request.queryParams("page");
        List<Article> articles = articleGraph.loadArticleFromName(pageName, 1).getConnections();
        if (articles == null) {
          //TODO(probably Aakash): article not found logic
          return "[PAGE NOT FOUND]";
        } else {
          List<String> links = Lists.transform(articles, new Function<Article, String>() {
            @Override
            public String apply(Article article) {
              return article.getTitle();
            }
          });

          JSONArray linksJson = new JSONArray(links);
          return linksJson.toString();
        }
      }
    });
  }
}

@@ -127,9 +127,145 @@
 aph.
-*
+Article;%0Aimport com.wikigraph.ArticleGraph;%0Aimport com.wikigraph.WikidumpArticleReader;%0Aimport com.wikigraph.wikidump.ArticleNameResolver
 ;%0Aim
@@ -407,59 +407,8 @@
 ute;
-%0Aimport com.wikigraph.wikidump.ArticleNameResolver;
 %0A%0Aim
@@ -524,19 +524,42 @@
 k.Spark.
-get
+externalStaticFileLocation
 ;%0Aimport
@@ -578,34 +578,19 @@
 k.Spark.
-staticFileLocation
+get
 ;%0A%0Apubli
@@ -984,17 +984,25 @@
 );%0A%0A    
-s
+externalS
 taticFil
@@ -1012,16 +1012,35 @@
 cation(%22
+src/main/resources/
 static%22)

Switch to external static files location.