from django.shortcuts import render
from django.views.generic import TemplateView

from bloonsa_game.models import Level
from users.util import bloonsa_util, actions


class IndexView(TemplateView):

    def get(self, request, *args, **kwargs):
        bloonsa_util.tag_player(request=request)
        return render(request, "bloonsa_game/index.html", context={})

class TermsView(TemplateView):

    def get(self, request, *args, **kwargs):
        bloonsa_util.tag_player(request=request)
        return render(request, "bloonsa_game/terms.html", context={})

class GameView(TemplateView):

    def get(self, request, *args, **kwargs):
        bloonsa_util.tag_player(request=request)
        # This init is for accounts made with 'createsuperuser' or originating from bloonsb
        player = bloonsa_util.init_player(request=request)
        total_levels = Level.objects.count()

        if type(kwargs.get("pk")) is int:
            level = Level.objects.get(id=kwargs["pk"])
            if level:
                bloonsa_util.log(player=player,
                                 action=actions.bloonsa_load_level_by_url,
                                 note=level)
                return render(request, "bloonsa_game/level.html", context={
                    "player": player,
                    "total_levels": total_levels,
                    "flashVars": level.get_flash_vars(seperator="&amp;"),
                    "levelTitle": level.title,
                    "levelAuthor": level.author,
                })
        return render(request, "bloonsa_game/game.html",
                      context={"player": player,
                               "total_levels": total_levels,})

class WIPView(TemplateView):

    def get(self, request, *args, **kwargs):
        return render(request, "bloonsa_game/error.html", context={})

    def post(self, request, *args, **kwargs):
        return render(request, "bloonsa_game/error.html", context={})