import json

from django.contrib.auth.models import User
from django.http import JsonResponse
from django.shortcuts import redirect, render

from .models import Game2048

# Create your views here.

# test_user
# 8!S#5RP!WVMACg


def game(request):

    return render(request, 'game_2048/index.html')


def set_result(request):
    user = request.user if str(
        request.user) != "AnonymousUser" else User.objects.get(username='test_user')

    if request.method == 'POST':
        # Get the game state from the POST request
        game_state = request.body

        obj = Game2048.objects.get(user=user)

        # Check if the game state idendical to the server game state
        if game_state != obj.game_state:
            # let string to JSON object
            json_game_state = json.loads(game_state)
            # extract value of best from JSON objest
            obj.best_score = json_game_state['best']
            obj.game_state = json_game_state  # save JSON object to game_state
            obj.save()
    else:
        return redirect('game_2048:game')

    return JsonResponse("", safe=False)


def get_result(request):
    # Check if user is logged in if not set user to test_user
    user = request.user if str(
        request.user) != "AnonymousUser" else User.objects.get(username='test_user')

    if request.method == 'GET':
        obj, created = Game2048.objects.get_or_create(user=user)
        game_state = obj.game_state

    return JsonResponse(game_state, safe=False)

[((17, 11, 17, 50), 'django.shortcuts.render', 'render', ({(17, 18, 17, 25): 'request', (17, 27, 17, 49): '"""game_2048/index.html"""'}, {}), "(request, 'game_2048/index.html')", False, 'from django.shortcuts import redirect, render\n'), ((41, 11, 41, 39), 'django.http.JsonResponse', 'JsonResponse', (), '', False, 'from django.http import JsonResponse\n'), ((53, 11, 53, 47), 'django.http.JsonResponse', 'JsonResponse', (), '', False, 'from django.http import JsonResponse\n'), ((22, 46, 22, 84), 'django.contrib.auth.models.User.objects.get', 'User.objects.get', (), '', False, 'from django.contrib.auth.models import User\n'), ((39, 15, 39, 41), 'django.shortcuts.redirect', 'redirect', ({(39, 24, 39, 40): '"""game_2048:game"""'}, {}), "('game_2048:game')", False, 'from django.shortcuts import redirect, render\n'), ((47, 46, 47, 84), 'django.contrib.auth.models.User.objects.get', 'User.objects.get', (), '', False, 'from django.contrib.auth.models import User\n'), ((33, 30, 33, 52), 'json.loads', 'json.loads', ({(33, 41, 33, 51): 'game_state'}, {}), '(game_state)', False, 'import json\n')]