package controllers;

import backend.ShownTextsTracker;
import backend.TextFinder;
import backend.WordsStatisticsService;
import com.fasterxml.jackson.databind.JsonNode;
import play.libs.Json;
import play.mvc.BodyParser;
import play.mvc.Controller;
import play.mvc.Http;
import play.mvc.Result;

import java.util.*;

/**
 * Created by hlib on 03.10.15.
 */
public class RestControllers extends Controller {

    public static Result getText() {
        return getFollowingText();
    }

    @BodyParser.Of(BodyParser.Json.class)
    public static Result getNextText() {
        //TODO improve json processing
        Http.RequestBody body = request().body();
        JsonNode jsonNode = body.asJson().get("unknownWords");
        System.out.println(jsonNode.asText());
        Iterator<JsonNode> it = jsonNode.elements();
        List<String> unknownWordList =  new ArrayList<String>();
        while (it.hasNext()) {
            unknownWordList.add(it.next().asText());
        }
        //TODO get text id from ui
        Long textId = 123L;
        WordsStatisticsService.updateWordsStatistics(new TextFinder().getNormalizedWordsByTextId(textId), unknownWordList);
        ShownTextsTracker.trackTextView(textId);

        return getFollowingText();
    }

    private static Result getFollowingText() {
        Map<String, Double> features = WordsStatisticsService.getFeaturesForUser();
        Map<Long, Integer> shownTextsStatistics = ShownTextsTracker.getShownTextsStatisticsForUser();
        Long textId = new TextFinder().getTextId(features, shownTextsStatistics);
        String plainText = TextFinder.getPlainTextById(textId);

        Map<String, Object> result = new HashMap<>();
        result.put("content", toHtml(plainText));

        return ok(Json.toJson(result));
    }

    private static Object toHtml(String text) {
        String result = "";
        for (String line: text.split("\n")) {
            result += "<p>" + line + "</p>";
        }
        return result;
    }
}

@@ -1388,24 +1388,26 @@
 ();%0A        
+//
 Map%3CLong, In
@@ -1480,32 +1480,121 @@
 sticsForUser();%0A
+        Map%3CLong, Integer%3E shownTextsStatistics = Collections.%3CLong, Integer%3EemptyMap();%0A
         Long tex

comment gettign statistics about text views