import asyncio
import sys
import os

sys.path.append(os.path.abspath("../src"))
from gumband_python_sdk import Gumband, GumbandOptions
from manifest import manifest
from gumband_python_sdk.constants import SocketEventType

exhibit_id = "1"
exhibit_token = "a2cd...edi23"


class GumbandService:
    def __init__(self, exhibit_id, token, manifest):
        """
        Initialize the GumbandService with necessary parameters.
        """
        self.gumband = Gumband(exhibit_id=exhibit_id, token=token, manifest=manifest, options=GumbandOptions(content_location="./content"))
        self.gumband.on(SocketEventType.READY.value, self.on_ready)
        self.gumband.on(SocketEventType.CONTROL_RECEIVED.value, self.on_control_received)
        self.gumband.on(SocketEventType.SETTING_RECEIVED.value, self.on_setting_received)
        self.gumband.on(SocketEventType.FILE_UPLOADED.value, self.on_file_uploaded)
        self.gumband.on(SocketEventType.FILE_DELETED.value, self.on_file_deleted)

    async def on_ready(self):
        """
        Handle the 'READY' event.
        """
        print("Gumband ready!")
        # Get the full manifest:
        # manifest_data = await self.gumband.get_manifest()
        # print(manifest_data)

        # Get a setting from Gumband:
        # my_setting = await self.gumband.get_setting()
        # print(my_setting)

        # Set a setting in Gumband:
        # await self.gumband.set_setting(manifest_id: str, new_setting_value: str|num)

        # Get a status from Gumband:
        # my_status = await self.gumband.get_status()
        # print(my_status)

        # Set a status in Gumband:
        # await self.gumband.set_status(manifest_id: str, new_status_value: str)

        # Send a user interaction event to Gumband:
        # await self.gumband.create_event(event_name: str, interaction_meta_data: dict)

        # Send an email notification to subscribed users:
        # await self.gumband.send_email_notification("Your attention, please. Your attention, please.")

    async def on_control_received(self, data):
        print(f"Control Received:{data}")

    async def on_setting_received(self, data):
        print(f"Setting Received:{data}")

    async def on_file_uploaded(self, data):
        print(f"File uploaded:{data}")
        # Sync local files with remote files.
        # await self.gumband.sync_content_files()

    async def on_file_deleted(self, data):
        print(f"File deleted:{data}")

    async def start(self):
        """
        Start the Gumband connection and keep it running.
        """
        await self.gumband.connect()
        while True:
            await asyncio.sleep(1)


# Instantiate and run the handler
if __name__ == "__main__":
    handler = GumbandService(
        exhibit_id=exhibit_id,
        token=exhibit_token,
        manifest=manifest,
    )
    asyncio.run(handler.start())
