from django.contrib.sessions.middleware import SessionMiddleware
from django.test import TestCase, RequestFactory
from graphene.test import Client
from graphql_relay import to_global_id

from api.schema import schema
from core.factories import UserFactory
from users.factories import SchoolClassFactory
from users.models import UserSetting


class UserSettingTests(TestCase):
    def setUp(self):
        self.user = UserFactory(username='aschi')
        self.class1 = SchoolClassFactory(users=[self.user])
        self.class2 = SchoolClassFactory(users=[self.user])
        self.class3 = SchoolClassFactory(users=[])

        request = RequestFactory().get('/')
        request.user = self.user

        # adding session
        middleware = SessionMiddleware()
        middleware.process_request(request)
        request.session.save()
        self.client = Client(schema=schema, context_value=request)

    def make_mutation(self, class_id):
        mutation = '''
            mutation UpdateSettings($input: UpdateSettingInput!) {
              updateSetting(input: $input) {
                success
                errors {
                  field
                }
              }
            }
        '''

        return self.client.execute(mutation, variables={
            'input': {
                'id': to_global_id('SchoolClassNode', class_id)
            }
        })

    def make_query(self):
        query = '''
            query MeQuery {
              me {
                  selectedClass {
                    name
                    id
                  }
                }
            }
        '''

        return self.client.execute(query)

    def test_selects_first_class_on_first_call(self):
        result = self.make_query()
        first_class = self.user.school_classes.first()
        self.assertIsNone(result.get('errors'))
        self.assertEqual(result.get('data').get('me').get('selectedClass').get('name'), first_class.name)

    def test_returns_selected_class(self):
        selected_class = self.user.school_classes.all()[1]
        setting = UserSetting.objects.create(user=self.user, selected_class=selected_class)
        setting.save()
        result = self.make_query()
        self.assertIsNone(result.get('errors'))
        self.assertEqual(result.get('data').get('me').get('selectedClass').get('name'),
                         selected_class.name)

    def test_user_can_select_class(self):

        selected_class = self.user.school_classes.first()
        setting = UserSetting.objects.create(user=self.user, selected_class=selected_class)
        setting.save()

        selected_class = self.user.school_classes.all()[1]
        mutation_result = self.make_mutation(selected_class.pk)
        self.assertIsNone(mutation_result.get('errors'))
        query_result = self.make_query()
        self.assertIsNone(query_result.get('errors'))
        self.assertEqual(query_result.get('data').get('me').get('selectedClass').get('name'),
                         selected_class.name)

    def test_user_can_select_class_even_no_settings_exist(self):

        selected_class = self.user.school_classes.all()[1]
        mutation_result = self.make_mutation(selected_class.pk)
        self.assertIsNone(mutation_result.get('errors'))
        query_result = self.make_query()
        self.assertIsNone(query_result.get('errors'))
        self.assertEqual(query_result.get('data').get('me').get('selectedClass').get('name'),
                         selected_class.name)


    def test_user_cannot_select_class_shes_not_part_of(self):

        default_class = self.user.school_classes.first()
        setting = UserSetting.objects.create(user=self.user, selected_class=default_class)
        setting.save()

        mutation_result = self.make_mutation(self.class3.pk)
        self.assertIsNotNone(mutation_result.get('errors'))


