# from channels.generic.websocket import AsyncWebsocketConsumer
# import json

# class DashConsumer(AsyncWebsocketConsumer):

#     async def connect(self):

#         self.groupname = 'dashboard'
#         await self.channel_layer.group_add(
#             self.groupname,
#             self.channel_name,
#         )

#         await self.accept()

#     async def disconnect(self, close_code):
#         # await self.disconnect()
#         pass

#     async def receive(self, text_data):

#         print(">>>>", text_data)

#         pass

# class Notification(AsyncWebsocketConsumer):

#     async def connect(self):

#         self.groupname = 'notification'
#         await self.channel_layer.group_add(
#             self.groupname,
#             self.channel_name,
#         )

#         await self.accept()

#     async def disconnect(self, close_code):
#         # await self.disconnect()
#         pass

#     async def receive(self, text_data):

#         print("This is Notification : ", text_data)

#         pass
from channels.generic.websocket import WebsocketConsumer
import json
from asgiref.sync import async_to_sync


class NotificationConsumer(WebsocketConsumer):

    # Function to connect to the websocket
    def connect(self):
       # Checking if the User is logged in
        if self.scope["user"].is_anonymous:
            # Reject the connection
            self.close()
        else:
            # print(self.scope["user"])   # Can access logged in user details by using self.scope.user, Can only be used if AuthMiddlewareStack is used in the routing.py
            # Setting the group name as the pk of the user primary key as it is unique to each user. The group name is used to communicate with the user.
            self.group_name = str(self.scope["user"].pk)
            async_to_sync(self.channel_layer.group_add)(
                self.group_name, self.channel_name)
            self.accept()

    # Function to disconnet the Socket
    def disconnect(self, close_code):
        self.close()
        # pass

    # Custom Notify Function which can be called from Views or api to send message to the frontend
    def notify(self, event):
        self.send(text_data=json.dumps(event["text"]))
