#!/usr/bin/env python3
import asyncio
import os

import disnake
from disnake import OptionType, Option
from disnake.ext import commands
from __init__ import version_info as ver
from lib import work_with_cogs, cog_list
from lib import preload_checks, determine_prefix
from lib import logger

preload_checks()


intents = disnake.Intents(messages=True,
                          guilds=True,
                          message_content=True,
                          voice_states=True,
                          members=True,
                          presences=True
                          )

bot = commands.Bot(command_prefix=determine_prefix,
                   intents=intents,
                   reload=True,

                   )


asyncio.run(work_with_cogs('load', bot, asyncio.run(cog_list())))


@bot.event
async def on_ready():
    logger.info('Bot started')
    logger.info(f'Disnake version {disnake.__version__}')
    logger.info('We have logged in as {0.user}'.format(bot))
    logger.info(f'Version of bot is - v{ver.major}.{ver.minor}.{ver.micro}-{ver.releaselevel}')


@bot.slash_command(
    name="cog",
    description="Work with cogs",
    options=[
        Option(
            "what_do",
            description="Specify what do with cogs",
            type=OptionType.string,
            required=True,
            choices=["load", "unload", "reload", "enable", "disable"]
        ),
        Option(
            'cog',
            description="specify cog",
            type=OptionType.string
        )
    ]
)
@commands.is_owner()
async def slash_cogs(inter: disnake.ApplicationCommandInteraction, what_do, cog: str):
    await work_with_cogs(what_do, bot, cog)
    await inter.response.send_message(f'Cog {cog} is {what_do}ed', ephemeral=True)


@slash_cogs.autocomplete('cog')
async def _cog_opt(inter: disnake.ApplicationCommandInteraction, current: str, what_do):
    current = current.lower()
    if what_do == 'enable':
        _list = await cog_list('./cogs/disabled/')
    else:
        _list = await cog_list()
    return [choice for choice in _list if current in choice.lower()]


@slash_cogs.error
async def cogs_error(inter, what_do):
    await inter.response.send_message('Error', ephemeral=True)
    logger.error(f'User {inter.author} tries to use cogs func\n{what_do}\n')


bot.run(os.getenv('TOKEN'))
