import django_tables2 as tables
from django_tables2.utils import A

from generic.tables import SingleImageColumn, SelectionTable

from .models import AnatomyQuestion, UserAnswer

from django.utils.html import format_html

from easy_thumbnails.files import get_thumbnailer

from easy_thumbnails.exceptions import InvalidImageFormatError

#from generic.tables import UserAnswerTable

class AnatomyQuestionTable(SelectionTable):
    edit = tables.LinkColumn(
        "anatomy:anatomy_question_update", text="Edit", args=[A("pk")], orderable=False
    )
    view = tables.LinkColumn(
        "anatomy:question_detail", text="View", args=[A("pk")], orderable=False
    )
    image = SingleImageColumn("image", orderable=False)
    # clone = tables.LinkColumn('anatomy:anatomy_question_clone',
    #                          text='Clone',
    #                          args=[A('pk')],
    #                          orderable=False)
    delete = tables.LinkColumn(
        "anatomy:question_delete", text="Delete", args=[A("pk")], orderable=False
    )
    exams = tables.ManyToManyColumn(verbose_name="Exams")

    class Meta(SelectionTable.Meta):
        model = AnatomyQuestion
        template_name = "django_tables2/bootstrap4.html"
        fields = (
            "question_type",
            "description",
            "examination",
            "modality",
            "region",
            "body_part",
            "created_date",
            "open_access",
            "author",
        )
        sequence = ("view", "image", "exams")
        order_by = "-created_date"

    def __init__(self, data=None, *args, **kwargs):
        super().__init__(
            data.prefetch_related(
                "answers", "question_type", "body_part", "modality", "region", "examination", "exams", "author"
            ),
            *args,
            **kwargs,
        )
class AnatomyUserAnswerTable(tables.Table):
    select = tables.CheckBoxColumn(accessor=("pk"))
    delete = tables.LinkColumn(
        "anatomy:user_answer_delete", text="Delete", args=[A("pk")], orderable=False
    )
    view = tables.LinkColumn('anatomy: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",
        )
    
    def __init__(self, data=None, *args, **kwargs):
        super().__init__(
            data.prefetch_related(
                "exam", "question", "question__question_type", "question__modality", "question__examination", "question__region", "user"
            ),
            *args,
            **kwargs,
        )
