
from email import message
import imp
from multiprocessing import AuthenticationError
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.settings import api_settings
from rest_framework import filters
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status,viewsets
from rest_framework.authentication import TokenAuthentication
from profiles_api import serializers,models,permissions



class helloApiView(APIView):
    """Test APIView"""

    serializer_class=serializers.HelloSerializers   
    def get(self,request,format=None):
        """Returns a list of APIView Features"""

        an_apiview=[
        'Uses HTTP Menthofs as functiosn (get, post, patch)',
        'Is similar to a tradational Django view'
        'Gives you the most control over application logic',
        'Is mapped manually to URLs'
        ]

        return Response({'message':'Hello!','an_apiview':an_apiview})

    def post(self, request):
        serializer=self.serializer_class(data=request.data)

        if serializer.is_valid():
            name=serializer.validated_data.get("name")
            message=f'Hello { name }'
            return Response({'message':message})
        
        else:
            return Response(
                serializer.errors,
                status=status.HTTP_400_BAD_REQUEST
                 )


    def put(self,request, pk=None):
        """Handle updating and object"""

        return Response({'method':'PUT'})

    def patch(self,request,pk=None):
        """Handle the partial update"""

        return Response({'method':'PATCH'})

    def delete(self,request,pk=None):
        """Delete and object"""
        return Response({"method":"Delete"})





class HelloViewSets(viewsets.ViewSet):
    """Test API ViewSet"""
    serializer_class = serializers.HelloSerializers

    def list(self, request):
        """Return a hello message."""
        
        a_viewSet=[
            'Line 1',
            'Line 2',
            'Line 3',
            'Line 4'
        ]
        return Response({'message':'Hello','data':a_viewSet})

    
    def create(self,request):
        """Create a data"""
        serializer=self.serializer_class(data=request.data)

        if serializer.is_valid():
            name=serializer.validated_data.get('name')
            message=f'Hello {name}'
            return Response({'message':message})

        else:
            return Response(
            serializer.errors,
            status=status.HTTP_400_BAD_REQUEST
            )
    def retrieve(self, request, pk=None):
        """Handle getting an object by its ID"""

        return Response({'http_method': 'GET'})

    def update(self, request, pk=None):
        """Handle updating an object"""

        return Response({'http_method': 'PUT'})

    def partial_update(self, request, pk=None):
        """Handle updating part of an object"""

        return Response({'http_method': 'PATCH'})

    def destroy(self, request, pk=None):
        """Handle removing an object"""

        return Response({'http_method': 'DELETE'})


class UserProfileViewSet(viewsets.ModelViewSet):
    """Handle creating, creating and updating profiles"""
    serializer_class = serializers.UserProfileSerializer
    queryset = models.UserProfile.objects.all()
    authentication_classes = (TokenAuthentication,)
    permission_classes = (permissions.UpdateOwnProfile,)
    filter_backends=(filters.SearchFilter,)
    search_fields=('name','email')


class UserLoginApiView(ObtainAuthToken):
    """Handle creating user authenication tokens"""
    renderer_classes=api_settings.DEFAULT_RENDERER_CLASSES
    