#!/usr/bin/env python3
from os import getenv
from os.path import isfile

from disnake import OptionType, Option, Localized, ApplicationCommandInteraction, Intents, __version__
from disnake.ext.commands import Bot, is_owner
from dotenv import load_dotenv

from integral_lib.CogsPrep import work_with_cogs, cog_list
from integral_lib.Comands import determine_prefix
from integral_lib.Logger import logger

load_dotenv()
if not isfile('.env') or not getenv('CONF_FILE'):
    with open('.env', 'a', encoding='utf-8') as f:
        f.write("CONF_FILE='config.json'\n")
    load_dotenv()
if not isfile(getenv('CONF_FILE')):
    with open(getenv('CONF_FILE'), 'a', encoding='utf-8') as f:
        f.write("")

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

bot = Bot(command_prefix=determine_prefix,
          intents=intents,
          reload=True,
          test_guilds=[648126669122568215]
          )

bot.i18n.load("locale/")

work_with_cogs('load', bot, cog_list())


@bot.event
async def on_ready():
    logger.info('Bot started')
    logger.info(f'Disnake version {__version__}')
    logger.info(f'We have logged in as {bot.user}')


@bot.slash_command(
    name='cog',
    options=[
        Option(
            name=Localized('cog', key="COG".lower()),
            description=Localized("cog file", key="COG_FILE"),
            type=OptionType.string
        )
    ]
)
@is_owner()
async def slash_cogs(inter: ApplicationCommandInteraction):
    """
    Working with cogs (modules) {{SLASH_COG}}

    Parameters
    ----------
    :param inter:
    """
    pass


@slash_cogs.sub_command(description=Localized("Enables Cog", key="ENABLE_COG"))
async def enable(inter: ApplicationCommandInteraction, cog: str):
    """

    Parameters
    ----------
    :param inter:
    :param cog: Select Cogfile {{COG_FILE}}
    """
    await work_with_cogs('enable', bot, cog)
    await inter.response.send_message(f'Cog {cog} is enabled', ephemeral=True)


@slash_cogs.sub_command(description=Localized("Disables Cog", key="DISABLE_COG"))
async def disable(inter: ApplicationCommandInteraction, cog: str):
    """

    Parameters
    ----------
    :param inter:
    :param cog: Select Cogfile {{COG_FILE}}
    """
    await work_with_cogs('disable', bot, cog)
    await inter.response.send_message(f'Cog {cog} is disabled', ephemeral=True)


@slash_cogs.sub_command(description=Localized("Loads Cog", key="LOAD_COG"))
async def load(inter: ApplicationCommandInteraction, cog: str):
    """

    Parameters
    ----------
    :param inter:
    :param cog: Select Cogfile {{COG_FILE}}
    """
    await work_with_cogs('load', bot, cog)
    await inter.response.send_message(f'Cog {cog} is loaded', ephemeral=True)


@slash_cogs.sub_command(description=Localized("Unload Cog", key="UNLOAD_COG"))
async def unload(inter: ApplicationCommandInteraction, cog: str):
    """

    Parameters
    ----------
    :param inter:
    :param cog: Select Cogfile {{COG_FILE}}
    """
    await work_with_cogs('unload', bot, cog)
    await inter.response.send_message(f'Cog {cog} is unload', ephemeral=True)


@slash_cogs.sub_command(description=Localized("Reloads Cog", key="RELOAD_COG"))
async def reload(inter: ApplicationCommandInteraction, cog: str):
    """

    Parameters
    ----------
    :param inter:
    :param cog: Select Cogfile {{COG_FILE}}
    """
    await work_with_cogs('reload', bot, cog)
    await inter.response.send_message(f'Cog {cog} is reloaded', ephemeral=True)


@disable.autocomplete('cog')
@unload.autocomplete('cog')
@load.autocomplete('cog')
@reload.autocomplete('cog')
async def _cog_opt(inter: ApplicationCommandInteraction, current: str):
    current = current.lower()
    _list = cog_list(fold='./cogs/')
    return [choice for choice in _list if current in choice.lower()]


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


@slash_cogs.error
async def cogs_error(inter: ApplicationCommandInteraction):
    await inter.response.send_message(Localized("Error", key="EROR"), ephemeral=True)
    logger.error(f'User {inter.author} tries to use cogs func')


bot.run(getenv('TOKEN'))
