from graphql_relay import to_global_id

from api.test_utils import create_client, DefaultUserTestCase
from assignments.models import Assignment, StudentSubmission
from .queries.mutations import UPDATE_ASSIGNMENT_MUTATION
from ..factories import AssignmentFactory


class AssignmentPermissionsTestCase(DefaultUserTestCase):
    def setUp(self):
        super(AssignmentPermissionsTestCase, self).setUp()
        self.assignment = AssignmentFactory(
            owner=self.teacher
        )

        self.assignment_id = to_global_id('AssignmentNode', self.assignment.pk)
        self.module_id = to_global_id('ModuleNode', self.assignment.module.pk)

    def _submit_submission(self, user=None):


        if user is None:
            client = create_client(self.student1)
        else:
            client = create_client(user)

        return client.execute(UPDATE_ASSIGNMENT_MUTATION, variables={
            'input': {
                "assignment": {
                    "id": self.assignment_id,
                    "answer": 'Halo',
                    "final": True
                }
            }
        })

    def test_permissions(self):
        self.assertTrue(self.teacher.has_perm('users.can_manage_school_class_content'))
        self.assertTrue(self.teacher2.has_perm('users.can_manage_school_class_content'))
        self.assertFalse(self.student1.has_perm('users.can_manage_school_class_content'))
        self.assertFalse(self.student2.has_perm('users.can_manage_school_class_content'))

    def test_count(self):
        self.assertEqual(Assignment.objects.count(), 1)

    def test_submit_submission(self):
        result = self._submit_submission()
        self.assertIsNone(result.get('errors'))
        self.assertEqual(StudentSubmission.objects.count(), 1)

    def _test_visibility(self, user, count):
        client = create_client(user)
        query = '''
            query AssignmentWithSubmissions($id: ID!) {
              assignment(id: $id) {
                title
                submissions {
                  id
                  text
                  document
                  student {
                    firstName
                    lastName
                  }
                }
              }
            }
        '''
        result = client.execute(query, variables={
            'id': self.assignment_id
        })

        self.assertIsNone(result.get('errors'))
        self.assertEqual(len(result.get('data').get('assignment').get('submissions')), count)

    def test_visible_for_teacher(self):
        self._submit_submission()
        self._test_visibility(self.teacher, 1)

    def test_visible_for_teacher2(self):
        self._submit_submission()
        self._test_visibility(self.teacher2, 0)

    def test_visible_for_student1(self):
        self._submit_submission()
        self._test_visibility(self.student1, 0)

    def test_visible_for_student2(self):
        self._submit_submission()
        self._test_visibility(self.student2, 0)

    def test_advanced_visibility(self):
        self._submit_submission(self.student1)
        self._submit_submission(self.student_second_class)
        self._test_visibility(self.teacher, 1)
        self._test_visibility(self.teacher2, 1)
