from graphql_relay import from_global_id

from core.tests.base_test import SkillboxTestCase
from core.tests.helpers import GQLResult
from rooms.models import Room


class GQLRoom:
    def __init__(self, room_data):
        self.id = room_data.get('id')
        self.slug = room_data.get('slug')
        self.title = room_data.get('title')
        self.entry_count = room_data.get('entryCount')
        self.appearance = room_data.get('appearance')
        self.description = room_data.get('description')
        self.school_class = room_data.get('schoolClass')

class AddRoomResult:
    def __init__(self, result: GQLResult):
        self.room = GQLRoom(result.data.get('addRoom').get('room'))
#         id
#       slug
#       title
#       entryCount
#       appearance
#       description
#       schoolClass {
#         id
#         name
#       }

class NewRoomMutationTestCase(SkillboxTestCase):
    def setUp(self) -> None:
        self.createDefault()
        self.mutation = """
        mutation AddRoom($input: AddRoomInput!){
          addRoom(input: $input) {
            room {
              id
              slug
              title
              entryCount
              appearance
              description
              schoolClass {
                id
                name
              }
            }
          }
        }
        """

    def test_create_new_room(self):
        self.assertEqual(Room.objects.count(), 0)
        title = 'some title'
        appearance = 'blue'
        result = self.get_client().execute(self.mutation, variables={
            'input': {
                'room': {
                    'title': title,
                    # description
                    'appearance': appearance
                }
            }
        })
        self.assertIsNone(result.errors)
        room = GQLRoom(result.data.get('addRoom').get('room'))
        self.assertEqual(room.title, title)
        self.assertEqual(room.appearance, appearance)
        self.assertIsNone(room.description)
        self.assertEqual(int(from_global_id(room.school_class.get('id'))[1]), self.teacher.selected_class.id)
        self.assertEqual(Room.objects.count(), 1)

    def test_create_new_room_for_other_school_class(self):
        self.assertEqual(Room.objects.count(), 0)
        result = self.get_client(self.teacher2).execute(self.mutation, variables={
            'input': {
                'room': {
                    'title': 'BIG NO NO!',
                    # description
                    'schoolClass': {
                      'id': self.school_class.graphql_id
                    },
                    'appearance': 'red'
                }
            }
        })

        self.assertEqual(Room.objects.count(), 1)

        room = Room.objects.all()[0]
        self.assertEqual(room.school_class.id, self.teacher2.selected_class.id)

