from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

from .serializers import MusicSessionSerializer
from .utils import player


class MusicSession(APIView):
    serializer_class = MusicSessionSerializer

    def put(self, request, *args, **kwargs):
        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)
        source = serializer.validated_data['source']
        duration = serializer.validated_data['duration']
        player.playlist_start(source, duration * 60)
        return Response(data=serializer.data, status=status.HTTP_200_OK)

    def delete(self, request, *args, **kwargs):
        player.playlist_stop()
        return Response(status=status.HTTP_204_NO_CONTENT)
