package com.sculture;

import com.google.gson.Gson;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.HttpRequestWithBody;
import com.sculture.model.response.StoriesResponse;
import com.sculture.model.response.StoryResponse;
import com.sculture.util.MyGson;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;

/**
 * Created by bilal on 14/10/15.
 */
@WebServlet(name = "sculture")
public class MainServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/error.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //User login state

        boolean isLoggedIn = false;
        request.setAttribute("isLoggedIn", false);
        request.setAttribute("username", "");

        if (request.getSession().getAttribute("username") != null) {
            request.setAttribute("username", request.getSession().getAttribute("username"));
            request.setAttribute("isLoggedIn", true);
            isLoggedIn = true;
        }

        StoriesResponse allStories = getStories("/search/all", null);
        StoriesResponse trendingStories = getStories("/recommendation/trending", null);
        StoriesResponse likedStories = getStories("/recommendation/similarToLiked", null);
        StoriesResponse followedStories = getStories("/recommendation/fromFollowedUser", null);


        // Merge liked and followed, removing duplicates
        HashSet<StoryResponse> recommendedSet = new HashSet<StoryResponse>();
        if(likedStories.getResult() != null) recommendedSet.addAll(likedStories.getResult());
        if(followedStories.getResult() != null) recommendedSet.addAll(followedStories.getResult());

        ArrayList<StoryResponse> recommendedList = new ArrayList<StoryResponse>();
        recommendedList.addAll(recommendedSet);

        StoriesResponse recommendedStories = new StoriesResponse();
        recommendedStories.setResult(recommendedList);


        request.setAttribute("allStories", allStories);
        request.setAttribute("trendingStories", trendingStories);
        request.setAttribute("recommendedStories", recommendedStories);

        request.getRequestDispatcher("/frontend_homepage.jsp").forward(request, response);

    }



    StoriesResponse getStories(String endPoint, String accessToken) {
        StoriesResponse storiesResponse = null;

        JSONObject params = new JSONObject();
        params.put("page", 1);
        params.put("size", 4);

        HttpRequestWithBody requestWithBody = Unirest.post("http://52.59.252.52:9000" + endPoint)
                .header("Content-Type", "application/json");
        if(accessToken != null) {
            requestWithBody.header("access_token", accessToken);
        }

        try {
            HttpResponse<JsonNode> jsonStoriesResponse = requestWithBody.body(new JsonNode(params.toString())).asJson();
            Gson gson = MyGson.create();
            storiesResponse = gson.fromJson(jsonStoriesResponse.getBody().toString(), StoriesResponse.class);
        } catch (UnirestException e) {
            e.printStackTrace();
        }

        return storiesResponse;
    }

}

@@ -1264,24 +1264,59 @@
 In = false;%0A
+        String accessToken = null;%0A
         requ
@@ -1648,16 +1648,104 @@
 = true;%0A
+            accessToken = request.getSession().getAttribute(%22access_token%22).toString();%0A
         
@@ -1739,32 +1739,33 @@
 g();%0A        %7D%0A%0A
+%0A
         StoriesR
@@ -1990,20 +1990,27 @@
 Liked%22, 
-null
+accessToken
 );%0A     
@@ -2093,20 +2093,27 @@
 dUser%22, 
-null
+accessToken
 );%0A%0A%0A   
@@ -2259,32 +2259,56 @@
  if(likedStories
+ != null && likedStories
 .getResult() != 
@@ -2380,32 +2380,59 @@
 (followedStories
+ != null && followedStories
 .getResult() != 
@@ -3502,25 +3502,25 @@
 ader(%22access
-_
+-
 token%22, acce

Fix recommended stories.