# -*- coding: utf-8 -*-
#
# ITerativ GmbH
# http://www.iterativ.ch/
#
# Copyright (c) 2019 ITerativ GmbH. All rights reserved.
#
# Created on 2019-04-11
# @author: chrigu <christian.cueni@iterativ.ch>
from django.conf import settings
import json

from django.test import TestCase, RequestFactory
from graphene.test import Client

from api import schema
from api.schema import schema
from api.test_utils import DefaultUserTestCase, create_client
from assignments.factories import AssignmentFactory, StudentSubmissionFactory
from assignments.models import Assignment
from books.factories import ModuleFactory
from books.models import ContentBlock, Chapter
from core.factories import UserFactory
from users.models import User
from users.services import create_users


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

        self.submission1 = StudentSubmissionFactory(student=self.student1, assignment=self.assignment)
        self.submission2 = StudentSubmissionFactory(student=self.student2, assignment=self.assignment)

        self.client = create_client(self.student1)

    def query_my_assignments(self):
        query = '''
                query MyActivityQuery {
                  myActivity {
                    edges {
                      node {
                        id
                        title
                        slug
                        metaTitle
                        mySubmissions {
                          edges {
                            node {
                              id
                              text
                              assignment {
                                id
                                title
                              }
                            }
                          }
                        }
                        myAnswers {
                          edges {
                            node {
                              id
                              survey {
                                id
                                title
                              }
                            }
                          }
                        }
                        myContentBookmarks {
                          edges {
                            node {
                              id
                              uuid
                              note {
                                id
                                text
                              }
                              contentBlock {
                                id
                                type
                                contents
                              }
                            }
                          }
                        }
                        myChapterBookmarks {
                          edges {
                            node {
                              id
                              note {
                                id
                                text
                              }
                              chapter {
                                id
                                title
                                description
                              }
                            }
                          }
                        }
                        bookmark {
                          id
                          note {
                            id
                            text
                          }
                          module {
                            id
                            teaser
                            metaTitle
                            intro
                          }
                        }
                      }
                    }
                  }
                }

                '''

        result = self.client.execute(query)

        self.assertIsNone(result.get('errors'))
        return result

    @staticmethod
    def get_content(result):
        return result.get('data').get('myActivity').get('edges')

    def test_my_assignment_query(self):
        result = self.query_my_assignments()
        contents = self.get_content(result)
        self.assertEqual(len(contents), 1)
        self.assertEquals(contents[0].get('node').get('mySubmissions').get('edges')[0].get('node').get('text'), self.submission1.text)

