import json
from http import HTTPMethod, HTTPStatus

from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair

from ..conftest import HttpApiProxy, LambdaContext


def test_get_enrollment(
    app,
    seeds,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = app.lambda_handler(
        http_api_proxy(
            raw_path='/enrollments/578ec87f-94c7-4840-8780-bb4839cc7e64',
            method=HTTPMethod.GET,
        ),
        lambda_context,
    )
    assert r['statusCode'] == HTTPStatus.OK

    body = json.loads(r['body'])
    assert 'user' in body
    assert 'course' in body


def test_get_scormset(
    app,
    seeds,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    r = app.lambda_handler(
        http_api_proxy(
            raw_path='/enrollments/9c166c5e-890f-4e77-9855-769c29aaeb2e/scorm',
            method=HTTPMethod.GET,
        ),
        lambda_context,
    )
    assert r['statusCode'] == HTTPStatus.OK

    body = json.loads(r['body'])
    print(body)


def test_post_scormset(
    app,
    seeds,
    dynamodb_persistence_layer: DynamoDBPersistenceLayer,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    scormbody = {
        'suspend_data': '{"v":2,"d":[123,34,112,114,111,103,114,101,115,115,34,58,256,108,263,115,111,110,265,267,34,48,266,256,112,266,49,53,44,34,105,278,276,287,99,281,284,286,275,277,275,290,58,49,125,300,284,49,289,291,285,287,295,256,297,299,302,304,298,125,284,50,313,299,301,34,317,275,293,123,320,51,287,324,320,52,328,278,320,53,332,267,320,54,336,325,315,34,55,340,320,56,345,342,57,348,302,308,306,337,342,49,303,323,333,356,322,256,329,300,365,125],"cpv":"_lnxccXW"}',
        'launch_data': '',
        'comments': '',
        'comments_from_lms': '',
        'core': {
            'student_id': '',
            'student_name': '',
            'lesson_location': '',
            'credit': '',
            'lesson_status': 'incomplete',
            'entry': '',
            'lesson_mode': 'normal',
            'exit': 'suspend',
            'session_time': '00:00:00',
            'score': {'raw': '', 'min': '', 'max': '100'},
            'total_time': '00:00:00',
        },
        'objectives': {},
        'student_data': {
            'mastery_score': '',
            'max_time_allowed': '',
            'time_limit_action': '',
        },
        'student_preference': {'audio': '', 'language': '', 'speed': '', 'text': ''},
        'interactions': {},
    }

    r = app.lambda_handler(
        http_api_proxy(
            raw_path='/enrollments/578ec87f-94c7-4840-8780-bb4839cc7e64/scorm',
            method=HTTPMethod.POST,
            body=scormbody,
        ),
        lambda_context,
    )
    assert r['statusCode'] == HTTPStatus.NO_CONTENT

    r = dynamodb_persistence_layer.collection.get_item(
        KeyPair('578ec87f-94c7-4840-8780-bb4839cc7e64', 'SCORM_COMMIT#LAST')
    )
    assert r['cmi']['suspend_data'] == scormbody['suspend_data']
