package com.sarmatianHeroes.objects
{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.utils.*

	import com.sarmatianHeroes.*;

	public class Squad extends Sprite 
	{
		private var _map:Map = Map.getInstance();
		
		public var quantity:Number;
		public var vx:Number = 0;
		public var vy:Number = 0;
		public var _balls:Array;
		public var vis:Array;
		private var _isMove:Boolean = false;
		public var bradius:int;
		public var _offset:int = 8;
		public var collision:Sprite;
		public var toCenterOffset:int;//onehalf of wide squad
		public var speed:Number = 60;
		public var Damage:Number = 8;
		public var attackRadius:Number = 50;
		public var moveFinished:Boolean = true;
		public var walked:Number = 0;
		public var target:Object;
		
		var _squadHP:int;
		var _oneHP:int = 12;
		public var _visType:String;
		
		public function Squad(type:String, quantity:Number=12) 
		{
			this.quantity = quantity;
			_squadHP = quantity * _oneHP;
			_visType = type;
			init();
		}
		
		public function init():void 
		{
			_balls = [];
			vis = [];
			bradius = 6;
			collision = new Sprite;
			addChild(collision);
			this.setChildIndex(collision,0);
			// Creating a bunch of balls and adding them to the collision list
			for(var i:uint = 0; i < quantity; i++)
			{
				var ball:Ball = new Ball(bradius, 0x000000);
				ball.vx = ball.vy = 0;
				_balls.push(ball);
				ball.alpha= 0.2;
				ball.scaleX = 1.5;
				collision.addChild(ball);
				var _vis:MovieClip;
				switch (_visType)
				{
					case "HazarSword": _vis = new HazarSword; _vis.scaleX = _vis.scaleY = 0.5; break;
					case "HazarBow": _vis = new HazarBow; _vis.scaleX = _vis.scaleY = 0.5; break;
					case "HazarSpear": _vis = new HazarSpear; _vis.scaleX = _vis.scaleY = 0.6; break;
				}
				
				
				_vis.gotoAndStop(2);
				vis.push(_vis);
				addChild(_vis);
			}
			setQuad();
		}
		
		public function setQuad():void
		{
			var quad:int = Math.sqrt(quantity-1)+1;
			toCenterOffset = (quad*bradius*2 + (quad-1)*bradius*2)/2;
			for(var i:uint = 0; i < quantity; i++)
			{
				_balls[i].x = bradius + ((i) - Math.floor(i/quad)*quad)*(bradius*2 + _offset) - toCenterOffset;
				_balls[i].y = bradius + (Math.floor(i/quad))*(bradius*2 + _offset) - toCenterOffset;
				_balls[i].x0 = _balls[i].x;
				_balls[i].y0 = _balls[i].y;
				vis[i].x = _balls[i].x;
				vis[i].y = _balls[i].y;
			}
		}
		
		public function attack(_target:Object):void
		{
			
			this.target = _target;
			//trace("hit_HERO", this.target);
		}
		
		public function takeDamage(damage:int):int
		{
			
			if(quantity > 0)
			{
				var _boom:Boom = new Boom(x,y);
				stage.addChild(_boom);trace(_boom);
			}
			_squadHP -= damage;
			trace(_oneHP);
			var _killed:int = Math.floor(damage/_oneHP);
			if(_killed > quantity)
			{
				_killed = quantity;
			}
			
			for(var i:int = 0; i < _killed; i++)
			{
				var _who:int = Math.floor(Math.random()*quantity);
				var _b:Ball = _balls[_who];
				_balls.splice(_who,1);
				collision.removeChild(_b);
				var _v:MovieClip = vis[_who];
				vis.splice(_who,1);
				removeChild(_v);
				/*var _b:Ball = _balls.pop();
				collision.removeChild(_b);
				removeChild(vis.pop());*/
				quantity--;
				var _body:MovieClip = new DeadBody(_map._layerForward,_map._layerBackObjects,this);
			}
			//trace(_squadHP,quantity,_killed);

			if (quantity<=0)
			{
				quantity = 0;
				_map._squadAll.splice(_map._squadAll.indexOf(this),1);
				_map._layerMain.removeChild(this);
				trace("squad was killed...");
				if(_map._squadAll.length == 0)
				{
					_map.mapComplit();
				}
			}
			return _killed;
		}
	}
}