from django.template.loader import render_to_string
from django.utils.translation import gettext as _

from django_tables2 import (
    A,
    BooleanColumn,
    Column,
    DateTimeColumn,
    LinkColumn,
    RelatedLinkColumn,
    Table,
)

from aleksis.apps.kort.forms import PrinterSelectForm


class CardTable(Table):
    """Table to list cards."""

    class Meta:
        attrs = {"class": "highlight"}

    person = RelatedLinkColumn()
    chip_number = LinkColumn("card", verbose_name=_("Chip number"), args=[A("pk")])
    current_status = Column(verbose_name=_("Current status"), accessor=A("pk"))
    valid_until = Column(verbose_name=_("Valid until"))
    deactivated = BooleanColumn(verbose_name=_("Deactivated"))
    actions = Column(verbose_name=_("Actions"), accessor=A("pk"))

    def render_current_status(self, value, record):
        return render_to_string(
            "kort/card/status.html",
            dict(
                card=record,
            ),
        )

    def render_actions(self, value, record):
        return render_to_string(
            "kort/card/actions.html", dict(pk=value, card=record, printer_form=PrinterSelectForm())
        )


class CardPrinterTable(Table):
    """Table to list card printers."""

    class Meta:
        attrs = {"class": "highlight"}

    name = LinkColumn("card_printer", verbose_name=_("Printer name"), args=[A("pk")])
    location = Column(verbose_name=_("Printer location"))

    current_status = Column(verbose_name=_("Current status"), accessor=A("pk"))
    last_seen_at = DateTimeColumn(verbose_name=_("Last seen at"))
    jobs_count = Column(verbose_name=_("Running jobs"))

    actions = Column(verbose_name=_("Actions"), accessor=A("pk"))

    def render_current_status(self, value, record):
        return render_to_string(
            "kort/printer/status.html",
            dict(
                printer=record,
            ),
        )

    def render_actions(self, value, record):
        return render_to_string("kort/printer/actions.html", dict(pk=value, printer=record))