# Path: data_manager/data_utils.py
# def get_prefix(yasen, message: Message):
#     """
#     Get command prefix for a message.
#     :param yasen: the bot instance.
#     :param message: the message.
#     :return: the prefix for that message.
#     """
#     guild = message.guild
#     default = yasen.default_prefix
#     if not guild:
#         return default
#     try:
#         return yasen.data_manager.get_prefix(str(guild.id)) or default
#     except AttributeError:
#         return default
# 
# Path: scripts/discord_utils.py
# def leading_members(ctx: Context, msg: str) -> tuple:
#     """
#     Get leading mentions as members from a message.
#     :param ctx: the discord context.
#     :param msg: the message.
#     :return: A tuple of (a list of mentioned members, leftover message)
#     """
#     ids, left = leading_mentions(msg)
#     guild = ctx.guild
#     return [m for m in (get(guild.members, id=i) for i in set(ids)) if m], left
# 
# Path: world_of_warships/war_gaming.py
# async def get_clan_id(region: Region, wows_api: WowsAsync,
#                       logger, search) -> Optional[int]:
#     """
#     Get clan id by search term.
#     :param region: the region.
#     :param wows_api: the WowsAsync instance.
#     :param logger: the logger.
#     :param search: the search query.
#     :return: the clan id.
#     """
#     coro = wows_api.clans(region, search=search, language='en', limit=1)
#     return await __get_id(coro, logger, 'clan_id')
# 
# async def get_player_id(region: Region, wows_api: WowsAsync,
#                         logger, search) -> Optional[int]:
#     """
#     Get player id by search term.
#     :param region: the region.
#     :param wows_api: the WowsAsync instance.
#     :param logger: the logger.
#     :param search: the search query.
#     :return: the player id.
#     """
#     coro = wows_api.players(region, search, language='en', limit=1)
#     return await __get_id(coro, logger, 'account_id')


from discord.ext.commands import BadArgument, Context, Converter
from wowspy import Region
from data_manager.data_utils import get_prefix
from scripts.discord_utils import leading_members
from world_of_warships.war_gaming import get_clan_id as gcid, \
    get_player_id as gpid
class ConvertRegion(Converter):
    __slots__ = ()

    async def convert(self, ctx, argument) -> Region:
        try:
            return Region[str(argument).upper()] if argument else Region.NA
        except KeyError:
            raise BadArgument('Please enter a region in `NA, EU, RU, AS`')


async def get_player_id(ctx: Context, name, region: Region):
    if not name:
        raise BadArgument('Please enter a player name.')
    bot = ctx.bot
    wows_api = bot.wows_api
    logger = bot.logger
    data_manager = bot.data_manager
    members, _ = leading_members(ctx, name)
    if not members:
        id_ = await gpid(region, wows_api, logger, name)
        if id_ is not None:
            return id_
        raise BadArgument(f'Player **{name}** not found!')
    member = members[0]
    saved = data_manager.get_shame(
        str(ctx.guild.id), str(member.id), region.name
    )
    if saved:
        return saved
    else:

# Path: data_manager/data_utils.py
# def get_prefix(yasen, message: Message):
#     """
#     Get command prefix for a message.
#     :param yasen: the bot instance.
#     :param message: the message.
#     :return: the prefix for that message.
#     """
#     guild = message.guild
#     default = yasen.default_prefix
#     if not guild:
#         return default
#     try:
#         return yasen.data_manager.get_prefix(str(guild.id)) or default
#     except AttributeError:
#         return default
# 
# Path: scripts/discord_utils.py
# def leading_members(ctx: Context, msg: str) -> tuple:
#     """
#     Get leading mentions as members from a message.
#     :param ctx: the discord context.
#     :param msg: the message.
#     :return: A tuple of (a list of mentioned members, leftover message)
#     """
#     ids, left = leading_mentions(msg)
#     guild = ctx.guild
#     return [m for m in (get(guild.members, id=i) for i in set(ids)) if m], left
# 
# Path: world_of_warships/war_gaming.py
# async def get_clan_id(region: Region, wows_api: WowsAsync,
#                       logger, search) -> Optional[int]:
#     """
#     Get clan id by search term.
#     :param region: the region.
#     :param wows_api: the WowsAsync instance.
#     :param logger: the logger.
#     :param search: the search query.
#     :return: the clan id.
#     """
#     coro = wows_api.clans(region, search=search, language='en', limit=1)
#     return await __get_id(coro, logger, 'clan_id')
# 
# async def get_player_id(region: Region, wows_api: WowsAsync,
#                         logger, search) -> Optional[int]:
#     """
#     Get player id by search term.
#     :param region: the region.
#     :param wows_api: the WowsAsync instance.
#     :param logger: the logger.
#     :param search: the search query.
#     :return: the player id.
#     """
#     coro = wows_api.players(region, search, language='en', limit=1)
#     return await __get_id(coro, logger, 'account_id')


# Path: world_of_warships/shell_handler.py
from discord.ext.commands import BadArgument, Context, Converter
from wowspy import Region
from data_manager.data_utils import get_prefix
from scripts.discord_utils import leading_members
from world_of_warships.war_gaming import get_clan_id as gcid, \
    get_player_id as gpid

class ConvertRegion(Converter):
    __slots__ = ()

    async def convert(self, ctx, argument) -> Region:
        try:
            return Region[str(argument).upper()] if argument else Region.NA
        except KeyError:
            raise BadArgument('Please enter a region in `NA, EU, RU, AS`')


async def get_player_id(ctx: Context, name, region: Region):
    if not name:
        raise BadArgument('Please enter a player name.')
    bot = ctx.bot
    wows_api = bot.wows_api
    logger = bot.logger
    data_manager = bot.data_manager
    members, _ = leading_members(ctx, name)
    if not members:
        id_ = await gpid(region, wows_api, logger, name)
        if id_ is not None:
            return id_
        raise BadArgument(f'Player **{name}** not found!')
    member = members[0]
    saved = data_manager.get_shame(
        str(ctx.guild.id), str(member.id), region.name
    )
    if saved:
        return saved
    else:

        prefix = get_prefix(ctx.bot, ctx.message)