package servlet;

import java.io.IOException;
import java.util.List;

import DAO.*;
import DTO.UtilisateurDTO;
import Mapper.UtilisateurMapper;
import metier.*;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.mapstruct.factory.Mappers;


@WebServlet(name = "Show", urlPatterns = {"/Show"})
public class Show extends HttpServlet {
    Utilisateur u;
    QuizzDAO quizzDAO = new QuizzDAO();
    SessionDAO sessionDAO = new SessionDAO();
    UtilisateurDAO utilisateurDAO = new UtilisateurDAO();

    UtilisateurMapper mapper_u = Mappers.getMapper(UtilisateurMapper.class);

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");

        String result = "<!DOCTYPE html>" +
                "<html>" +
                "<head>" +
                "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + request.getContextPath() + "/css/style.css\">" +
                "</head>" +
                "<body>";



        //UTILISATEUR
        List<Utilisateur> listUser = utilisateurDAO.findAll();

        result += "<H2>Utilisateurs :</H2>";
        result +=
                "<table class=\"show\">" +
                        "<thead class=\"show\">" +
                        "<tr>" +
                        "<th class=\"show\">Nom</th><th class=\"show\">mail</th>" +
                        "</tr>" +
                        "</thead>" +
                        "<tbody>";
        for (Utilisateur u : listUser) {
            UtilisateurDTO dto = mapper_u.toDTO(u);
            result +=
                    "<tr>" +
                            "<th class=\"show\">" + dto.getName() + "</th>" + "<th class=\"show\">" + dto.getEmail() + "</th>" +
                            "</tr>";
            System.out.println(u.getName()+dto.getEmail());
        }
        result +=
                "</tbody>"+
            "</table>";

        //QUIZZ
        List<Quizz> listQuizz = quizzDAO.findAll();

        result  += "<H2>Quizz :</H2>";
        for (Quizz quizz : listQuizz) {
            result += "quizz n°" + quizz.getId() + "<br/>";
            result += "Admin" + quizz.getUtilisateur().getEmail() + " (id: " + quizz.getUtilisateur().getId() + ") <br/>";
            result += "nb Question :" + quizz.getQuestions().size() +"<br/>";
            for (Question question : quizz.getQuestions()) {
                result += question.getQuestion() + "<br/>";

                result += "choix de réponse : <br/>";
                result += question.getReponse().valHTML();

                result += "Reponses corrects :<br/>";
                for (String reponse : question.getReponse().getReponses()) {
                    result += reponse;
                }
            }
        }

        //SESSION
        List<Session> listSession = sessionDAO.findAll();
        result += "<H2>Session : </H2>";
        result +=
            "<table class=\"show\">"+
                "<thead class=\"show\">"+
                    "<tr>"+
                        "<th class=\"show\">ID</th><th class=\"show\">Theme</th><th class=\"show\">Code PIN</th><th class=\"show\">Qizzs</th><th class=\"show\">Participants</th>"+
                    "</tr>"+
                "</thead>"+
                "<tbody>";

        for(Session session : listSession){
                result+=
                    "<tr>"+
                        "<th class=\"show\">"+session.getId()+"</th>"+"<th class=\"show\">"+session.getTheme()+"</th>"+"<th class=\"show\">"+session.getCodePIN()+"</th>";
                String quizzsID = "";
                for(Quizz quizz : session.getQuizzs()){
                    quizzsID += quizz.getId() + " ; ";
                }
                String UtilisateurID = "";
                for(Utilisateur utilisateur : session.getUtilisateurs()){
                    UtilisateurID += utilisateur.getId() + " ; ";
                }
                result += "<th class=\"show\">"+quizzsID+"</th>"+"<th class=\"show\">"+UtilisateurID+"</th>";
                result+="</tr>";
        }

                result +=
                "</tbody>"+
            "</table>";





        //HTML END
        result += "</body>\n</html>";

        response.getWriter().write(result);
    }
}
