import discord
from discord.ext import commands
import aiohttp
from urllib import parse


class Overwatch:
    def __init__(self, bot):
        self.bot = bot

    @commands.command(aliases=['ow'])
    async def overwatch(self, ctx, battletag: str, region: str):
        """krijg stats van iemand met een overwatch account"""
        tag = parse.quote(battletag.replace('#', '-'))
        async with aiohttp.ClientSession as sess:
            headers = {'User-Agent': 'Linux.Jappenbot'}
            async with sess.get(f'https://owapi.net/api/v3/u/{tag}/blob', headers=headers) as resp:
                data = await resp.json()
                wins = (data[region]['stats']['competitive']['overall_stats']['wins'])
                losses = (data[region]['stats']['competitive']['overall_stats']['losses'])
                sr = (data[region]['stats']['competitive']['overall_stats']['comprank'])
                prestige = (data[region]['stats']['competitive']['overall_stats']['prestige'])
                level = (data[region]['stats']['competitive']['overall_stats']['level'])
                final_level = (prestige * 100) + level
                avatar = (data[region]['stats']['competitive']['overall_stats']['avatar'])
                win_rate = (data[region]['stats']['competitive']['overall_stats']['win_rate'])
                final_winrate = f'{win_rate}%'
                embed = discord.Embed(title=battletag, color=0xFF69B4)
                embed.add_field(name='Wins', value=wins, inline=False)
                embed.add_field(name='Loses', value=losses, inline=True)
                embed.add_field(name='Winrate', value=final_winrate, inline=False)
                embed.add_field(name='Level', value=final_level, inline=True)
                embed.add_field(name='Skillrating', value=sr, inline=False)
                embed.set_thumbnail(url=avatar)
                embed.set_footer(text='https://www.owapi.net')
                await ctx.send(embed=embed)


def setup(bot):
    bot.add_cog(Overwatch(bot))