import mwpy_gameobject
clock = mwpy_gameobject.clock

MWPYGameObject = mwpy_gameobject.MWPYGameObject
MWPYGameObjectType = mwpy_gameobject.MWPYGameObjectType
MWPYGameObjectState = mwpy_gameobject.MWPYGameObjectState


class MWPYWall(MWPYGameObject):
	def __init__(self, gameObjectType = MWPYGameObjectType['MWGOT_WALL']):
		MWPYGameObject.__init__(self, gameObjectType)
		self.current_time = clock()
		self.max_life_time = 10
		self.is_flash = False
		self.state = MWPYGameObjectState['AS_NONE']
		self.dust_play_time = 0

	def can_render(self):
		return self.is_flash
	def can_update(self):
		return self.is_enable

	def do_update(self, delta, currenttime, world_dict):
	      self.current_time += delta
	      if(self.state == MWPYGameObjectState['AS_NONE'] or self.state == MWPYGameObjectState['AS_PLAY']):
		      self.mass = 0
		      self.vel_x = 0
		      self.vel_y = 0
		      if(self.current_time - self.dust_play_time > 0.5):
			      self.state = MWPYGameObjectState['AS_NONE']
	      delta = self.current_time
	      if (delta > self.max_life_time):
		      self.disable()
		      self.reset()
	      elif (delta / self.max_life_time > 0.7):
		      delta = int(delta * 10)
		      if ( (delta % 2) == 0):
			      self.is_flash = True
		      else:
			      self.is_flash = False

	def enable(self):
		self.is_enable = True
		self.is_flash = True
		self.current_time = 0
		self.mass = 1
		self.state = MWPYGameObjectState['AS_DOWN']

	def reset(self):
		self.mass = 1
		self.state = MWPYGameObjectState['AS_DOWN']

	def update_sprites_state(self, state, offset_x, offset_y):
		pos_x = round( (self.x + offset_x) * mwpy_gameobject.ScrW) / mwpy_gameobject.ScrW
		pos_y = round( (self.y + offset_y) * mwpy_gameobject.ScrH) / mwpy_gameobject.ScrH
		width = round(self.w * 2 * mwpy_gameobject.ScrW) / mwpy_gameobject.ScrW
		height = round(self.h * 2 * mwpy_gameobject.ScrH) / mwpy_gameobject.ScrH
		dustsprite = None
		if(state in self.states_info):
			sprite_list = self.states_info[state]
			for si in sprite_list:
				if(si.name == "DustSprite"):
					dustsprite = si
				else:
					wallsprite = si
					si.posx = pos_x - 0.5 / mwpy_gameobject.ScrW
					si.posy = pos_y - 0.5 / mwpy_gameobject.ScrH
					si.width = width
					si.height = height
			if(None != dustsprite):
				dustsprite.posx = wallsprite.posx - wallsprite.width * 0.5
				dustsprite.posy = wallsprite.posy + wallsprite.height - wallsprite.height * 0.2
				dustsprite.width = wallsprite.width * 2.0
				dustsprite.height = wallsprite.height * 0.2
			return sprite_list

		return []
