"""
lib.CogsPrepare
~~~~~~~~~~~~~
Loads, unloads Cogs files
cog_list: return list of cog filenames
work_with_cogs: loads, reloads and unloads cogs files

"""
from os import listdir, rename
from typing import List

from disnake.ext import commands

from .Logger import logger


async def cog_list(fold: str = './cogs') -> List[str]:
    cogs_list = []
    for _filename in listdir(fold):
        if _filename.endswith('.py'):
            cogs_list.append(_filename[:-3])
    return cogs_list


async def work_with_cogs(what_do, bot: commands.Bot, cog):
    if isinstance(cog, str):
        cog = cog.split()
    for _filename in cog:
        if what_do == "load":
            bot.load_extension(f'cogs.{_filename}')
            logger.info(f'Loaded cog {_filename}')
        elif what_do == 'unload':
            bot.unload_extension(f'cogs.{_filename}')
            logger.info(f'Cog {_filename} unloaded')
        elif what_do == 'reload':
            bot.reload_extension(f'cogs.{_filename}')
            logger.info(f'Cog {_filename} reloaded')
        elif what_do == 'disable':
            bot.unload_extension(f'cogs.{_filename}')
            rename(f'cogs/{_filename}.py', f'cogs/disabled/{_filename}.py')
            logger.info(f'Cog {_filename} stopped and disabled')
        elif what_do == 'enable':
            rename(f'cogs/disabled/{_filename}.py', f'cogs/{_filename}.py')
            bot.load_extension(f'cogs.{_filename}')
            logger.info(f'Cog {_filename} started and enabled')
