import os
from pkg_resources import resource_filename
from hearthstone import cardxml
from .blackrock import *
from .game import *
from .classic import *
from .debug import *
from .gvg import *
from .naxxramas import *
from .tgt import *
from .tutorial import *


def merge(id):
	"""
	Find the xmlcard and the card definition of \a id
	Then return a merged class of the two
	"""
	card = db[id]
	carddef = globals().get(id)
	if not carddef:
		cls = type(id, (), {})
	else:
		if hasattr(carddef, "scripts"):
			# This basically means the card has already been merged...
			return card
		cls = type(id, (carddef, ), {})
	card.scripts = cls
	return card


def filter(**kwargs):
	"""
	Returns a list of card IDs matching the given filters. Each filter, if not
	None, is matched against the registered card database.
	cards.
	Examples arguments:
	\a collectible: Whether the card is collectible or not.
	\a type: The type of the card (hearthstone.enums.CardType)
	\a race: The race (tribe) of the card (hearthstone.enums.Race)
	\a rarity: The rarity of the card (hearthstone.enums.Rarity)
	\a cost: The mana cost of the card
	"""
	cards = db.values()

	if "type" not in kwargs:
		kwargs["type"] = [CardType.SPELL, CardType.WEAPON, CardType.MINION]

	for attr, value in kwargs.items():
		if value is not None:
			# What? this doesn't work?
			# cards = __builtins__["filter"](lambda c: getattr(c, attr) == value, cards)
			cards = [card for card in cards if (isinstance(value, list) and getattr(card, attr) in value) or getattr(card, attr) == value]

	return [card.id for card in cards]


# Here we import every card from every set and load the cardxml database.
# For every card, we will "merge" the class with its Python definition if
# it exists.
# This code is only ran once, at initial import.

if "db" not in globals():
	xmlfile = resource_filename(__name__, "data/CardDefs.xml")
	if not os.path.exists(xmlfile):
		raise RuntimeError("%r does not exist - generate it!" % (xmlfile))

	with open(xmlfile, "r") as f:
		db, xml = cardxml.load(xmlfile)
		for id in db:
			globals()[id] = merge(id)

@@ -88,46 +88,56 @@
 rom 
-.blackrock import *
+hearthstone.enums import CardType
 %0Afrom .
-game
  import 
 *%0Afr
@@ -136,140 +136,70 @@
 ort 
-*%0Afrom .classic import *%0Afrom .debug import *%0Afrom .gvg import *%0Afrom .naxxramas import *%0Afrom .tgt import *%0Afrom .tutorial import *
+blackrock, classic, debug, game, gvg, naxxramas, tgt, tutorial
 %0A%0A%0Ad
@@ -342,239 +342,250 @@
 f = 
-globals().get(id)%0A%09if not carddef:%0A%09%09cls = type(id, (), %7B%7D)%0A%09else:%0A%09%09if hasattr(carddef, %22scripts%22):%0A%09%09%09# This basically means the card has already been merged...%0A%09%09%09return card%0A%09%09cls = type(id, (carddef, ), %7B%7D)%0A%09card.scripts = cls
+None%0A%09for cardset in (blackrock, classic, debug, game, gvg, naxxramas, tgt, tutorial):%0A%09%09if hasattr(cardset, id):%0A%09%09%09carddef = getattr(cardset, id)%0A%09%09%09card.scripts = type(id, (carddef, ), %7B%7D)%0A%09%09%09break%0A%09else:%0A%09%09card.scripts = type(id, (), %7B%7D)%0A
 %0A%09re

Clean up and improve runtime card merging algorithm