from django.shortcuts import render, HttpResponse
from django.http import HttpResponseBadRequest
from django.views.generic import TemplateView
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt

import random
import time

# Create your views here.

from game.models import Level


class CSRFexemptTemplateView(TemplateView):
    @method_decorator(csrf_exempt)
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)


class LoadLevel(CSRFexemptTemplateView):

    def post(self, request, *args, **kwargs):
        levelNum = request.POST.get("level_num")
        if levelNum is None or not levelNum.isdigit():
            return HttpResponseBadRequest()

        level = Level.objects.filter(id=int(levelNum)).first()
        if level is None:
            return HttpResponseBadRequest()

        flashVars = level.getFlashVars(seperator="&")
        return HttpResponse(flashVars)


class RandomLevel(CSRFexemptTemplateView):
    def post(self, request, *args, **kwargs):
        level = Level.objects.order_by("?").first()
        flashVars = level.getFlashVars(seperator="&")
        return HttpResponse(flashVars)

    def get(self, request, *args, **kwargs):
        level = Level.objects.order_by("?").first()
        flashVars = level.getFlashVars(seperator="&")
        return HttpResponse(flashVars)

class CompleteLevel(CSRFexemptTemplateView):
    def post(self, request, *args, **kwargs):
        pass

class RateLevel(CSRFexemptTemplateView):
    def post(self, request, *args, **kwargs):
        pass
