"""Views for the chat app."""

from rest_framework import permissions
from rest_framework.generics import ListAPIView, CreateAPIView, RetrieveUpdateDestroyAPIView, get_object_or_404

from aparnik.contrib.messaging.utils import notify

from .serializers import *


# chat session
class ChatSessionListAPIView(ListAPIView):
    serializer_class = ChatSessionListSerializer
    permission_classes = [permissions.IsAuthenticated]
    filter_class = []

    def get_queryset(self):
        user = self.request.user
        queryset = ChatSession.objects.user_session(user=user)
        return queryset


class ChatSessionDetailAPIView(RetrieveUpdateDestroyAPIView):
    serializer_class = ChatSessionDetailsSerializer
    queryset = ChatSession.objects.all()
    permission_classes = [permissions.IsAuthenticated]
    lookup_url_kwarg = 'uri'
    lookup_field = 'uri'


class ChatSessionCreateAPIView(CreateAPIView):
    serializer_class = ChatSessionDetailsSerializer
    permission_classes = [permissions.IsAuthenticated]

    def get_queryset(self):
        return ChatSession.objects.all()

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


# chat member
class ChatSessionMemberListAPIView(ListAPIView):
    serializer_class = ChatSessionMemberListSerializer
    permission_classes = [permissions.IsAuthenticated]
    filter_class = []

    def get_queryset(self):
        user = self.request.user
        uri = None
        if 'uri' in self.request.parser_context['kwargs']:
            uri = self.request.parser_context['kwargs']['uri']
        return ChatSessionMember.objects.filter(chat_session__uri=uri)


class ChatSessionMemberDetailAPIView(RetrieveUpdateDestroyAPIView):
    serializer_class = ChatSessionMemberDetailsSerializer
    queryset = ChatSessionMember.objects.all()
    permission_classes = [permissions.IsAuthenticated]
    lookup_url_kwarg = 'id'
    lookup_field = 'id'
    # TODO: check permission for delete id


# message list
class ChatSessionMessageListAPIView(ListAPIView):
    serializer_class = ChatSessionMessageListSerializer
    permission_classes = [permissions.IsAuthenticated]
    filter_class = []

    def get_queryset(self):
        user = self.request.user
        uri = None
        if 'uri' in self.request.parser_context['kwargs']:
            uri = self.request.parser_context['kwargs']['uri']
        return ChatSessionMessage.objects.filter(chat_session__uri=uri).order_by('-created_at')


class ChatSessionMessageDetailAPIView(RetrieveUpdateDestroyAPIView):
    serializer_class = ChatSessionMessageDetailsSerializer
    queryset = ChatSessionMessage.objects.all()
    permission_classes = [permissions.IsAuthenticated]
    lookup_url_kwarg = 'id'
    lookup_field = 'id'
    # TODO: check permission for delete id


class ChatSessionMessageCreateAPIView(CreateAPIView):
    serializer_class = ChatSessionMessageDetailsSerializer
    permission_classes = [permissions.IsAuthenticated]

    def get_queryset(self):
        return ChatSession.objects.all()

    def perform_create(self, serializer):
        user = self.request.user
        chat_session = None
        if 'uri' in self.request.parser_context['kwargs']:
            uri = self.request.parser_context['kwargs']['uri']
            chat_session = get_object_or_404(ChatSession.objects.all(), uri=uri)
        serializer.save(user=user, chat_session=chat_session)
        notify(
            uri=chat_session.uri,
            source=user,
            source_display_name=user.get_full_name(),
            recipient=None,
            action='Create',
            obj=serializer.instance,
            short_description='You a new message',
            extra_data={
                'uri': chat_session.uri,
                'message': serializer.data,
            },
            channels=['websocket'],
            silent=True,
        )
##
# class ChatSessionMessageView(APIView):
#     """Create/Get Chat session messages."""
#
#     permission_classes = (permissions.IsAuthenticated,)
#
#     def get(self, request, *args, **kwargs):
#         """return all messages in a chat session."""
#         uri = kwargs['uri']
#
#         chat_session = ChatSession.objects.get(uri=uri)
#         messages = [chat_session_message.to_json()
#                     for chat_session_message in chat_session.messages.all()]
#
#         return Response({
#             'id': chat_session.id, 'uri': chat_session.uri,
#             'messages': messages
#         })
#
#     def post(self, request, *args, **kwargs):
#         """create a new message in a chat session."""
#         uri = kwargs['uri']
#         message = request.data['message']
#
#         user = request.user
#         chat_session = ChatSession.objects.get(uri=uri)
#
#         chat_session_message = ChatSessionMessage.objects.create(
#             user=user, chat_session=chat_session, message=message
#         )
#
#         notif_args = {
#             'source': user,
#             'source_display_name': user.get_full_name(),
#             'category': 'chat', 'action': 'Sent',
#             'obj': chat_session_message.id,
#             'short_description': 'You a new message', 'silent': True,
#             'extra_data': {
#                 'uri': chat_session.uri,
#                 'message': chat_session_message.to_json()
#             }
#         }
#         notify.send(
#             sender=self.__class__, **notif_args, channels=['websocket']
#         )
#
#         return Response({
#             'status': 'SUCCESS', 'uri': chat_session.uri, 'message': message,
#             'user': deserialize_user(user)
#         })
