from django.shortcuts import render
from .models import Tournament
from rest_framework import generics
from .serializers import TournamentSerializer
from django.http import HttpResponse
from django.template import loader
from django.contrib.auth.decorators import login_required

from django.shortcuts import redirect
from django.shortcuts import reverse

## https://docs.djangoproject.com/en/4.2/intro/tutorial03/

class TournamentCreate(generics.CreateAPIView):

	queryset = Tournament.objects.all(),

	serializer_class = TournamentSerializer

class TournamentList(generics.ListAPIView):

    queryset = Tournament.objects.all()

    serializer_class = TournamentSerializer

class TournamentDetail(generics.RetrieveAPIView):

    queryset = Tournament.objects.all()

    serializer_class = TournamentSerializer

class TournamentUpdate(generics.RetrieveUpdateAPIView):

    queryset = Tournament.objects.all()

    serializer_class = TournamentSerializer

class TournamentDelete(generics.RetrieveDestroyAPIView):

    queryset = Tournament.objects.all()

    serializer_class = TournamentSerializer

def reserveTournament(request, pk, user_pk):

    t = Tournament.objects.get(id=pk)

    t.rented_user_id = user_pk  # change field

    t.save() # this will update only

    ##

    latest_question_list = Tournament.objects.all()

    context = {
       "latest_question_list": latest_question_list,
        "status_message": ("You have successfull reserved the %s." % t.name),
    }

    if request.user.is_superuser:
    
        template = loader.get_template("manage.html")

    else:

        template = loader.get_template("reserve.html")

    return HttpResponse(template.render(context, request))

    latest_question_list = Tournament.objects.all()

    template = loader.get_template("reserve.html")

    context = {
       "latest_question_list": latest_question_list,
        "status_message": ("You have successfull reserved the %s." % t.name),
    }

    return HttpResponse(template.render(context, request))

def returnTournament(request, pk, user_pk):

    t = Tournament.objects.get(id=pk)

    t.rented_user_id = 0  # change field

    t.save() # this will update only

    ##

    latest_question_list = Tournament.objects.all()

    context = {
       "latest_question_list": latest_question_list,
        "status_message": ("You have successfull returned the %s." % t.name),
    }

    if request.user.is_superuser:
    
        template = loader.get_template("manage.html")

    else:

        template = loader.get_template("reserve.html")

    return HttpResponse(template.render(context, request))

#@login_required
def custom(request):
    print("custom custom custom")

    latest_question_list = Tournament.objects.all()

    context = {

        "latest_question_list": latest_question_list,
    }

    if request.user.is_superuser:
    
        template = loader.get_template("admin.html")

    else:

        template = loader.get_template("tournament.html")

    return HttpResponse(template.render(context, request))
