from django.core.management import BaseCommand

from basicknowledge.models import BasicKnowledge
from datetime import date

actions = {
    "text_block": lambda c: print(c.value.get("text")),
    "subtitle": lambda c: print(f'<h3>{c.value.get("text")}</h3>'),
    "section_title": lambda c: print(f'<h2>{c.value.get("text")}</h2>'),
    "thinglink_block": lambda _: print("===THINGLINK==="),
    "image_block": lambda _: print("===BILD==="),
    "document_block": lambda _: print("===DOCUMENT==="),
    "infogram_block": lambda _: print("===INFOGRAM==="),
    "genially_block": lambda _: print("===GENIALLY==="),
    "video_block": lambda _: print("===VIDEO==="),
    "link_block": lambda c: print(
        f'===LINK=== {c.value.get("text")} {c.value.get("url")}'
    ),
}


class Command(BaseCommand):
    """
    usage: `python manage.py export_instruments --slugs slug-a slug-b [...]`
    """

    def add_arguments(self, parser):
        parser.add_argument("--slugs", nargs="+", type=str)

    def handle(self, *args, **options):
        # todo: only published after X: Page.last_published_at > X
        print(
            """
        <!DOCTYPE html>
          <html>
          <head>
            <meta charset="UTF-8">
            </head>
            <body>
              """
        )
        for i in BasicKnowledge.objects.live().filter(slug__in=options["slugs"]):
            print(f"<h1>{i.title}</h1>")
            print(f"<p>{i.intro}</p>")
            for c in i.contents:
                action = actions.get(
                    c.block_type, lambda c: print(f"==={c.block_type}===")
                )
                action(c)
        print("</body></html>")
