import graphene
from graphene_django.filter import DjangoFilterConnectionField

from basicknowledge.models import BasicKnowledge
from basicknowledge.queries import InstrumentNode
from books.models import Module
from books.schema.queries import ModuleNode
from users.models import User

from .types import PrivateUserNode


class UsersQuery(object):
    me = graphene.Field(PrivateUserNode)
    all_users = DjangoFilterConnectionField(PrivateUserNode)
    my_activity = DjangoFilterConnectionField(ModuleNode)
    my_instrument_activity = DjangoFilterConnectionField(InstrumentNode)

    def resolve_me(self, info, **kwargs):
        return info.context.user

    def resolve_all_users(self, info, **kwargs):
        if not info.context.user.is_superuser:
            return User.objects.none()
        else:
            return User.objects.all()

    def resolve_my_activity(self, info, **kwargs):
        return Module.objects.all()

    def resolve_my_instrument_activity(self, info, **kwargs):
        return BasicKnowledge.objects.all()


class AllUsersQuery(object):
    me = graphene.Field(PrivateUserNode)
    all_users = DjangoFilterConnectionField(PrivateUserNode)

    def resolve_all_users(self, info, **kwargs):
        if not info.context.user.is_superuser:
            return User.objects.none()
        else:
            return User.objects.all()
