package ca.xqz.tweetmouth;

import ca.xqz.tweetmouth.Tweet;
import ca.xqz.tweetmouth.TweetParser;

import java.net.InetAddress;
import java.net.UnknownHostException;

import java.io.IOException;

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

import org.elasticsearch.action.search.SearchResponse;

import org.elasticsearch.client.transport.TransportClient;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import org.elasticsearch.transport.client.PreBuiltTransportClient;

import spark.ModelAndView;
import spark.Spark;
import spark.TemplateEngine;
import spark.TemplateViewRoute;

import spark.template.mustache.MustacheTemplateEngine;

class Main {
    private final static int CLIENT_PORT = 9300;
    private final static TemplateEngine TEMPLATE = new MustacheTemplateEngine();
    private final static TemplateViewRoute SEARCH = (req, res) -> {
        TransportClient client = getTransportClient();
        QueryBuilder qb = QueryBuilders.simpleQueryStringQuery(req.queryParams("q"));
        SearchResponse search_res = client.prepareSearch().setQuery(qb).get();
        Map<String, Object> temp_res = new HashMap<String, Object>();
        temp_res.put("resp", search_res);
        temp_res.put("hits", search_res.getHits());
        temp_res.put("total_hits", search_res.getHits().getTotalHits());
        temp_res.put("suggest", search_res.getSuggest());
        System.out.println(search_res);
        return new ModelAndView(temp_res, "results.html");
    };

    private static TransportClient getTransportClient() {
        InetAddress localhost;
        try {
            localhost = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            System.out.println("Unknown localhost");
            throw new RuntimeException(e);
        }

        return new PreBuiltTransportClient(Settings.EMPTY)
            .addTransportAddress(new InetSocketTransportAddress(localhost, CLIENT_PORT));
    }

    public static void main(String[] args) {
        TransportClient client = getTransportClient();
        System.out.println("Successfully created a client");

        try {
            List<Tweet> tweetList = TweetParser.parseTweetsFromFile(System.in);
            System.out.println(tweetList.get(55));
        } catch (IOException e) {
            System.out.println("IO Exception in parsing tweets");
            throw new RuntimeException(e);
        }

        client.close();
        System.out.println("Successfully closed a client");

        Spark.staticFileLocation("/public");
        Spark.get("/search", SEARCH, TEMPLATE);
    }
}

@@ -25,79 +25,8 @@
 h;%0A%0A
-import ca.xqz.tweetmouth.Tweet;%0Aimport ca.xqz.tweetmouth.TweetParser;%0A%0A
 impo
@@ -2101,19 +2101,36 @@
 %5B%5D args)
+ throws Exception
  %7B%0A
-
         
@@ -2250,306 +2250,10 @@
     
-try %7B%0A            List%3CTweet%3E tweetList = TweetParser.parseTweetsFromFile(System.in);%0A            System.out.println(tweetList.get(55));%0A        %7D catch (IOException e) %7B%0A            System.out.println(%22IO Exception in parsing tweets%22);%0A            throw new RuntimeException(e);%0A        %7D%0A%0A       
+//
  cli

Rebase cleanup