package com.app.quiz.service;

import java.util.*;

import org.springframework.stereotype.Service;

import com.app.quiz.model.Question;

@Service
public class QuestionsService {

    private final Map<Integer, Question> questions = new HashMap<>();
    private int nextId = 1;

    public List<Question> loadQuizzes() {
        return List.copyOf(questions.values());
    }

    public Question getQuestionById(int id) {
        return questions.get(id);
    }

    public int getNextId() {
        return nextId++;
    }

    public boolean addQuestion(Question Question) {
        Integer QuestionId = Question.getId();

        if (questions.containsKey(QuestionId)) {
            return false;
        } else {
            questions.put(QuestionId, Question);
            return true;
        }
    }

    public boolean editQuestion(Question Question) {
        Integer QuestionId = Question.getId();

        if (questions.containsKey(QuestionId)) {
            questions.put(QuestionId, Question);
            return true;
        } else {
            return false;
        }
    }

    public boolean deleteQuestion(int id) {
        Integer QuestionId = Integer.valueOf(id);
        if (questions.containsKey(QuestionId)) {
            questions.remove(QuestionId);
            return true;
        } else {
            return false;
        }
    }

    public int submitQuestion(ArrayList<Question> list) {
        int result = 0;
        for (Question Question: list){
            Question QuestionInList = questions.get(Question.getId());
            if(QuestionInList.getCorrectAnswer().equals(Question.getCorrectAnswer())) {
                result++;
            }
        }
        return result;
    }
}