from graphql_relay import to_global_id

from core.factories import UserFactory
from core.tests.base_test import SkillboxTestCase
from users.factories import SchoolClassFactory
from users.models import SchoolClassMember, UserSetting


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


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

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

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

        return self.get_client(user=self.user).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)
        self.assertFalse(result.get('data').get('me').get('selectedClass').get('readOnly'))

    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'))

    def test_inactive_class_as_selected_class(self):
        selected_class = self.class2
        membership = SchoolClassMember.objects.get(user=self.user, school_class=selected_class)
        self.assertTrue(membership.active)
        membership.active = False
        membership.save()

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

        result = self.make_query()
        self.assertIsNone(result.get('errors'))
        self.assertTrue(result.get('data').get('me').get('selectedClass').get('readOnly'))

