import json
import logging
import sys
import threading
import lib

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


bot_owner = 386629192743256065
lib.Commands.check_json()
intents = disnake.Intents(messages=True, guilds=True, message_content=True)
intents.members = True

bot = commands.Bot(command_prefix=lib.Commands.determine_prefix,
                   intents=intents,
                   status=disnake.Status.idle,
                   reload=True,
                   test_guilds=[929446191270330410, 987120286933602354])

threading.current_thread().name = "main"
logging.basicConfig(stream=sys.stdout, filemode='w', level='INFO',
                    format='%(asctime)s - %(levelname)s - %(threadName)s - %(message)s')


for filename in lib.CogsPrepare.cog_list():
    try:
        bot.load_extension(f'cogs.{filename}')
        logging.info(f'Loaded cog {filename}')

    except commands.ExtensionNotFound:
        logging.error(f"Error: {filename} couldn't be found to load.")

    except commands.ExtensionFailed as error:
        logging.error(f'Error: {filename} failed to load properly.', error)

    except commands.ExtensionError:
        logging.error(f'Error: unknown error with {filename}')


@bot.event
async def on_ready():
    logging.info(f'Bot started')
    logging.info('We have logged in as {0.user}'.format(bot))


@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=[
                OptionChoice("load", "load"),
                OptionChoice("unload", "unload"),
                OptionChoice("reload", "reload")
            ]
        )
    ]
)
async def slash_cogs(inter, what_do):
    if inter.author.id == bot_owner:
        await lib.CogsPrepare.work_with_cogs(what_do, bot)
        await inter.response.send_message(f'Cogs are {what_do}ed', ephemeral=True)

    else:
        await inter.response.send_message('You`re not bot owner', ephemeral=True)


bot.run('OTQ3OTUzOTAxNzgzNjIxNjYy.GTXbMv.KrztaTO7-ivsPEAVjsyikSQ-GP-ANwULmDraig')
