
import os,string

from common import *
import FS_file_system
import thing

PATH_THINGS=os.path.join('data','things')


class BluePrint:
  def __init__(s,game,name,dir):
    s.game=game
    s.name=name
    s.dir=dir
    s.details=[]
    s.images=[]
    s.prop={ 'blocklos':0 }
  def __repr__(s):
    return '<bprint:%s>'%(s.name)
  def detail(s,k,v):
    s.details.append( (k,v) )
#    print s.name,k,v
    if k=='image':
      s.images.append( s.game.image_cache(s.dir,v) )
  def check(s,val):
    return s.prop[val]
  def examine(s):
    for k,v in s.details:
      if k=='los_cost' and v=='block': s.prop['blocklos']=1
    if not s.images:
      raise 'BluePrint.examine: no images',(s.name)
    s.image_size=s.images[0].get_size()
    for rest in s.images[1:]:
      if s.image_size!=rest.get_size():
        raise 'Blueprint.examine: image sizes are different',(s.name,s.details)


class ThingFactory(FS_file_system.FS_file_system):
  def __init__(s,game,path=None):
    s.game=game
    s.blueprints={}
    if path is None: path=PATH_THINGS
    s.FS_walk_and_load_config(s.load_config,path)

  def blueprints_get(s,names):
    rv=[]
    for i in names: rv.append( s.blueprints[i] )
    return rv

  def create_tile(s,pos,blueprint_names):
    # pos=positon
    # tile_names= 'N' blueprints per single tile
    return thing.Thing_is_tile( s.game, pos, s.blueprints_get(blueprint_names) )
  def create_objects(s,pos,tile,blueprint_names):
    # pos=position
    # object_name= one blueprint per object (on create)
    return s.create_object( pos, tile, s.blueprints_get(blueprint_names) )

  def create_object(s,pos,tile,blueprints):
#    print 'NEW obj:',pos,name
    if len(blueprints)!=1:
      raise 'factory.create_object: expected only 1 blueprint',(pos,blueprints)
    name=blueprints[0].name
    obj_class=thing.__dict__['OBJECT_'+name]
    obj=obj_class(s.game,list(pos),tile,[ s.blueprints[name] ])
    tile.child_add( obj )
    return obj

  def has_type(s,type):
    return s.blueprints.has_key(type)

  def load_config(s,dir,conf):
    if DEBUG: print 'loading',dir,conf
    mode=None
    allimages=[]
    for l in open(conf).readlines():
      sl=string.strip(l)
      if sl:
        if sl[0]=='#':
          pass
        elif mode is None:
          if string.lower(sl) not in [ 'things','things']:
            print "ThingFactory: load_conf: Not thing conf",conf
          mode=0
        elif mode==0:
          if sl[0]=='[' and sl[-1]==']':
            mode=1
            allimages=[]
            blue=BluePrint( s.game,sl[1:-1],dir )
#            vals=[ ('name',sl[1:-1]), ('dir',dir), ]
        elif mode==1:
          p=string.find(sl,'=')
          if p==-1:
            raise 'ThingFactory: load_conf: line not of form key=val, missing =',(conf,sl)
          blue.detail( string.strip(sl[:p]), string.strip(sl[p+1:]) )
        else:
          raise 'ThingFactory: load_conf: unknown mode!',(conf,mode)
      else:
        if mode==1:
          s.blueprint_add(blue)
          blue=None
        mode=0
    if blue is not None:
      s.blueprint_add(blue)
  def blueprint_add(s,blue):
    blue.examine()
    s.blueprints[ blue.name ]=blue


