import graphene
from django.db.models import Q
from graphene import relay
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField

from assignments.models import StudentSubmission
from assignments.schema.types import StudentSubmissionNode
from books.models import Module, Chapter, ContentBlock, RecentModule
from books.schema.interfaces.module import ModuleInterface
from books.schema.nodes.chapter import ChapterNode
from notes.models import ModuleBookmark, ContentBlockBookmark, ChapterBookmark
from notes.schema import ModuleBookmarkNode, ContentBlockBookmarkNode, ChapterBookmarkNode
from objectives.schema import ObjectiveGroupNode
from surveys.models import Answer
from surveys.schema import AnswerNode


class ModuleNode(DjangoObjectType):
    class Meta:
        model = Module
        only_fields = [
            'slug', 'title', 'meta_title', 'teaser', 'intro', 'objective_groups', 'assignments', 'hero_image', 'topic'
        ]
        filter_fields = {
            'slug': ['exact', 'icontains', 'in'],
            'title': ['exact', 'icontains', 'in'],
        }
        interfaces = (ModuleInterface,)

    chapters = graphene.List(ChapterNode)
    solutions_enabled = graphene.Boolean()
    bookmark = graphene.Field(ModuleBookmarkNode)
    my_submissions = DjangoFilterConnectionField(StudentSubmissionNode)
    my_answers = DjangoFilterConnectionField(AnswerNode)
    my_content_bookmarks = DjangoFilterConnectionField(ContentBlockBookmarkNode)
    my_chapter_bookmarks = DjangoFilterConnectionField(ChapterBookmarkNode)
    snapshots = graphene.List('books.schema.nodes.SnapshotNode')
    objective_groups = graphene.List(ObjectiveGroupNode)

    def resolve_chapters(self, info, **kwargs):
        return Chapter.get_by_parent(self)

    def resolve_topic(self, info, **kwargs):
        return self.get_parent().specific

    def resolve_solutions_enabled(self, info, **kwargs):
        school_class = info.context.user.selected_class
        return self.solutions_enabled_for.filter(pk=school_class.pk).exists() if school_class is not None else False

    def resolve_bookmark(self, info, **kwags):
        return ModuleBookmark.objects.filter(
            user=info.context.user,
            module=self
        ).first()

    def resolve_my_submissions(self, info, **kwargs):
        user = info.context.user
        return StudentSubmission.objects.filter(student=user, assignment__module=self)
        # we want:
        # StudentSubmission

    def resolve_my_answers(self, info, **kwargs):
        user = info.context.user
        return Answer.objects.filter(owner=user, survey__module=self)
        # Survey

    def resolve_my_content_bookmarks(self, info, **kwargs):
        user = info.context.user
        content_blocks = ContentBlock.objects.live().descendant_of(self)
        return ContentBlockBookmark.objects.filter(content_block__in=content_blocks, user=user)
        # Bookmark Text
        # Bookmark Image etc
        # Bookmark Other
        # Note
        #

    def resolve_my_chapter_bookmarks(self, info, **kwargs):
        user = info.context.user
        chapters = Chapter.objects.live().descendant_of(self)
        return ChapterBookmark.objects.filter(chapter__in=chapters, user=user)

    @staticmethod
    def resolve_objective_groups(parent, info, **kwargs):
        return parent.objective_groups.all() \
            .prefetch_related('hidden_for')

    @staticmethod
    def resolve_snapshots(parent, info, **kwargs):
        user = info.context.user
        return parent.snapshots.filter(Q(creator=user) | Q(Q(creator__team=user.team ) & Q(shared=True)))


class RecentModuleNode(DjangoObjectType):
    class Meta:
        model = RecentModule
        interfaces = (relay.Node,)
