package ai.elimu.rest.v2.crowdsource;

import ai.elimu.dao.ContributorDao;
import ai.elimu.dao.WordContributionEventDao;
import ai.elimu.dao.WordPeerReviewEventDao;
import ai.elimu.model.contributor.Contributor;
import ai.elimu.model.contributor.WordContributionEvent;
import ai.elimu.model.contributor.WordPeerReviewEvent;
import ai.elimu.model.v2.gson.crowdsource.WordContributionEventGson;
import ai.elimu.rest.v2.JpaToGsonConverter;
import ai.elimu.web.content.word.WordPeerReviewsController;
import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * REST API for the Crowdsource application: https://github.com/elimu-ai/crowdsource
 * <p>
 * 
 * This controller has similar functionality as the {@link WordPeerReviewsController}.
 */
@RestController
@RequestMapping(value = "/rest/v2/crowdsource/word-peer-reviews", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class WordPeerReviewsRestController {
    
    private Logger logger = LogManager.getLogger();
    
    @Autowired
    private WordContributionEventDao wordContributionEventDao;

    @Autowired
    private ContributorDao contributorDao;

    @Autowired
    private WordPeerReviewEventDao wordPeerReviewEventDao;
    
    /**
     * Get {@link WordContributionEvent}s pending a {@link WordPeerReviewEvent} for the current {@link Contributor}.
     *
     * Note: Currently The list of Emojis are not delivered in this method as opposed to the handleGetRequest method in
     *       WordPeerReviewsController
     */
    @RequestMapping(method = RequestMethod.GET)
    public String handleGetRequest(HttpServletRequest request,
                                   HttpServletResponse response) {
        logger.info("handleGetRequest");


        // Validate the Contributor.
        JSONObject jsonObject = new JSONObject();

        String providerIdGoogle = request.getHeader("providerIdGoogle");
        logger.info("providerIdGoogle: " + providerIdGoogle);
        if (StringUtils.isBlank(providerIdGoogle)) {
            jsonObject.put("result", "error");
            jsonObject.put("errorMessage", "Missing providerIdGoogle");
            response.setStatus(HttpStatus.BAD_REQUEST.value());

            String jsonResponse = jsonObject.toString();
            logger.info("jsonResponse: " + jsonResponse);
            return jsonResponse;
        }

        // Lookup the Contributor by ID
        Contributor contributor = contributorDao.readByProviderIdGoogle(providerIdGoogle);
        logger.info("contributor: " + contributor);
        if (contributor == null) {
            jsonObject.put("result", "error");
            jsonObject.put("errorMessage", "The Contributor was not found.");
            response.setStatus(HttpStatus.UNPROCESSABLE_ENTITY.value());

            String jsonResponse = jsonObject.toString();
            logger.info("jsonResponse: " + jsonResponse);
            return jsonResponse;
        }

        List<WordContributionEvent> mostRecentWordContributionEvents = wordContributionEventDao.readMostRecentPerWord();
        logger.info("mostRecentWordContributionEvents.size(): " + mostRecentWordContributionEvents.size());

        // For each WordContributionEvent, check if the Contributor has already performed a peer-review.
        // If not, add it to the list of pending peer reviews.
        List<WordContributionEvent> wordContributionEventsPendingPeerReview = new ArrayList<>();
        for (WordContributionEvent mostRecentWordContributionEvent : mostRecentWordContributionEvents) {
            // Ignore WordContributionEvents made by the current Contributor
            if (mostRecentWordContributionEvent.getContributor().getId().equals(contributor.getId())) {
                continue;
            }

            // Check if the current Contributor has already peer-reviewed this Word contribution
            List<WordPeerReviewEvent> wordPeerReviewEvents = wordPeerReviewEventDao.readAll(mostRecentWordContributionEvent, contributor);
            if (wordPeerReviewEvents.isEmpty()) {
                wordContributionEventsPendingPeerReview.add(mostRecentWordContributionEvent);
            }
        }
        logger.info("wordContributionEventsPendingPeerReview.size(): " + wordContributionEventsPendingPeerReview.size());

        JSONArray wordContributionEventsJsonArray = new JSONArray();
        for(WordContributionEvent wordContributionEvent : wordContributionEventsPendingPeerReview){
            WordContributionEventGson wordContributionEventGson = JpaToGsonConverter.getWordContributionEventGson(wordContributionEvent);
            String json = new Gson().toJson(wordContributionEventGson);
            wordContributionEventsJsonArray.put(new JSONObject(json));
        }

        String jsonResponse = wordContributionEventsJsonArray.toString();
        logger.info("jsonResponse: " + jsonResponse);
        return jsonResponse;
    }
}

@@ -856,68 +856,8 @@
 pe;%0A
-import org.springframework.web.bind.annotation.RequestBody;%0A
 impo
@@ -915,16 +915,16 @@
 apping;%0A
+
 import o
@@ -1166,34 +1166,8 @@
 st;%0A
-import java.util.HashMap;%0A
 impo
@@ -1285,16 +1285,19 @@
  %3Cp%3E%0A * 
+%3Cp%3E
 %0A * This
@@ -1557,21 +1557,17 @@
 oller %7B%0A
-    
 %0A
+
     priv
@@ -1610,21 +1610,17 @@
 gger();%0A
-    
 %0A
+
     @Aut
@@ -1827,13 +1827,9 @@
 ao;%0A
-    
 %0A
+
     
@@ -1955,16 +1955,20 @@
 .%0A     *
+ %3Cp%3E
 %0A     * 
@@ -2039,16 +2039,14 @@
 hod 
-as oppos
+compar
 ed t
@@ -2085,21 +2085,22 @@
 %0A     * 
-     
+%7B@link
  WordPee
@@ -2117,16 +2117,17 @@
 ntroller
+%7D
 %0A     */
@@ -4972,16 +4972,17 @@
      for
+ 
 (WordCon
@@ -5060,16 +5060,17 @@
 rReview)
+ 
 %7B%0A      

remove unused imports and reformat code