package Game.Scenarios
{
	import Engine.GameSprite;
	import Engine.SpriteGroup;
	import flash.events.Event;
	import flash.geom.Point;
	import Game.LibGraphics;
	import Game.Objects.Bonuses.GunBonus;
	import Game.Objects.Boss;
	import Game.Objects.FlyerEnemy;
	import Game.Objects.RocketEnemy;
	import Game.Objects.UFOEnemy;
	import Game.SceneGame;
	/**
	 * ...
	 * @author KEFIR
	 */
	public class Mission1Scenario extends Scenario
	{
		private var _state:uint = 0;
		
		private var _text_controlls:GameSprite;
		private var _text_gun_bonus:GameSprite;
		
		private var _enemyGroup:Array = new Array();
		
		private var _timeline_mem:uint;
		
		private function GenerateUFOs(cnt:int, group:SpriteGroup):void {
			var inity:int = ((group as SceneGame).Player.y);
			if (inity < 200) {
				inity = 200;
			}
			
			for (var i:int = 0; i < cnt; i++) {
				var _Enemy:UFOEnemy = new UFOEnemy();
					_Enemy.x = 700;
					_Enemy.y = (inity - (cnt * 100) / 2) + 100 * i;
					_Enemy.init_y = _Enemy.y;
					group.AddSprite(_Enemy);
					
					_enemyGroup[_enemyGroup.length] = _Enemy;
			}
		}
		
		private function GenerateFlyers(cnt:int, group:SpriteGroup):void {
			var inity:int = ((group as SceneGame).Player.y);
			if (inity < 200) {
				inity = 200;
			}
			
			for (var i:int = 0; i < cnt; i++) {
				var _Enemy:FlyerEnemy = new FlyerEnemy();
					_Enemy.x = 700;
					_Enemy.y = (inity - (cnt * 100) / 2) + (100 * i);
					_Enemy.initY = _Enemy.y;
					group.AddSprite(_Enemy);
					
					_enemyGroup[_enemyGroup.length] = _Enemy;
			}
		}
		
		private function GenerateRockets(cnt:int, group:SpriteGroup):void {
			
			for (var i:int = 0; i < cnt; i++) {
				var _Enemy:RocketEnemy = new RocketEnemy();
					_Enemy.x = 700;
					_Enemy.y = (group as SceneGame).Player.y + i * 100;
					group.AddSprite(_Enemy);
					
					_enemyGroup[_enemyGroup.length] = _Enemy;
			}
		}
		
		private function GenerateGunBonus(group:SpriteGroup):void {
			
			var _bonus:GunBonus = new GunBonus();
			_bonus.x = 800;
			group.AddSprite(_bonus);
			_enemyGroup[0] = _bonus;
		}
		
		/**
		 * Если хоть один жив, возвращает true
		 * @return
		 */
		private function checkGroup():Boolean {
			var truecnt:uint = 0;
			
			for (var i:int = 0; i < _enemyGroup.length; i++) {
				if (GameSprite(_enemyGroup[i]).Delete) {
					truecnt++;
				}
			}
			
			return !(truecnt == _enemyGroup.length);
			
		}
		
		override public function Run(obj:Object, timeline:uint):void
		{
			if (_state == 0) {
				if (timeline > 1500) {
					
					(obj as SceneGame).Group.AddSprite(_text_controlls);
					_state = 1;
				}
			}
			
			if (_state == 1) {
				if (timeline > 11500) {
					_text_controlls.Delete = true;
					_state = 2;
				}
			}
			
			if (_state == 2) {
				if (timeline > 13000) {
					_enemyGroup = new Array();
					GenerateUFOs(1, SpriteGroup(obj));
					_state = 3;
				}
			}
			
			if (_state == 3) {
				if (!checkGroup()) {
					_timeline_mem = timeline;
					_state = 4;
				}
			}
			
			if (_state == 4) {
				if (timeline - _timeline_mem > 2000) {
					_enemyGroup = new Array();
					GenerateUFOs(1, SpriteGroup(obj));
					_state = 5;
				}
			}
			
			if (_state == 5) {
				if (!checkGroup()) {
					_timeline_mem = timeline;
					_state = 6;
				}
			}
			
			if (_state == 6) {
				if (timeline - _timeline_mem > 2000) {
					_enemyGroup = new Array();
					GenerateUFOs(2, SpriteGroup(obj));
					_state = 7;
				}
			}
			
			if (_state == 7) {
				if (!checkGroup()) {
					_timeline_mem = timeline;
					_state = 8;
				}
			}
			
			if (_state == 8) {
				if (timeline - _timeline_mem > 2000) {
					_enemyGroup = new Array();
					GenerateUFOs(3, SpriteGroup(obj));
					_state = 9;
				}
			}
			
			if (_state == 9) {
				if (!checkGroup()) {
					_timeline_mem = timeline;
					_state = 10;
				}
			}
			
			if (_state == 10) {
				if (timeline - _timeline_mem > 3500) {
					_enemyGroup = new Array();
					GenerateFlyers(1, SpriteGroup(obj));
					_state = 11;
				}
			}
			
			if (_state == 11) {
				if (!checkGroup()) {
					_enemyGroup = new Array();
					GenerateFlyers(2, SpriteGroup(obj));
					GenerateUFOs(2, SpriteGroup(obj));
					_state = 12;
				}
			}
			
			if (_state == 12) {
				if (!checkGroup()) {
					_timeline_mem = timeline;
					_state = 13;
				}
			}
			
			if (_state == 13) {
				if (timeline - _timeline_mem > 3000) {
					(obj as SpriteGroup).Group.AddSprite(_text_gun_bonus);
					_timeline_mem = timeline;
					_state = 14;
				}
			}
			
			if (_state == 14) {
				if (timeline - _timeline_mem > 5000) {
					_text_gun_bonus.Delete = true;
					_enemyGroup = new Array();
					GenerateGunBonus(SpriteGroup(obj));
					_state = 15;
				}
			}
			
			if (_state == 15) {
				if (!checkGroup()) {
					_timeline_mem = timeline;
					_state = 16;
				}
			}
			
			if (_state == 16) {
				if (timeline - _timeline_mem > 3000) {
					_enemyGroup = new Array();
					GenerateUFOs(5, SpriteGroup(obj));
					_state = 17;
				}
			}
			
			if (_state == 17) {
				if (!checkGroup()) {
					_enemyGroup = new Array();
					GenerateFlyers(4, SpriteGroup(obj));
					_state = 18;
				}
			}
			
			if (_state == 18) {
				if (!checkGroup()) {
					_enemyGroup = new Array();
					GenerateFlyers(4, SpriteGroup(obj));
					_state = 19;
				}
			}
			
			if (_state == 19) {
				if (!checkGroup()) {
					_enemyGroup = new Array();
					GenerateFlyers(4, SpriteGroup(obj));
					_state = 20;
				}
			}
			
			if (_state == 20) {
				if (!checkGroup()) {
					_enemyGroup = new Array();
					GenerateGunBonus(SpriteGroup(obj));
					_state = 21;
				}
			}
			
			if (_state == 21) {
				if (!checkGroup()) {
					_enemyGroup = new Array();
					GenerateFlyers(4, SpriteGroup(obj));
					_state = 22;
				}
			}
			
			if (_state == 22) {
				if (!checkGroup()) {
					_timeline_mem = timeline;
					_state = 23;
				}
			}
			
			if (_state == 23) {
				if (timeline - _timeline_mem > 3000) {
					GenerateRockets(1, SpriteGroup(obj));
					_state = 24;
				}
			}
			
			if (_state == 24) {
				if (!checkGroup()) {
					GenerateRockets(1, SpriteGroup(obj));;
					_state = 25;
				}
			}
			
			if (_state == 25) {
				if (!checkGroup()) {
					_enemyGroup = new Array();
					GenerateGunBonus(SpriteGroup(obj));
					_state = 26;
				}
			}
			
			if (_state == 26) {
				if (!checkGroup()) {
					GenerateRockets(1, SpriteGroup(obj));;
					_state = 27;
				}
			}
			
			if (_state == 27) {
				if (!checkGroup()) {
					_timeline_mem = timeline;
					_state = 28;
				}
			}
			
			if (_state == 28) {
				if (timeline - _timeline_mem > 3000) {
					_state = 29;
				}
			}
			
			if (_state == 29) {
				_enemyGroup = new Array();
				GenerateGunBonus(SpriteGroup(obj));
				_state = 30;
			}
			
			if (_state == 30) {
				if (!checkGroup()) {
					_timeline_mem = timeline;
					_state = 31;
				}
			}
			
			if (_state == 31) {
				if (timeline - _timeline_mem > 3000) {
					(obj as SceneGame).chnMainTheme.stop();
					(obj as SceneGame).chnMainTheme = (obj as SceneGame).sndBossThee.play(0, 999);
					//(obj as SceneGame).chnMainTheme.addEventListener(Event.SOUND_COMPLETE, (obj as SceneGame).loopBossTheme);
					
					var gameBoss:Boss = new Boss();
					gameBoss.y = (obj as SceneGame).y;
					gameBoss.x = 800;
					(obj as SceneGame).AddSprite(gameBoss);
					
					_state = 32;
				}
			}
		}
		
		public function Mission1Scenario()
		{
			_text_controlls = new GameSprite(new LibGraphics.TextControlls(), new Point(0, 0));
			_text_controlls.x = 10;
			_text_controlls.y = 190;
			_text_controlls.zIndex = 5;
			
			_text_gun_bonus = new GameSprite(new LibGraphics.TextGunBonus(), new Point(0, 0));
			_text_gun_bonus.x = 142;
			_text_gun_bonus.y = 195;
			_text_gun_bonus.zIndex = 5;
		}
		
	}

}