package Game.Mobs
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flinjin.events.FlinjinSpriteEvent;
	import flinjin.graphics.Sprite;
	import Game.Balance;
	import Game.GameMain;
	
	/**
	 * ...
	 * @author Michael Miriti
	 */
	public class Hippie extends Mob
	{
		[Embed(source="../../bmp/mobs/hippie/body.png")]
		private static var _bodyBitmap:Class;
		
		[Embed(source="../../bmp/mobs/hippie/head.png")]
		private static var _headBitmap:Class;
		
		[Embed(source="../../bmp/mobs/hippie/leg.png")]
		private static var _legBitmap:Class;
		
		[Embed(source="../../bmp/mobs/hippie/hand.png")]
		private static var _handBitmap:Class;
		
		private var _head:Sprite;
		private var _body:Sprite;
		private var _legs:Vector.<Sprite> = new Vector.<Sprite>;
		private var _hands:Vector.<Sprite> = new Vector.<Sprite>;
		
		private var _legRotation:Number = 0;
		private var _minX:Number;
		private var _handRotation:Number = 0;
		
		private var _centerPos:Point = new Point();
		
		/**
		 * Hippie mob
		 */
		public function Hippie()
		{
			super(150, 150);
			
			_head = new Sprite(new _headBitmap());
			_head.setCenterInBitmapCenter();
			
			_body = new Sprite(new _bodyBitmap());
			_body.setCenterInBitmapCenter();
			
			_legs[0] = new Sprite(new _legBitmap(), new Point(10, 0));
			_legs[1] = new Sprite(new _legBitmap(), new Point(10, 0));
			
			_hands[0] = new Sprite(new _handBitmap(), new Point(30, 5));
			_hands[1] = new Sprite(new _handBitmap(), new Point(30, 5));
			
			addSprite(_legs[0], 65, 95);
			addSprite(_legs[1], 75, 95);
			addSprite(_hands[0], 75, 60);
			addSprite(_body, 75, 75);
			addSprite(_hands[1], 75, 60);
			addSprite(_head, 70, 30);
			
			_healhPoints = Balance.hippieHealth;
			
			_legRotation = Math.random() * Math.PI;
			
			addEventListener(FlinjinSpriteEvent.ADDED_TO_LAYER, onAdded);
		}
		
		override public function get position():Point
		{
			return _centerPos;
		}
		
		private function onAdded(e:FlinjinSpriteEvent):void
		{
			/** @todo wtf?! hardcode! **/
			_minX = 200 - 100 * (y - 275) / 70;
			zIndex = y;
		}
		
		override public function setState(newState:int):void
		{
			if (newState == Mob.STATE_DIENG)
			{
				Delete();
				return;
			}
			
			if (newState == Mob.STATE_IDLE)
				_legRotation = 0;
			
			if (newState == Mob.STATE_ATTACK)
			{
				_legRotation = 0;
				_handRotation = 0;
			}
			
			super.setState(newState);
		}
		
		override public function Move():void
		{
			if (_healhPoints <= 0)
				setState(Mob.STATE_DIENG);
			
			if (_state == Mob.STATE_DIENG)
			{
				return;
			}
			// Legs, hands and head animation here
			_legs[0].x = 65 - Math.cos(_legRotation) * 5;
			_legs[0].y = 95 - Math.sin(_legRotation) * 5;
			
			_legs[1].x = 75 - Math.cos(_legRotation + Math.PI) * 5;
			_legs[1].y = 95 - Math.sin(_legRotation + Math.PI) * 5;
			
			_hands[0].angle = Math.sin(_handRotation);
			_hands[1].angle = Math.sin(_handRotation + Math.PI);
			
			_head.x = 70 + Math.sin(_handRotation) * 3;
			
			if (_state == Mob.STATE_MOVING)
			{
				_legRotation -= 0.2;
				_handRotation = _legRotation;
				
				x -= Balance.hippieSpeed;
				
				if (x + 75 <= _minX)
					setState(Mob.STATE_ATTACK);
			}
			
			if (_state == Mob.STATE_ATTACK)
			{
				_handRotation -= 0.5;
				if ((parent as GameMain).factory != null)
				{
					(parent as GameMain).factory.Hit(Balance.hippieAttackPower);
				}
				else
				{
					_minX = -200;
					setState(STATE_MOVING);
				}
			}
			super.Move();
			
			_centerPos.x = x + width / 2;
			_centerPos.y = y + height / 2;
		}
	
	}

}