import os
from xml.etree import ElementTree
from .enums import GameTag, PlayReq, Race


_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.path.pardir, "data", "TextAsset")


class XMLCard(object):
	_tags = {
		"type": "CardType",
		"health": "Health",
		"durability": "Durability",
		"atk": "Atk",
		"cost": "Cost",
		"race": "Race",
		"charge": "Charge",
		"overload": "Recall",
		"poisonous": "Poisonous",
		"secret": "Secret",
		"windfury": "Windfury",
		"adjacentBuff": "AdjacentBuff",
		"oneTurnEffect": "OneTurnEffect",
		"hasAura": "Aura",
		"hasCombo": "Combo",
		"hasDeathrattle": "Deathrattle",
	}

	cantAttack = False

	def action(self):
		pass

	@classmethod
	def get(cls, id):
		from . import carddata
		if not hasattr(carddata, id):
			return cls(id)
		return getattr(carddata, id)(id)

	def __init__(self, id):
		self.file = os.path.join(_path, "%s.xml" % (id))
		self.xml = ElementTree.parse(self.file)

	def __getattribute__(self, name):
		parent = super()
		if name != "_tags" and name in self._tags:
			if hasattr(parent.__self_class__, name):
				return parent.__getattribute__(name)
			return self.getTag(self._tags[name])
		return parent.__getattribute__(name)

	def _reqParam(self, req):
		tags = self.xml.findall("Power/PlayRequirement[@reqID='%i']" % (req))
		if tags:
			return int(tags[0].attrib["param"])
		return 0


	def _getTag(self, element, locale="enUS"):
		type = element.attrib["type"]
		if type == "String":
			return element.find(locale).text

		value = int(element.attrib["value"])
		if type == "Bool":
			return bool(value)
		return value

	def getTag(self, name):
		element = self.xml.findall('./Tag[@name="%s"]' % (name))
		if not element:
			return 0
		return self._getTag(element[0])

	@property
	def tags(self):
		return {GameTag(int(e.attrib["enumID"])): self._getTag(e) for e in self.xml.findall("./Tag")}

	@property
	def spellpower(self):
		# Game treats as a bool. We default to 1 if present.
		return int(getTag("Spellpower"))

	@property
	def name(self):
		return self.xml.findall("./Tag[@name='CardName']/enUS")[0].text

	@property
	def entourage(self):
		cards = self.xml.findall("EntourageCard")
		return [tag.attrib["cardID"] for tag in cards]

	@property
	def requirements(self):
		reqs = self.xml.findall("Power[PlayRequirement]/PlayRequirement")
		return {PlayReq(int(tag.attrib["reqID"])) for tag in reqs}

	@property
	def minMinions(self):
		return self._reqParam(PlayReq.REQ_MINIMUM_TOTAL_MINIONS)

	@property
	def minTargets(self):
		return self._reqParam(PlayReq.REQ_MINIMUM_ENEMY_MINIONS)

	@property
	def targetMaxAttack(self):
		return self._reqParam(PlayReq.REQ_TARGET_MAX_ATTACK)

	@property
	def targetMinAttack(self):
		return self._reqParam(PlayReq.REQ_TARGET_MIN_ATTACK)

	@property
	def targetRace(self):
		race = self._reqParam(PlayReq.REQ_TARGET_WITH_RACE)
		if race:
			return Race(race)

@@ -233,144 +233,190 @@
 e%22: 
-%22CardType%22,%0A%09%09%22health%22: %22Health%22,%0A%09%09%22durability%22: %22Durability%22,%0A%09%09%22atk%22: %22Atk%22,%0A%09%09%22cost%22: %22Cost%22,%0A%09%09%22race%22: %22Race%22,%0A%09%09%22charge%22: %22Charge%22
+GameTag.CARDTYPE,%0A%09%09%22health%22: GameTag.HEALTH,%0A%09%09%22durability%22: GameTag.DURABILITY,%0A%09%09%22atk%22: GameTag.ATK,%0A%09%09%22cost%22: GameTag.COST,%0A%09%09%22race%22: GameTag.CARDRACE,%0A%09%09%22charge%22: GameTag.CHARGE
 ,%0A%09%09
@@ -431,16 +431,22 @@
 d%22: 
-%22Recall%22
+GameTag.RECALL
 ,%0A%09%09
@@ -462,101 +462,126 @@
 s%22: 
-%22Poisonous%22,%0A%09%09%22secret%22: %22Secret%22,%0A%09%09%22windfury%22: %22Windfury%22,%0A%09%09%22adjacentBuff%22: %22AdjacentBuff%22
+GameTag.POISONOUS,%0A%09%09%22secret%22: GameTag.SECRET,%0A%09%09%22windfury%22: GameTag.WINDFURY,%0A%09%09%22adjacentBuff%22: GameTag.ADJACENT_BUFF
 ,%0A%09%09
@@ -597,17 +597,24 @@
 ffect%22: 
-%22
+GameTag.
 OneTurnE
@@ -618,17 +618,16 @@
 rnEffect
-%22
 ,%0A%09%09%22has
@@ -637,14 +637,20 @@
 a%22: 
-%22Aura%22
+GameTag.AURA
 ,%0A%09%09
@@ -665,15 +665,21 @@
 o%22: 
-%22Combo%22
+GameTag.COMBO
 ,%0A%09%09
@@ -700,21 +700,28 @@
 e%22: 
-%22Deathrattle%22
+GameTag.DEATH_RATTLE
 ,%0A%09%7D
@@ -1707,28 +1707,26 @@
 etTag(self, 
-name
+id
 ):%0A%09%09element
@@ -1757,27 +1757,27 @@
 ag%5B@
-name=%22%25s
+enumID=%22%25i
 %22%5D' %25 (
-name
+id
 ))%0A%09

Use GameTag id values in XMLCard instead of names