import logging
from asyncio import sleep

import discord
from discord.ext import commands
from config import SETTINGS
from crew import crew_embed
from diary import diary_embed
from film import film_embed
from helpers import LetterboxdError
from list_ import list_embed
from review import review_embed
from user import user_embed

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s | %(message)s',
    datefmt='%m/%d %H:%M:%S')

bot = commands.Bot(command_prefix='!', case_insensitive=True)
bot.remove_command('help')


@bot.event
async def on_ready():
    logging.info(
        'Logged in %d servers as %s' % (len(bot.guilds), bot.user.name))
    bot.loop.create_task(update_stats())


@bot.event
async def on_message(message):
    if message.content.startswith('!'):
        message.content = message.content.replace('’', '').replace('‘', '')
        await bot.process_commands(message)


async def update_stats():
    while True:
        await bot.change_presence(
            activity=discord.Game('!helplb - {} servers'.format(
                len(bot.guilds))))
        await sleep(900)


@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send('This command requires a parameter.')
    elif isinstance(error, commands.BotMissingPermissions):
        await ctx.send('This command requires the {} permission.'.format(
                ', '.join(err for err in error.missing_perms)))
    elif isinstance(error, (commands.CommandNotFound, commands.CheckFailure)):
        return
    elif isinstance(error, commands.CommandInvokeError):
        if isinstance(error.original, discord.HTTPException):
            return
    else:
        await ctx.send('Sorry, the command crashed. :/')
        logging.error(ctx.message.content)
        raise error


async def send_msg(ctx, msg):
    if isinstance(msg, discord.Embed):
        await ctx.send(embed=msg)
    else:
        await ctx.send(msg)


# Commands


@bot.command()
async def helplb(ctx):
    help_embed = discord.Embed(colour=discord.Color.from_rgb(54, 57, 62))
    help_embed.set_thumbnail(url='https://i.imgur.com/Kr1diFu.png')
    help_embed.set_author(
        name='Letterboxd Bot', icon_url='https://i.imgur.com/5VALKVy.jpg')
    help_embed.set_footer(
        text='Created by Porkepik#2664',
        icon_url='https://i.imgur.com/li4cLpd.png')
    for key, value in SETTINGS['help'].items():
        help_embed.add_field(name=key, value=value, inline=False)
    help_embed.description = 'Invite Bot | '\
        + '[GitHub](https://github.com/Porkepik/Letterboxd-Bot)'
    await ctx.send(embed=help_embed)


@bot.command()
async def user(ctx, username):
    try:
        msg = await user_embed(username)
    except LetterboxdError as err:
        msg = err
    await send_msg(ctx, msg)


@bot.command()
async def diary(ctx, username):
    try:
        msg = await diary_embed(username)
    except LetterboxdError as err:
        msg = err
    await send_msg(ctx, msg)


@bot.command(aliases=['actor', 'actress', 'director'])
async def crew(ctx, *, arg):
    try:
        msg = await crew_embed(arg, ctx.invoked_with)
    except LetterboxdError as err:
        msg = err
    await send_msg(ctx, msg)


@bot.command(aliases=['movie'])
async def film(ctx, *, arg):
    try:
        # eiga.me ratings for specific servers
        if ctx.guild and ctx.guild.id in SETTINGS['mkdb_servers']:
            msg = await film_embed(arg, True)
        else:
            msg = await film_embed(arg)
    except LetterboxdError as err:
        msg = err
    await send_msg(ctx, msg)


async def check_if_two_args(ctx):
    msg = ctx.message.content.split()
    if len(msg) < 3:
        await ctx.send('This command requires 2 parameters.')
    return len(msg) > 2


@bot.command(name='list')
@commands.check(check_if_two_args)
async def list_(ctx, username, *args):
    try:
        msg = await list_embed(username, ' '.join(str(i) for i in args))
    except LetterboxdError as err:
        msg = err
    await send_msg(ctx, msg)


@bot.command(aliases=['entry'])
@commands.check(check_if_two_args)
async def review(ctx, username, *args):
    try:
        msg = await review_embed(username, ' '.join(str(i) for i in args))
    except LetterboxdError as err:
        msg = err
    await send_msg(ctx, msg)


@bot.command(name='del')
@commands.bot_has_permissions(manage_messages=True)
async def delete(ctx):
    await ctx.message.delete()
    found_bot_msg = False
    found_usr_cmd = False
    cmd_list = list()
    for command in bot.commands:
        cmd_list.append('!' + command.name)
        for alias in command.aliases:
            cmd_list.append('!' + alias)

    async for log_message in ctx.channel.history(limit=30):
        if log_message.author.id == bot.user.id and not found_bot_msg:
            bot_message = log_message
            found_bot_msg = True
        elif found_bot_msg:
            if log_message.content:
                first_word = log_message.content.split()[0]
            else:
                continue
            if first_word in cmd_list:
                found_usr_cmd = True
                cmd_message = log_message
                break

    if found_usr_cmd:
        if not ctx.author.permissions_in(ctx.channel).manage_messages:
            if not cmd_message.author.id == ctx.author.id:
                return
        await cmd_message.delete()
        await bot_message.delete()


bot.run(SETTINGS['discord'])

[((330, 435), 'logging.basicConfig', 'logging.basicConfig', ([], {'level': 'logging.INFO', 'format': '"""%(asctime)s | %(message)s"""', 'datefmt': '"""%m/%d %H:%M:%S"""'}), "(level=logging.INFO, format='%(asctime)s | %(message)s',\n    datefmt='%m/%d %H:%M:%S')\n", (349, 435), False, 'import logging\n'), ((452, 507), 'discord.ext.commands.Bot', 'commands.Bot', ([], {'command_prefix': '"""!"""', 'case_insensitive': '(True)'}), "(command_prefix='!', case_insensitive=True)\n", (464, 507), False, 'from discord.ext import commands\n'), ((3837, 3870), 'discord.ext.commands.check', 'commands.check', (['check_if_two_args'], {}), '(check_if_two_args)\n', (3851, 3870), False, 'from discord.ext import commands\n'), ((4109, 4142), 'discord.ext.commands.check', 'commands.check', (['check_if_two_args'], {}), '(check_if_two_args)\n', (4123, 4142), False, 'from discord.ext import commands\n'), ((4377, 4427), 'discord.ext.commands.bot_has_permissions', 'commands.bot_has_permissions', ([], {'manage_messages': '(True)'}), '(manage_messages=True)\n', (4405, 4427), False, 'from discord.ext import commands\n'), ((1099, 1109), 'asyncio.sleep', 'sleep', (['(900)'], {}), '(900)\n', (1104, 1109), False, 'from asyncio import sleep\n'), ((2078, 2112), 'discord.Color.from_rgb', 'discord.Color.from_rgb', (['(54)', '(57)', '(62)'], {}), '(54, 57, 62)\n', (2100, 2112), False, 'import discord\n'), ((2743, 2763), 'user.user_embed', 'user_embed', (['username'], {}), '(username)\n', (2753, 2763), False, 'from user import user_embed\n'), ((2924, 2945), 'diary.diary_embed', 'diary_embed', (['username'], {}), '(username)\n', (2935, 2945), False, 'from diary import diary_embed\n'), ((3143, 3176), 'crew.crew_embed', 'crew_embed', (['arg', 'ctx.invoked_with'], {}), '(arg, ctx.invoked_with)\n', (3153, 3176), False, 'from crew import crew_embed\n'), ((3469, 3490), 'film.film_embed', 'film_embed', (['arg', '(True)'], {}), '(arg, True)\n', (3479, 3490), False, 'from film import film_embed\n'), ((3529, 3544), 'film.film_embed', 'film_embed', (['arg'], {}), '(arg)\n', (3539, 3544), False, 'from film import film_embed\n'), ((1789, 1823), 'logging.error', 'logging.error', (['ctx.message.content'], {}), '(ctx.message.content)\n', (1802, 1823), False, 'import logging\n')]