#
# modified from Hoop library - Game Programming With Python, Sean Riley
#
# datamanager.py
#

import copy, simobject

class Category:
    """ base class for all Category classes that describe
    simulation objects in the game.
    """
    def __init__(self, name):
        self.name = name

class SimCategory(Category):
    """I am a category class for simulation objects. Each sim category
    has data that allows it to create a different type of actual
    simObject."""
    def __init__(self, name, sim_cat):
        Category.__init__(self, name)
        # would like to remoe eval, but want to retain elegance of code...
        self.sim_cat = eval("simobject."+ sim_cat)


    def create(self, *args):
        """Create a SimObject using my data as a template.
        """
        newSim = apply(self.sim_cat, args)
        return newSim


class DataManager:
    """Repository of category objects that define the types
    of simulation objects in the game.
    """
    def __init__(self):
        self.categories = {}    # lists of category objects
        self.initCategory("sims", initialSimCategories, SimCategory)      # categoryName, rawdata, categoryClass

    def initCategory(self, categoryName, rawData, categoryClass):
        newCatList = []
        self.categories[categoryName] = newCatList
        for data in rawData:
            cdata = copy.copy(data)
            newCatObj = apply( categoryClass, cdata)
            newCatList.append( newCatObj )

    def findCategory(self, categoryName, name):
        category = self.categories.get(categoryName, None)
        if not category:
            print "cannot find category", categoryName
            return None
        for cat in category:
            if cat.name == name:
                return cat

    def createInstance(self, categoryName, name, *args):
        category = self.findCategory(categoryName, name)
        if category:
            return apply(category.create, args)

initialSimCategories = [
     # name, sim_cat
     ("survivor", "Survivor") ,
     ("zombie_massive", "Zombie"),
     ("zombie_massive_corpse", "Pickup"),
     ("zombie_normal", "Zombie"),
     ("zombie_normal_corpse", "Pickup"),
     ("zombie_fast", "Zombie"),
     ("zombie_fast_corpse", "Pickup"),
     ("Pistol", "Pistol"),
     ("SMG", "Weapon"),
     ("AssaultR", "Weapon"),
     ("SniperR", "Weapon"),
     ("LaserR", "LaserR"),
     ("GaussR", "Weapon"),
     ("MiniGun", "Weapon"),
     ("Shotgun", "Weapon"),
     ("Shotgun_bullet", "Bullet"),
     ("Pistol_bullet", "Bullet"),
     ("SMG_bullet", "Bullet"),
     ("AssaultR_bullet", "Bullet"),
     ("SniperR_bullet", "Bullet"),
     ("LaserR_bullet", "LaserBullet"),
     ("GaussR_bullet", "Bullet"),
     ("MiniGun_bullet", "Bullet"),
     ("Pistol_muzzle_flash", "Effect"),
     ("SMG_muzzle_flash", "Effect"),
     ("AssaultR_muzzle_flash", "Effect"),
     ("SniperR_muzzle_flash", "Effect"),
     ("LaserR_muzzle_flash", "Effect"),
     ("GaussR_muzzle_flash", "Effect"),
     ("MiniGun_muzzle_flash", "Effect"),
     ("ricochet", "Effect"),
     ("slash", "Effect"),
     ("fire", "Effect"),
     ("remains", "Pickup"),
     ("petrol", "Pickup"),
     ("ammo", "Ammo"),
     ("medkit", "Medkit"),
     ("grenade_concussion", "GrenadeConcussion"),
     ("grenade_molotov", "GrenadeMolotov"),
     ("grenade_incendiary", "GrenadeIncendiary"),
     ("blood", "Effect"),
     ("concrete_block", "Block"),
     ("shrub", "Block"),
     ("zombie_hive", "ZombieHive")
     ]
