from django.utils.safestring import mark_safe
import django_tables2 as tables
from django_tables2.utils import A

from .models import Question, UserAnswer

from django.utils.html import format_html

from generic.tables import SelectionTable


class QuestionTable(SelectionTable):
    def get_view_cell(record):
    
        if record.open_access:
            return mark_safe(
                """<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-opencollective" viewBox="0 0 16 16">
    <path fill-opacity=".4" d="M12.995 8.195c0 .937-.312 1.912-.78 2.693l1.99 1.99c.976-1.327 1.6-2.966 1.6-4.683 0-1.795-.624-3.434-1.561-4.76l-2.068 2.028c.468.781.78 1.679.78 2.732h.04Z"/>
    <path d="M8 13.151a4.995 4.995 0 1 1 0-9.99c1.015 0 1.951.273 2.732.82l1.95-2.03a7.805 7.805 0 1 0 .04 12.449l-1.951-2.03a5.072 5.072 0 0 1-2.732.781z"/>
</svg> View"""
            )
        else:
            return 'View'
    #edit = tables.LinkColumn(
    #    "sbas:sbas_question_update", text="Edit", args=[A("pk")], orderable=False
    #)
    view = tables.LinkColumn(
        "sbas:question_detail", text=get_view_cell, args=[A("pk")], orderable=False
    )
    #delete = tables.LinkColumn(
    #    "sbas:question_delete", text="Delete", args=[A("pk")], orderable=False
    #)
    #exams = tables.ManyToManyColumn(verbose_name="Exams")

    #selection = tables.CheckBoxColumn(accessor="pk", orderable=False)

    stem = tables.Column()
    answers = tables.TemplateColumn("<ol type='A'><li>{{ record.a_answer|safe}}</li><li>{{ record.b_answer|safe}}</li><li>{{ record.c_answer|safe}}</li><li>{{ record.d_answer|safe}}</li><li>{{ record.e_answer|safe}}</li></ol>")

    edit = tables.LinkColumn(
        "sbas:question_update", text="Edit", args=[A("pk")], orderable=False
    )

    class Meta(SelectionTable.Meta):
        model = Question
        template_name = "django_tables2/bootstrap4.html"
        fields = (
            #"stem",
            #"a_answer",
            #"b_answer",
            #"c_answer",
            #"d_answer",
            #"e_answer",
            "best_answer",
            "created_date",
            "category",
            "author",
            "edit"
        )
        sequence = ("view", "stem", "answers")#, "exams")
        #`attrs = {"class": "table"}
        order_by = "-created_date"

    def __init__(self, data=None, *args, **kwargs):
        super().__init__(
            data.prefetch_related(
                "category", "author"
            ),
            *args,
            **kwargs,
        )

    def render_stem(self, value, record=None, **kwargs):
        """Render the stem, optionally with a title from the record.

        Some versions of django-tables2 may call the render method without
        passing a `record`; guard for that and fall back to rendering the
        value only.
        """
        title = getattr(record, "title", None) if record is not None else None
        if title:
            return format_html("<h5>{}</h5>{}", title, mark_safe(value))
        return mark_safe(value)

    def render_best_answer(self, value, record=None, **kwargs):
        """Show only the capitalised first character of the best_answer."""
        if value is None:
            return ""
        s = str(value).strip()
        if not s:
            return ""
        return s[0].upper()



class UserAnswerTable(tables.Table):
    select = tables.CheckBoxColumn(accessor=("pk"))
    #delete = tables.LinkColumn(
    #    "sbas:user_answer_delete", text="Delete", args=[A("pk")], orderable=False
    #)
    view = tables.LinkColumn('sbas:user_answer_view',
                             text='View',
                             args=[A('pk')],
                             orderable=False)
    class Meta:
        model = UserAnswer
        template_name = "django_tables2/bootstrap4.html"
        fields = (
            "cid",
            "user",
            "question",
            "answer",
            #"answer_compare",
            "exam",
            "created",
            "updated",
        )
    
