package eu.dnetlib.uoaadmintools.controllers;

import eu.dnetlib.uoaadmintools.entities.Topic;
import eu.dnetlib.uoaadmintools.dao.TopicDAO;
import eu.dnetlib.uoaadmintools.entities.Question;
import eu.dnetlib.uoaadmintools.dao.QuestionDAO;
import eu.dnetlib.uoaadmintools.entities.TopicResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")
public class TopicController {
    private final Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private TopicDAO topicDAO;

    @Autowired
    private QuestionDAO questionDAO;

    @RequestMapping(value = "/topic", method = RequestMethod.GET)
    public List<Topic> getAllTopics() {
        return topicDAO.findAll();
    }

    @RequestMapping(value = "/topic", method = RequestMethod.DELETE)
    public void deleteAllTopics() {
        topicDAO.deleteAll();
    }

    //TODO: if id is provided check that it exists!
    @RequestMapping(value = "/topic", method = RequestMethod.POST)
    public Topic insertOrUpdateTopic(@RequestBody Topic topic) {
        return topicDAO.save(topic);
    }

    @RequestMapping(value = "/topic/{id}", method = RequestMethod.GET)
    public Topic getTopic(@PathVariable(value = "id") String id) {
        return topicDAO.findById(id);
    }

    @RequestMapping(value = "/topic/{id}", method = RequestMethod.DELETE)
    public void deleteTopic(@PathVariable(value = "id") String id) {
        List<Question> questions = questionDAO.findByTopicsIn(id);
        for (Question question : questions) {
            List<String> topics = question.getTopics();
            topics.remove(id);
            if (topics.isEmpty()) {
                questionDAO.delete(question.getId());
            } else {
                questionDAO.save(question);
            }
        }
        topicDAO.delete(id);
    }

    @RequestMapping(value = "/topic/delete", method = RequestMethod.POST)
    public void deleteTopics(@RequestBody List<String> topics) {
        for (String topic : topics) {
            deleteTopic(topic);
        }
    }

    @RequestMapping(value = "/topic/toggle", method = RequestMethod.POST)
    public void toggleTopics(@RequestBody List<String> topics, @RequestParam(value="order", defaultValue = "") String order) throws Exception {
        for (String id: topics) {
            Topic topic = topicDAO.findById(id);
            topic.setQuestionOrder(order);
            topicDAO.save(topic);
        }
    }

    @RequestMapping(value = "/topic/active", method = RequestMethod.GET)
    public List<TopicResponse> getActiveTopic() {
        List<TopicResponse> topicResponses = new ArrayList<>();
        for(Topic topic : topicDAO.findAll()) {
            TopicResponse topicResponse = new TopicResponse(topic);
            List<Question> questions = questionDAO.findByTopicsInAndIsActiveIsTrue(topic.getId());
            if(topic.getQuestionOrder().equals("hits")) {
                questions.sort(Comparator.comparingInt(Question::getHitCount).reversed());
            } else if(topic.getQuestionOrder().equals("weight")) {
                questions.sort(Comparator.comparingDouble(Question::getWeight).reversed());
            }
            topicResponse.setQuestions(questions);
            topicResponses.add(topicResponse);
        }
        return topicResponses;
    }

}
