import graphene
from api.utils import get_object
from core.logger import get_logger
from graphene import relay
from graphene_django.filter import DjangoFilterConnectionField
from wagtail.models import Locale

from ..models import Book, Chapter, Module, ModuleCategory, ModuleLevel, Snapshot, Topic
from .connections import ModuleConnection, TopicConnection

logger = get_logger(__name__)


class BookQuery(object):
    node = relay.Node.Field()
    topic = graphene.Field(
        "books.schema.nodes.topic.TopicOr404Node", slug=graphene.String()
    )
    module = graphene.Field(
        "books.schema.nodes.module.ModuleNode", slug=graphene.String(), id=graphene.ID()
    )
    chapter = relay.Node.Field("books.schema.nodes.chapter.ChapterNode")
    content_block = relay.Node.Field("books.schema.nodes.content.ContentBlockNode")
    snapshot = relay.Node.Field("books.schema.nodes.snapshot.SnapshotNode")

    topics = relay.ConnectionField(TopicConnection)
    modules = relay.ConnectionField(ModuleConnection)
    chapters = DjangoFilterConnectionField("books.schema.nodes.chapter.ChapterNode")

    module_level = graphene.Field(
        "books.schema.nodes.module_level.ModuleLevelNode", id=graphene.ID(required=True)
    )
    module_levels = graphene.List("books.schema.nodes.module_level.ModuleLevelNode")

    module_category = graphene.Field(
        "books.schema.nodes.module_category.ModuleCategoryNode",
        id=graphene.ID(required=True),
    )
    module_categories = graphene.List(
        "books.schema.nodes.module_category.ModuleCategoryNode"
    )

    def resolve_books(self, *args, **kwargs):
        return Book.objects.filter(**kwargs).live()

    def resolve_topics(self, *args, **kwargs):
        default_locale = Locale.get_default()
        return Topic.objects.live().filter(locale=default_locale)

    def resolve_modules(self, *args, **kwargs):
        return Module.objects.filter(**kwargs).live()

    def resolve_chapters(self, *args, **kwargs):
        return Chapter.objects.filter(**kwargs).live()

    def resolve_snapshot(self, info, **kwargs):
        id = kwargs.get("id")
        snapshot = get_object(Snapshot, id)
        return snapshot

    def resolve_module(self, info, **kwargs):
        slug = kwargs.get("slug")
        id = kwargs.get("id")
        module = None
        try:
            if id is not None:
                module = get_object(Module, id)

            elif slug is not None:
                module = Module.objects.get(slug=slug)

            return module

        except Module.DoesNotExist:
            return None

    def resolve_topic(self, info, **kwargs):
        slug = kwargs.get("slug")
        id = kwargs.get("id")

        if id is not None:
            return get_object(Topic, id)
        if slug is not None:
            try:
                default_locale = Locale.get_default()
                return Topic.objects.get(slug=slug, locale=default_locale)
            except Topic.DoesNotExist:
                from books.schema.nodes.topic import NotFoundFailure

                return NotFoundFailure
        return None

    def resolve_module_level(self, info, **kwargs):
        module_level_id = kwargs.get("id")
        try:
            if module_level_id is not None:
                return get_object(Module, module_level_id)

        except Module.DoesNotExist:
            return None

    def resolve_module_levels(self, *args, **kwargs):
        return ModuleLevel.objects.all()

    def resolve_module_category(self, info, **kwargs):
        id = kwargs.get("id")
        try:
            if id is not None:
                return get_object(Module, id)

        except Module.DoesNotExist:
            return None

    def resolve_module_categories(self, *args, **kwargs):
        return ModuleCategory.objects.all()


class ModuleTypeQuery(graphene.ObjectType):
    node = relay.Node.Field()
    name = graphene.String()
    id = graphene.ID()
