
from discord.ext import commands

import collections
import csv
import sys

from utilities import checks

sys.path.insert(0, "..")
from units.runescape import get_ge_data, get_item_id, get_monster_data, UnitOutputError
sys.path.pop(0)

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

class RuneScape(commands.Cog):
	
	def __init__(self, bot):
		self.bot = bot
	
	async def cog_check(self, ctx):
		return await checks.not_forbidden().predicate(ctx)
	
	@commands.group(aliases = ["rs"], invoke_without_command = True, case_insensitive = True)
	async def runescape(self, ctx):
		'''RuneScape'''
		await ctx.send_help(ctx.command)
	
	@runescape.command(aliases = ["grandexchange", "grand_exchange"])
	async def ge(self, ctx, *, item: str):
		'''Grand Exchange'''
		try:
			item_id = await get_item_id(
				item, aiohttp_session = ctx.bot.aiohttp_session
			)
			data = await get_ge_data(
				item, item_id = item_id,
				aiohttp_session = ctx.bot.aiohttp_session
			)
		except UnitOutputError as e:
			await ctx.embed_reply(f"{ctx.bot.error_emoji} Error: {e}")
			return
		await ctx.embed_reply(
			title = data["name"],
			title_url = f"https://services.runescape.com/m=itemdb_rs/viewitem?obj={item_id}",
			thumbnail_url = data["icon_large"],
			description = data["description"],
			fields = (
				("Current", data["current"]["price"]),
				("Today", data["today"]["price"]),
				("30 Day", data["day30"]["change"]),
				("90 Day", data["day90"]["change"]),
				("180 Day", data["day180"]["change"]),
				("Category", data["type"])
			)
		)
		# id?, members
	
	@runescape.command(aliases = ["bestiary"])
	async def monster(self, ctx, *, monster: str):
		'''Bestiary'''
		try:
			data = await get_monster_data(
				monster, aiohttp_session = ctx.bot.aiohttp_session
			)
		except UnitOutputError as e:
			return await ctx.embed_reply(f"{ctx.bot.error_emoji} Error: {e}")
		await ctx.embed_reply(
			data["description"],
			title = data["name"],
			fields = (
				("Level", data["level"]), ("Weakness", data["weakness"]),
				("XP/Kill", data["xp"]), ("Lifepoints", data["lifepoints"]),
				("Members", "Yes" if data["members"] else "No"),
				("Aggressive", "Yes" if data["aggressive"] else "No")
			)
		)
		# add other? - https://runescape.wiki/w/RuneScape_Bestiary#beastData
	
	@runescape.command(aliases = ["levels", "level", "xp", "ranks", "rank"])
	async def stats(self, ctx, *, username : str):
		'''Stats'''
		url = "http://services.runescape.com/m=hiscore/index_lite.ws"
		params = {"player": username}
		async with ctx.bot.aiohttp_session.get(url, params = params) as resp:
			if resp.status == 404:
				return await ctx.embed_reply(f"{ctx.bot.error_emoji} Player not found")
			data = await resp.text()
		data = csv.DictReader(data.splitlines(), fieldnames = ("rank", "level", "xp"))
		stats = collections.OrderedDict()
		stats_names = ("Overall", "Attack", "Defence", "Strength", "Constitution", "Ranged", 
						"Prayer", "Magic", "Cooking", "Woodcutting", "Fletching", "Fishing", 
						"Firemaking", "Crafting", "Smithing", "Mining", "Herblore", "Agility", 
						"Thieving", "Slayer", "Farming", "Runecrafting", "Hunter", "Construction", 
						"Summoning", "Dungeoneering", "Divination", "Invention")
		for stat in stats_names:
			stats[stat] = next(data)

		output = [f"`{name}`" for name in stats_names]
		fields = [("Skill", '\n'.join(output))]
		
		max_length = max(len(f"{int(values['rank']):,d}") for values in stats.values())
		output = [f"""`| {f"{int(values['rank']):,d}".rjust(max_length)}`""" for values in stats.values()]
		fields.append(("| Rank", '\n'.join(output)))
		
		max_length = max(len(f"{int(values['xp']):,d}") for values in stats.values())
		output = [f"""`| {values["level"].rjust(4).ljust(5)}| {f"{int(values['xp']):,d}".rjust(max_length)}`""" for values in stats.values()]
		fields.append(("| Level | Experience", '\n'.join(output)))
		
		title_url = f"http://services.runescape.com/m=hiscore/compare?user1={username.replace(' ', '+')}"
		await ctx.embed_reply(title = username, title_url = title_url, fields = fields)
	
	@runescape.command()
	async def wiki(self, ctx):
		'''WIP'''
		...
	
	@runescape.command(hidden = True)
	async def zybez(self, ctx):
		'''
		This command has been deprecated
		Zybez RuneScape Community was shut down on September 17th, 2018
		https://forums.zybez.net/topic/1783583-exit-post-the-end/
		'''
		# Previously used https://forums.zybez.net/runescape-2007-prices/api/?info
		await ctx.embed_reply("See https://forums.zybez.net/topic/1783583-exit-post-the-end/")


@@ -1795,23 +1795,16 @@
 s e:%0A%09%09%09
-return 
 await ct
@@ -1854,16 +1854,26 @@
 : %7Be%7D%22)%0A
+%09%09%09return%0A
 %09%09await 
@@ -1896,50 +1896,64 @@
 %0A%09%09%09
-data%5B%22description%22%5D,%0A%09%09%09title = data%5B%22name
+title = data%5B%22name%22%5D,%0A%09%09%09description = data%5B%22description
 %22%5D,%0A

Improve clarity of runescape monster command