from typing import List

import disnake
from disnake import OptionChoice
from disnake.ext import commands

from integral_lib.Comands import write_json
from integral_lib.Logger import logger


class Fun(commands.Cog, name='Fun'):
    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.has_permissions(administrator=True)
    @commands.slash_command(
        name="set_bot_channel",
        description="Set channel which iterate with src",

    )
    async def set_bot_channel(self, inter: disnake.ApplicationCommandInteraction, channel: str):
        await write_json(inter.guild.id,
                         "channel",
                         disnake.utils.find(lambda d: d.name == channel, inter.guild.channels).id)
        await inter.response.send_message(f"Channel set up to {channel}", ephemeral=True)

    @set_bot_channel.autocomplete('channel')
    async def _list_text_channels(self,
                                  inter: disnake.ApplicationCommandInteraction,
                                  current: str) -> List[OptionChoice]:
        _list = [r.name for r in inter.guild.text_channels]
        return [
            OptionChoice(name=choice, value=choice)
            for choice in _list if current in choice
        ]


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