import os

import disnake
import psutil
from disnake import ApplicationCommandInteraction
from disnake.ext import commands

from __init__ import version_info as ver
from integral_lib.Comands import determine_prefix
from integral_lib.Logger import logger


class BotInfo(commands.Cog, name='Bot Info'):
    def __init__(self, bot: commands.Bot):
        self.bot = bot  # defining src as global var in class

    @commands.Cog.listener()  # this is a decorator for events/listeners
    async def on_ready(self):
        logger.info(f'Cog {__name__.split(".")[1]} is ready!.')

    @commands.slash_command(name="info_bot",
                            description='Shows general info about src')  # this is for making a command
    async def info_bot(self, inter: ApplicationCommandInteraction):
        _pid = os.getpid()
        _process = psutil.Process(_pid)
        emb = disnake.Embed(
            title="General information",
            description="General information on about src",
        )
        emb.set_thumbnail(self.bot.user.avatar.url)
        emb.add_field(name="System info:", value=f"Memory Usage: {round(_process.memory_info().rss / 2 ** 20, 2)} Mb\n"
                                                 f"CPU Usage: {_process.cpu_percent()}%\n"
                                                 f'Bot ping: {round(self.bot.latency * 1000)}\n'
                                                 f'Prefix: `{determine_prefix(self.bot, inter)}\n`'
                      )
        emb.add_field(name="Bot info:", value="Bot owner: <@386629192743256065>\n"
                                              f"Bot version: {ver.major}.{ver.minor}.{ver.micro}-{ver.releaselevel}")
        emb.set_footer(text=f"Information requested by: {inter.author.display_name}")

        await inter.response.send_message(embed=emb, ephemeral=True)


def setup(bot):  # an extension must have a setup function
    bot.add_cog(BotInfo(bot))  # adding a cog
