import json
import uuid

import wagtail_factories

from vbv_lernwelt.media_library.content_blocks import (
    ExternalLinkBlock,
    InternalLinkBlock,
    LearnMediaBlock,
    RelativeLinkBlock,
)
from vbv_lernwelt.media_library.models import (
    LibraryDocument,
    MediaCategoryPage,
    MediaLibraryPage,
)


class LibraryDocumentFactory(wagtail_factories.DocumentFactory):
    link_display_text = "Dokument herunter laden"
    thumbnail = "https://d9-wret.s3.us-west-2.amazonaws.com/assets/palladium/production/s3fs-public/thumbnails/image/file.jpg"

    class Meta:
        model = LibraryDocument


class MediaLibraryPageFactory(wagtail_factories.PageFactory):
    title = "Mediathek"

    class Meta:
        model = MediaLibraryPage


class LearnMediaBlockFactory(wagtail_factories.StructBlockFactory):
    class Meta:
        model = LearnMediaBlock

    title = "Platzhalter PDF"
    icon_url = "/static/icons/demo/icon-hf-book.png"
    description = "Buch «Platzhalter» – Kapitel 16"
    link_display_text = "PDF anzeigen"
    url = "/static/media/documents/01a_Motorfahrzeughaftpflicht.pdf"
    open_window = True


def create_learn_media_block(link_block=None):
    if link_block is None:
        link_block = LearnMediaBlockFactory()
    return {
        "id": str(uuid.uuid4()),
        "type": "learn_media",
        "value": dict(link_block.items()),
    }


class ExternalLinkBlockFactory(wagtail_factories.StructBlockFactory):
    class Meta:
        model = ExternalLinkBlock

    title = "Platzhalter externer Link"
    description = "This link is really interesting..."
    url = "https://www.vbv.ch/"
    open_window = True
    link_display_text = "Link öffnen"


def create_external_link_block(link_block=None):
    if link_block is None:
        link_block = ExternalLinkBlockFactory()
    return {
        "id": str(uuid.uuid4()),
        "type": "external_link",
        "value": dict(link_block.items()),
    }


class InternalLinkBlockFactory(wagtail_factories.StructBlockFactory):
    class Meta:
        model = InternalLinkBlock

    title = "Platzhalter interner Link"
    description = "Link to a Learning Content"
    link_display_text = "Lerneinheit anzeigen"
    url = "/learn/versicherungsvermittler-in-lp/basis"
    # TODO: page = blocks.PageChooserBlock mit Titel etc


def create_internal_link_block(link_block=None):
    if link_block is None:
        link_block = InternalLinkBlockFactory()
    return {
        "id": str(uuid.uuid4()),
        "type": "internal_link",
        "value": dict(link_block.items()),
    }


class RelativeLinkBlockFactory(wagtail_factories.StructBlockFactory):
    class Meta:
        model = RelativeLinkBlock

    title = "Fahrzeug"
    description = "Platzhalter Querverweis"
    link_display_text = "Handlungsfeld anzeigen"
    icon_url = "/static/icons/demo/icon-hf-reisen.svg"
    url = "/media/versicherungsvermittler-in-media/category/fahrzeug"
    # TODO: page = blocks.PageChooserBlock zu Handlungsfeld


def create_relative_link_block(link_block=None):
    if link_block is None:
        link_block = RelativeLinkBlockFactory()
    return {
        "id": str(uuid.uuid4()),
        "type": "relative_link",
        "value": dict(link_block.items()),
    }


def create_media_collection(title, contents=None, description=None):
    if contents is None:
        contents = []

    result = {
        "id": str(uuid.uuid4()),
        "type": "content_collection",
        "value": {
            "title": title,
            "contents": [content_dict for content_dict in contents],
        },
    }

    if description:
        result["value"]["description"] = description

    return result


def create_document_collection(document_ids=None):
    if document_ids is None:
        document_ids = [d.id for d in LibraryDocument.objects.all()]

    return {
        "id": str(uuid.uuid4()),
        "type": "content_collection",
        "value": {
            "title": "Lernmedien",
            "contents": [
                {"id": str(uuid.uuid4()), "type": "Documents", "value": doc_id}
                for doc_id in document_ids
            ],
        },
    }


class MediaCategoryPageFactory(wagtail_factories.PageFactory):
    title = "Fahrzeug (Platzhalter)"
    introduction_text = """
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem.
Nulla consequat massa quis enim. Donec.
""".strip()
    description_title = "Das erwartet dich in diesem Handlungsfeld"
    description_text = """
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem.
Nulla consequat massa quis enim. Donec.
""".strip()
    overview_icon = "icon-hf-fahrzeug"
    body = json.dumps(
        [
            create_media_collection(
                title="Lernmedien",
                contents=[create_learn_media_block() for _ in range(8)],
            ),
            create_media_collection(
                title="Links",
                contents=[create_external_link_block() for _ in range(4)],
            ),
            create_media_collection(
                title="Verankerung im Lernpfad",
                contents=[create_internal_link_block() for _ in range(3)],
            ),
            create_media_collection(
                title="Querverweise",
                contents=[create_relative_link_block() for _ in range(2)],
            ),
        ]
    )
    items = [
        ("item", "Versicherung 1"),
        ("item", "Versicherung 2"),
        ("item", "Versicherung 3"),
    ]

    class Meta:
        model = MediaCategoryPage
