
import json
from django.http import HttpResponse
from django.urls import reverse
import pytest
from atlas.models import CaseCollection, Finding, Condition
#from atlas.views import GenericExamViews
from pytest_django.asserts import assertRedirects, assertContains, assertQuerySetEqual
from django.test import TestCase


#@pytest.mark.django_db
def test_create_condition(db):
    abnormality = Condition.objects.create(name="Abnorm")
    assert abnormality.name == "Abnorm"


def test_condition_tree_structure(db):
    parent = Condition.objects.create(name="parent")

    child1 = Condition.objects.create(name="child1")
    child2 = Condition.objects.create(name="child2")

    parent.child.set([child1, child2])

    child1_1 = Condition.objects.create(name="child1_1")
    child1_2 = Condition.objects.create(name="child1_2")

    child1.child.set([child1_1, child1_2])

    assert len(parent.get_children()) == 2
    assert len(child1.get_children()) == 2

    assert len(child1.get_parents()) == 1
    assert parent in child1.get_parents()

    assert len(child2.get_parents()) == 1
    assert parent in child2.get_parents()


#@pytest.fixture
#def create_basic_user(db, django_user_model):
#    return django_user_model.objects.create_superuser("basicuser", "ross@xkjq.uk", "password")
#
#@pytest.fixture
#def create_exam(db):
#    return Exam.objects.create(name="test exam", exam_mode=True)
#
#def test_exam_creation(create_exam):
#    exams = Exam.objects.filter(name="test exam")
#    assert exams.exists()
#
#    exam = exams.first()
#
#    assert exam.time_limit == 35 * 60
#
#def test_exam_list(rf, admin_user, create_exam):
#    request = rf.get('/rapids/exam/')
#    # Remember that when using RequestFactory, the request does not pass
#    # through middleware. If your view expects fields such as request.user
#    # to be set, you need to set them explicitly.
#    # The following line sets request.user to an admin user.
#    request.user = admin_user
#    response = GenericExamViews.exam_list(request)
#    #print(response)
#    assert response.status_code == 200
#
#def test_exam_active_admin(rf, admin_user, create_exam, client):
#    request = rf.get(reverse('rapids:active_exams'))
#    request.user = admin_user
#    response = GenericExamViews.active_exams(request)
#    print(response.content)
#    assert response.status_code == 200
#
#def test_exam_active_basic_user(create_basic_user, create_exam, client):
#    client.login(username="basicuser", password="password")
#    response = client.get(reverse('rapids:active_exams'))
#    print(response.content)
#    assert response.status_code == 200

def test_create_case(db, create_case):
    case = create_case
    assert case.title == "Sample Case"

def test_create_series(db, create_series):
    series = create_series
    assert series



def test_collection(db, create_case_collection, create_case, make_case, client, create_user):
    collection: CaseCollection = create_case_collection

    # create some cases
    case1 = create_case
    case2 = make_case()
    case3 = make_case(title="Case 3")
    case4 = make_case(title="Case 4")

    collection.cases.set([case1, case2, case3, case4])

    case5 = make_case(title="Case 5")

    assert collection.cases.all().count() == 4

    collection.add_case(case5)

    assert collection.cases.all().count() == 5

    assert collection.get_case_by_index(0) == case1
    assert collection.get_case_by_index(1) == case2
    assert collection.get_case_by_index(2) == case3
    assert collection.get_case_by_index(3) == case4
    assert collection.get_case_by_index(4) == case5

    assert collection.get_absolute_url() == reverse("atlas:collection_detail", kwargs={"pk": collection.pk})

    assert collection.get_take_url() == reverse("atlas:collection_take_start", kwargs={"pk": collection.pk})

    start_order = collection.get_cases()

    new_order = [
        case5.pk,
        case1.pk,
        case2.pk,
        case3.pk,
        case4.pk
    ]
    response: HttpResponse = client.post(reverse("atlas:exam_json_edit", kwargs={"pk":collection.pk} ), json=new_order, follow=True)
    # Check that sorting the exam works

    # First attempt is not logged in
    assertRedirects(response, f"{reverse('login')}?next=%2Fatlas%2Fcollection%2F1%2Fjson_edit")

    client.force_login(create_user)

    # Random user cannot modify order
    response: HttpResponse = client.post(reverse("atlas:exam_json_edit", kwargs={"pk":collection.pk} ), json=new_order, follow=True)
    assert response.status_code == 403

    assertQuerySetEqual(start_order , collection.get_cases())

    # Once the user has been made an author, they can modify the order
    collection.add_author(create_user)
    response: HttpResponse = client.post(reverse("atlas:exam_json_edit", kwargs={"pk":collection.pk} ), data={"set_exam_order" : json.dumps(new_order)}, follow=True)
    assert response.status_code == 200

    assert collection.get_cases().count() == 5
    assert collection.get_case_by_index(0) == case5
    assert collection.get_case_by_index(1) == case1



