import disnake
from disnake import Option, OptionType, Colour
from disnake.ext import commands

from integral_lib.Logger import logger


class General(commands.Cog):
    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",
        options=[
            Option("user",
                   "Specify any user",
                   OptionType.user),
        ]
    )
    async def info(self, inter: disnake.ApplicationCommandInteraction, user=None):
        """
        Shows user info {{SLASH_INFO}}

        Parameters
        ----------
        :param inter:
        :param user: 
        :return:
        """
        user = user or inter.author

        rolelist = [r.mention for r in user.roles if r != inter.guild.default_role]
        if rolelist:
            roles = "\n".join(rolelist)
        else:
            roles = "Not added any role"

        emb = disnake.Embed(
            title="General information",
            description=f"General information on server about {user}",
            color=Colour.random()
        )
        emb.set_thumbnail(url=user.display_avatar)
        emb.add_field(name="General info",
                      value=f"Username: {user}\n"
                            f"Nickname: {user.nick}\n"
                            f"Joined at: {user.joined_at.strftime('%A, %B %d %Y @ %H:%M:%S')}", inline=False)
        emb.add_field(name="Roles list", value=f"{roles}")
        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(General(bot))  # adding a cog
