# Standard Library
from types import SimpleNamespace

# Django
from django.template.loader import render_to_string
from django.test import SimpleTestCase


class WeightedAnswersTemplateRenderingTests(SimpleTestCase):
    def _quiz(self, show_answers_corrections_text):
        return SimpleNamespace(
            name="Quiz test",
            show_score_summary=True,
            show_answers_corrections_text=show_answers_corrections_text,
        )

    def test_weighted_summary_displays_explanatory_text_when_enabled(self):
        context = {
            "quiz": self._quiz(True),
            "conclusion": {"points": 4, "description": "<p>ok</p>"},
            "final_quiz": [
                {
                    "question": {"label": "Q1"},
                    "answers": ["R1"],
                    "question_explanation_text": "<p>Texte info</p>",
                }
            ],
            "relaunch_button": False,
        }

        rendered = render_to_string(
            "django_easy_quiz/weighted_answers_form.html",
            context,
        )

        self.assertIn("Texte info", rendered)
        self.assertIn("quiz-answer-explanatory-text", rendered)

    def test_weighted_summary_hides_explanatory_text_when_disabled(self):
        context = {
            "quiz": self._quiz(False),
            "conclusion": {"points": 4, "description": "<p>ok</p>"},
            "final_quiz": [
                {
                    "question": {"label": "Q1"},
                    "answers": ["R1"],
                    "question_explanation_text": "<p>Texte info</p>",
                }
            ],
            "relaunch_button": False,
        }

        rendered = render_to_string(
            "django_easy_quiz/weighted_answers_form.html",
            context,
        )

        self.assertNotIn("Texte info", rendered)
        self.assertNotIn("quiz-answer-explanatory-text", rendered)

    def test_weighted_summary_supports_legacy_string_answers(self):
        context = {
            "quiz": self._quiz(True),
            "conclusion": {"points": 2, "description": "<p>ok</p>"},
            "final_quiz": [
                {
                    "question": {"label": "Q legacy"},
                    "answers": ["<b>Ancienne réponse</b>"],
                }
            ],
            "relaunch_button": False,
        }

        rendered = render_to_string(
            "django_easy_quiz/weighted_answers_form.html",
            context,
        )

        self.assertIn("Ancienne réponse", rendered)

    def test_weighted_summary_partial_handles_selected_and_not_selected_answers(self):
        rendered = render_to_string(
            "django_easy_quiz/partials/weighted_summary_answers.html",
            {
                "fieldset_index": 2,
                "answer_entries": [
                    {"id": 10, "label": "Yes", "selected": True},
                    {"id": 11, "label": "No", "selected": False},
                ],
            },
        )

        self.assertIn("wa_sum_2_1", rendered)
        self.assertIn("wa_sum_2_2", rendered)
        self.assertIn("Yes", rendered)
        self.assertIn("No", rendered)
        self.assertIn("quiz-answer--not-selected", rendered)

    def test_weighted_summary_partial_supports_legacy_text_and_empty_state(self):
        rendered_legacy = render_to_string(
            "django_easy_quiz/partials/weighted_summary_answers.html",
            {
                "fieldset_index": 1,
                "answer_entries": ["<b>Legacy value</b>"],
            },
        )
        rendered_empty = render_to_string(
            "django_easy_quiz/partials/weighted_summary_answers.html",
            {
                "fieldset_index": 1,
                "answer_entries": [],
            },
        )

        self.assertIn("Legacy value", rendered_legacy)
        self.assertIn("You haven't selected any answer.", rendered_empty)


class PdfTemplateRenderingTests(SimpleTestCase):
    def test_pdf_template_displays_explanatory_text_when_enabled(self):
        context = {
            "quiz_type": "weighed_answers_quiz",
            "conclusion": {"points": 5, "description": "<p>Done</p>"},
            "final_quiz": [
                {
                    "question": {"label": "Q1"},
                    "answers": ["A1"],
                    "question_explanation_text": "<p>Info PDF</p>",
                }
            ],
            "final_quiz_show_answers_corrections_text": True,
            "logo": "logo.png",
        }

        rendered = render_to_string("django_easy_quiz/quiz_pdf.html", context)

        self.assertIn("Info PDF", rendered)
        self.assertIn("quiz-answer-explanatory-text", rendered)

    def test_pdf_template_supports_legacy_string_answers(self):
        context = {
            "quiz_type": "weighed_answers_quiz",
            "conclusion": {"points": 3, "description": "<p>Done</p>"},
            "final_quiz": [
                {
                    "question": {"label": "Q1"},
                    "answers": ["<i>Answer legacy</i>"],
                }
            ],
            "final_quiz_show_answers_corrections_text": False,
            "logo": "logo.png",
        }

        rendered = render_to_string("django_easy_quiz/quiz_pdf.html", context)

        self.assertIn("Answer legacy", rendered)
