package com.quizdeck.controllers;

import com.quizdeck.analysis.*;
import com.quizdeck.analysis.exceptions.*;
import com.quizdeck.analysis.outputs.AnalysisResult;
import com.quizdeck.exceptions.InvalidJsonException;
import com.quizdeck.model.database.CompleteQuiz;
import com.quizdeck.model.inputs.OwnerLabelsInput;
import com.quizdeck.repositories.CompletedQuizRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Cade on 2/17/2016.
 */

@RestController
@RequestMapping("/rest/secure/analysis/")
public class AnalysisController {

    @Autowired
    CompletedQuizRepository completedQuizRepository;

    private Logger log = LoggerFactory.getLogger(AnalysisController.class);

    @RequestMapping(value="accuracy/{Id}", method = RequestMethod.GET)
    public AnalysisResult accuracyResponse (@PathVariable String Id) throws AnalysisException{
        return processQuizWith(completedQuizRepository.findById(Id).getQuiz().getId(), QuizAnalysisAlgorithm.ACCURACY);
    }

    @RequestMapping(value="indecisiveness/{Id}", method = RequestMethod.GET)
    public AnalysisResult indecisivenessResponse (@PathVariable String Id) throws AnalysisException {
        return processQuizWith(Id, QuizAnalysisAlgorithm.INDECISIVENESS);
    }

    private AnalysisResult processQuizWith(String id, QuizAnalysisAlgorithm algorithm) throws AnalysisClassException, AnalysisConstructionException, InsufficientDataException, AnalysisResultsUnavailableException {
        CompleteQuiz completeQuiz = completedQuizRepository.findById(id);
        QuizAnalysisFactory factory = new QuizAnalysisFactory();

        factory.autoFillWith(completeQuiz);
        Analysis analysis = factory.getAnalysisUsing(algorithm);
        analysis.performAnalysis();

        return analysis.getResults();
    }

    @RequestMapping(value="label/net-accuracy/", method = RequestMethod.POST)
    public AnalysisResult groupNetAccuracyResponse(@Valid @RequestBody OwnerLabelsInput input, BindingResult result) throws InvalidJsonException, InsufficientDataException, AnalysisClassException, AnalysisConstructionException, AnalysisResultsUnavailableException {
        if(result.hasErrors())
            throw new InvalidJsonException();

        return processGroupWith(input.getOwner(), input.getLabels(), GroupAnalysisAlgorithm.ACCURACY);
    }

    private AnalysisResult processGroupWith(String owner, List<String> labels, GroupAnalysisAlgorithm algorithm) throws AnalysisClassException, AnalysisConstructionException, InsufficientDataException, AnalysisResultsUnavailableException {
        List<CompleteQuiz> quizList = completedQuizRepository.findByOwnerAndLabelsIn(owner, labels);
        GroupAnalysisFactory factory = new GroupAnalysisFactory();

        factory.setCompletedQuizzes(quizList);
        factory.setLabels(labels);
        Analysis analysis = factory.getAnalysisUsing(algorithm);
        analysis.performAnalysis();

        return analysis.getResults();
    }
}

@@ -371,65 +371,8 @@
 ry;%0A
-import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 impo
@@ -797,85 +797,8 @@
 y;%0A%0A
-    private Logger log = LoggerFactory.getLogger(AnalysisController.class);%0A%0A
     
@@ -830,16 +830,20 @@
 curacy/%7B
+quiz
 Id%7D%22, me
@@ -925,32 +925,36 @@
 Variable String 
+quiz
 Id) throws Analy
@@ -1002,62 +1002,14 @@
 ith(
-completedQuizRepository.findById(Id).getQuiz().getId()
+quizId
 , Qu
@@ -1089,16 +1089,20 @@
 veness/%7B
+quiz
 Id%7D%22, me
@@ -1198,16 +1198,20 @@
  String 
+quiz
 Id) thro
@@ -1264,16 +1264,20 @@
 uizWith(
+quiz
 Id, Quiz
@@ -1598,16 +1598,20 @@
 y.findBy
+Quiz
 Id(id);%0A

revert dumb changes that somehow got committed