import asyncio
import config
import secrets
from aiohttp import ClientWebSocketResponse
from mipa.ext import commands
from mipac.models.note import Note
from prometheus_client import Counter, start_http_server

MATCHING_NOTES = Counter('matching_notes', 'Amount of notes in the antenna')

class MyBot(commands.Bot):
    def __init__(self):
        super().__init__()

    async def _connect_channel(self):
        await self.router.connect_channel(['antenna'], config.antenna)

    async def on_ready(self, ws: ClientWebSocketResponse):
        print(f'connected: {self.user.username}')
        await self._connect_channel()

    async def on_reconnect(self, ws: ClientWebSocketResponse):
        print('Disconnected from server. Will try to reconnect.')
        await self._connect_channel()

    async def on_note(self, note: Note):
        MATCHING_NOTES.inc()


if __name__ == '__main__':
    start_http_server(config.prometheus_port)

    bot = MyBot()
    asyncio.run(bot.start(config.instance, secrets.token))