from django.shortcuts import render
from rest_framework import generics
from django.http import JsonResponse
from mama_care_notification.apns import *
import json
from mama_care_notification.fcm import *
from mama_care.views import *
from mama_care_notification.models import *
from mama_care_notification.serializers import *

# Create your views here.

class SendNotificationMobile(generics.GenericAPIView):
    queryset = []

    def post(self, request, *args, **kwargs):
        data = request.data
        if "deviceToken" in data:
            deviceToken = data["deviceToken"]
            title = data["title"]
            body = data["body"]
            deviceOSType = data["deviceOSType"]
            if deviceOSType == "android":
                send_push_notification_android(deviceToken, title, body)
            elif deviceOSType == "ios":
                send_push_notification_ios(deviceToken, title, body)
        return JsonResponse({"data":{}},safe = False)

#User Device

class UserDeviceListMobileView(generics.ListCreateAPIView):
    authentication_classes = [APIKeyAuthentication, MobileTokenAuthentication]
    filter_backends = [DjangoFilterBackend]
    filterset_fields = ["parentProfileId","deviceOSTypeId"]
    #filterset_class = ParentFilter
    pagination_class = StandardSetPagination
    queryset = UserDevice.objects.all().order_by('-id')
    serializer_class = UserDeviceSerializer

class UserDeviceListDashboardView(generics.ListCreateAPIView):
    authentication_classes = [APIKeyAuthentication, DashboardTokenAuthentication]
    filter_backends = [DjangoFilterBackend]
    pagination_class = StandardSetPagination
    queryset = UserDevice.objects.all()
    serializer_class = UserDeviceSerializer

class UserDeviceDetailDashboardView(generics.RetrieveUpdateDestroyAPIView):
    authentication_classes = [APIKeyAuthentication, DashboardTokenAuthentication]
    filter_backends = [DjangoFilterBackend]
    lookup_field = "id"
    queryset = UserDevice.objects.all()
    serializer_class = UserDeviceSerializer

#Notification

class NotificationListMobileView(generics.ListAPIView):
    authentication_classes = [APIKeyAuthentication, MobileTokenAuthentication]
    filter_backends = [DjangoFilterBackend]
    filterset_fields = ["userDeviceId"]
    #filterset_class = ParentFilter
    pagination_class = StandardSetPagination
    queryset = Notification.objects.all()
    serializer_class = NotificationSerializer

class NotificationDetailMobileView(generics.UpdateAPIView):
    authentication_classes = [APIKeyAuthentication, MobileTokenAuthentication]
    filter_backends = [DjangoFilterBackend]
    #filterset_class = ParentFilter
    lookup_field = "id"
    queryset = Notification.objects.all()
    serializer_class = NotificationSerializer

class NotificationListDashboardView(generics.ListCreateAPIView):
    authentication_classes = [APIKeyAuthentication, DashboardTokenAuthentication]
    filter_backends = [DjangoFilterBackend]
    pagination_class = StandardSetPagination
    queryset = Notification.objects.all()
    serializer_class = NotificationSerializer

class NotificationDetailDashboardView(generics.RetrieveUpdateDestroyAPIView):
    authentication_classes = [APIKeyAuthentication, DashboardTokenAuthentication]
    filter_backends = [DjangoFilterBackend]
    lookup_field = "id"
    queryset = Notification.objects.all()
    serializer_class = NotificationSerializer