from games.models import (
    GameCategory,
    Game,
    Player,
    PlayerScore,
)
from games.serializers import (
    GameCategorySerializer, 
    GameSerializer,
    PlayerSerializer,
    PlayerScoreSerializer
)    
from rest_framework import generics
from rest_framework.response import Response
from rest_framework.reverse import reverse

from django.contrib.auth.models import User
from games.serializers import UserSerializer
from rest_framework import permissions
from games.permissions import IsOwnerOrReadOnly


class GameCategoryList(generics.ListCreateAPIView):
    queryset=GameCategory.objects.all()
    serializer_class=GameCategorySerializer
    name='gamecategory-list'


class GameCategoryDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset=GameCategory.objects.all()
    serializer_class=GameCategorySerializer
    name='gamecategory-detail'


class GameList(generics.ListCreateAPIView):
    queryset=Game.objects.all()
    serializer_class=GameSerializer
    name='game-list'
    permission_classes=(
        permissions.IsAuthenticatedOrReadOnly,
        IsOwnerOrReadOnly,
    )

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)



class GameDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset=Game.objects.all()
    serializer_class=GameSerializer
    name='game-detail'

class PlayerList(generics.ListCreateAPIView):
    queryset=Player.objects.all()
    serializer_class=PlayerSerializer
    name='player-list'

class PlayerDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset=Player.objects.all()
    serializer_class=PlayerSerializer
    name='player-detail'

class PlayerScoreList(generics.ListCreateAPIView):
    queryset=PlayerScore.objects.all()
    serializer_class=PlayerScoreSerializer
    name='playerscore-list'

class PlayerScoreDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset=PlayerScore.objects.all()
    serializer_class=PlayerScoreSerializer
    name='playerscore-detail'                       


class UserList(generics.ListAPIView):
    queryset=User.objects.all()
    serializer_class=UserSerializer
    name='user-list'

class UserDetail(generics.RetrieveAPIView):
    queryset=User.objects.all()
    serializer_class=UserSerializer
    name='user-detail'    

class ApiRoot(generics.GenericAPIView):
    name='api-root'

    def get(self, request, *args, **kwargs):
        return Response(
            {
                'players':reverse(PlayerList.name, request=request),
                'game-categories':reverse(GameCategoryList.name, request=request),
                'games':reverse(GameList.name, request=request),
                'scores':reverse(PlayerScoreList.name, request=request),
                'users':reverse(UserList.name, request=request),
            }
        )