
import discord
from discord.ext import commands

import pycountry

from utilities import checks

async def setup(bot):
	await bot.add_cog(Osu(bot))

class Osu(commands.Cog):
	
	def __init__(self, bot):
		self.bot = bot
		self.load_emoji()
	
	@commands.Cog.listener()
	async def on_ready(self):
		self.load_emoji()
	
	def load_emoji(self):
		# TODO: Check only within Emoji Server emojis?
		self.ssh_emoji = discord.utils.get(self.bot.emojis, name = "osu_ssh") or "SS+"
		self.ss_emoji = discord.utils.get(self.bot.emojis, name = "osu_ss") or "SS"
		self.sh_emoji = discord.utils.get(self.bot.emojis, name = "osu_sh") or "S:"
		self.s_emoji = discord.utils.get(self.bot.emojis, name = "osu_s") or 'S'
		self.a_emoji = discord.utils.get(self.bot.emojis, name = "osu_a") or 'A'
	
	async def cog_check(self, ctx):
		return await checks.not_forbidden().predicate(ctx)
	
	@commands.group(
		aliases = ["osu!"],
		case_insensitive = True, invoke_without_command = True
	)
	async def osu(self, ctx):
		'''osu!'''
		await ctx.send_help(ctx.command)
	
	@osu.group(case_insensitive = True, invoke_without_command = True)
	async def taiko(self, ctx):
		'''Taiko'''
		await ctx.send_help(ctx.command)
	
	@osu.group(case_insensitive = True, invoke_without_command = True)
	async def mania(self, ctx):
		'''osu!mania'''
		await ctx.send_help(ctx.command)
	
	@osu.command()
	async def user(self, ctx, *, user: str):
		'''General user information'''
		await self.get_user(ctx, user)
	
	@taiko.command(name = "user")
	async def taiko_user(self, ctx, *, user: str):
		'''General user information'''
		await self.get_user(ctx, user, 1)
	
	@mania.command(name = "user")
	async def mania_user(self, ctx, *, user: str):
		'''General user information'''
		await self.get_user(ctx, user, 2)
	
	async def get_user(self, ctx, user, mode = 0):
		url = "https://osu.ppy.sh/api/get_user"
		params = {'k': ctx.bot.OSU_API_KEY, 'u': user, 'm': mode}
		async with ctx.bot.aiohttp_session.get(url, params = params) as resp:
			data = await resp.json()
		
		if not data:
			await ctx.embed_reply(
				f"{ctx.bot.error_emoji} Error: User not found"
			)
			return
		
		data = data[0]
		country_name = pycountry.countries.get(alpha_2 = data["country"]).name
		await ctx.embed_reply(
			title = data["username"],
			title_url = f"https://osu.ppy.sh/users/{data['user_id']}",
			fields = (
				("Ranked Score", f"{int(data['ranked_score']):,}"),
				("Hit Accuracy", f"{float(data['accuracy']):6g}%"),
				("Play Count", data["playcount"]),
				("Total Score", f"{int(data['total_score']):,}"),
				("Performance", f"{float(data['pp_raw']):,}pp"),
				("Rank", f"#{int(data['pp_rank']):,}"),
				("Level", data["level"]),
				("Country Rank", f"{country_name} #{int(data['pp_country_rank']):,}"),
				("Total Hits", f"{int(data['count300']) + int(data['count100']) + int(data['count50']):,}"),
				("300 Hits", f"{int(data['count300']):,}"),
				("100 Hits", f"{int(data['count100']):,}"),
				("50 Hits", f"{int(data['count50']):,}"),
				(self.ssh_emoji, data["count_rank_ssh"]),
				(self.ss_emoji, data["count_rank_ss"]),
				(self.sh_emoji, data["count_rank_sh"]),
				(self.s_emoji, data["count_rank_s"]),
				(self.a_emoji, data["count_rank_a"])
			)
		)


@@ -992,18 +992,18 @@
 :%0A%09%09
-'''
+%22%22%22
 osu!
-'''
+%22%22%22
 %0A%09%09a
@@ -1139,19 +1139,19 @@
 :%0A%09%09
-'''
+%22%22%22
 Taiko
-'''
+%22%22%22
 %0A%09%09a
@@ -1283,19 +1283,19 @@
 ctx):%0A%09%09
-'''
+%22%22%22
 osu!mani
@@ -1295,19 +1295,19 @@
 su!mania
-'''
+%22%22%22
 %0A%09%09await
@@ -1388,35 +1388,35 @@
 , user: str):%0A%09%09
-'''
+%22%22%22
 General user inf
@@ -1415,35 +1415,35 @@
 user information
-'''
+%22%22%22
 %0A%09%09await self.ge
@@ -1535,35 +1535,35 @@
 , user: str):%0A%09%09
-'''
+%22%22%22
 General user inf
@@ -1562,35 +1562,35 @@
 user information
-'''
+%22%22%22
 %0A%09%09await self.ge
@@ -1693,19 +1693,19 @@
 str):%0A%09%09
-'''
+%22%22%22
 General 
@@ -1724,11 +1724,11 @@
 tion
-'''
+%22%22%22
 %0A%09%09a

Normalize quotation mark type for Osu cog docstrings