# -*- coding: latin-1 -*-

import re
import xml.etree.ElementTree as ElementTree

import requests

from CommandTemplate import CommandTemplate
import GlobalStore


class Command(CommandTemplate):
	triggers = ['pokemon']
	helptext = "Looks up info on the provided Pokemon"
	callInThread = True  #WolframAlpha can be a bit slow

	def execute(self, message):
		replytext = u""
		if message.messagePartsLength == 0:
			replytext = u"Please provide the name of a Pokemon to search for"
		else:
			wolframReply = GlobalStore.commandhandler.runCommandFunction('fetchWolframAlphaData', None, "pokemon " + message.message, -1)
			if not wolframReply:
				replytext = u"Sorry, no WolframAlpha module found"
			elif not wolframReply[0]:
				replytext = wolframReply[1]
			else:
				pokemondata = {}
				dataKeysToKeep = ['name', u'Pokdex number', 'type', 'generation', 'species', 'evolves from', 'evolves into', 'natural abilities',
								  'hit points', 'attack', 'defense', 'special attack', 'special defense', 'speed']
				tableAsDict = self.turnWolframTableIntoDict(wolframReply[1])
				for key, value in tableAsDict.iteritems():
					if key in dataKeysToKeep:
						value = re.sub(' *\| *', ', ', value)  #'type' for instance is displayed as 'fire  |  flying' sometimes. Clean that up
						pokemondata[key] = value
				if len(pokemondata) == 0:
					replytext = u"No data on that Pokemon was found, for some reason. Did you make a typo?"
				else:
					#Let's turn the collected data into something presentable!
					replytext = u"{name} ({generation} nr {Pokdex number}) is a {species} of type '{type}'."
					if 'evolves from' in pokemondata:
						replytext += u" Evolves from {evolves from}."
					if 'evolves into' in pokemondata:
						replytext += u" Evolves into {evolves into}."
					replytext += u" {hit points} HP, {attack} Atk, {defense} Def, "
					replytext += u"{special attack} SAtk, {special defense} SDef, {speed} Spd."
					replytext = replytext.format(**pokemondata)

		message.bot.sendMessage(message.source, replytext)

	def turnWolframTableIntoDict(self, text):
		text = text.replace('<plaintext>', '').replace('</plaintext>', '')
		tableAsDict = {}
		lines = text.splitlines()
		for line in lines:
			parts = line.split(r'|', 1)
			#print "turned '{}' into '{}'".format(line, ", ".join(parts))
			if len(parts) > 1:
				parts[0] = parts[0].strip()
				parts[1] = parts[1].strip()
				if len(parts[0]) > 0 and len(parts[1]) > 0:
					tableAsDict[parts[0]] = parts[1]
		return tableAsDict
@@ -1846,34 +1846,27 @@
 %7D Def, %22
+ %5C
 %0A%09%09%09%09%09
-replytext +=
+%09%09%09
  u%22%7Bspec
@@ -1919,16 +1919,88 @@
 ed%7D Spd.
+ %22 %5C%0A%09%09%09%09%09%09%09%09 u%22More info: http://bulbapedia.bulbagarden.net/wiki/%7Bname%7D
 %22%0A%09%09%09%09%09r

Add a link to Bulbapedia for the found Pokemon, for more info