package Specific
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;
	import flash.events.Event;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	
	import com.indindi.StateMachine.*;
	import com.indindi.Text.*;
	
	public class OptionState extends IState
	{

		// Specifique
		private var OptionFond:Bitmap; 
		private var MyText:DText;
		var volBD:BitmapData;
		var levelBD:BitmapData;
		var tutoBD:BitmapData;
		var volBitmap:Bitmap = new Bitmap();
		var levelBitmap:Bitmap = new Bitmap();
		var tutoBitmap:Bitmap = new Bitmap();
		var _vol:Number;
		var _level:Number;
		
		/* 
		Pour pouvoir utiliser facilement les évènements sur l'état
		on garde un pointeur sur le movieClip (_root/Stage) qui est
		récupéré à la création
		*/
		private var PMC:MovieClip = new MovieClip();
		private var _mc:MovieClip = new MovieClip();
		
		/*
		Par commodité, on garde un pointeur sur la machine à état dont
		fait partie l'étt actuel qu'on récupère à la création.
		*/
		private var MySM:StateMachine = new StateMachine();
		
		public function OptionState(SM:StateMachine,ParentMC:MovieClip,stateName:String):void
		{
			super(stateName);
			PMC = ParentMC;			
			MySM = SM;
			MyText = PMC.dText;
		}
		
		
		/*
		Ces trois fonctions sont gérées au niveau de la StateMachine
		et doivent figurer dans la classe dérivée.
		*/
		override public function Enter():void
		{	
			trace("Entree options...");
			
			//PMC.setScore(0);
			
			_vol = PMC.getGlobalVol();
			_level = PMC.getLevel();
			var optionBD:BitmapData = new GfxOption(400, 400);
			OptionFond = new Bitmap(optionBD);
			
			OptionFond.x = 0;
			OptionFond.y = 0;
			drawInterface(OptionFond);
			
//			PMC.stage.addEventListener(MouseEvent.CLICK,GoGame);
//			PMC.stage.addEventListener(KeyboardEvent.KEY_DOWN,GoGame);
		}
		
		private function drawInterface(fond:Bitmap) {
			_mc.addChild(fond);
			
			var arrowUpVolMC:MovieClip = new ArrowUp();
			var arrowDownVolMC:MovieClip = new ArrowDown();
			var arrowUpLevelMC:MovieClip = new ArrowUp();
			var arrowDownLevelMC:MovieClip = new ArrowDown();
			var GoGameButtonMC:MovieClip = new GoGameButton();
			
			arrowUpVolMC.x = arrowDownVolMC.x = 80;
			arrowUpVolMC.y = 100;
			arrowDownVolMC.y = 125;
			arrowUpVolMC.addEventListener(MouseEvent.CLICK, arrowUpVolHandler);
			arrowDownVolMC.addEventListener(MouseEvent.CLICK, arrowDownVolHandler);

			
			
			arrowUpLevelMC.x = arrowDownLevelMC.x = 250;
			arrowUpLevelMC.y = 100;
			arrowDownLevelMC.y = 125;
			arrowUpLevelMC.addEventListener(MouseEvent.CLICK, arrowUpLevelHandler);
			arrowDownLevelMC.addEventListener(MouseEvent.CLICK, arrowDownLevelHandler);
			
			drawVol();
			drawLevel();
			
			volBitmap.y = levelBitmap.y = 110;
			volBitmap.x = 105;
			levelBitmap.x = 275;
			
			tutoBitmap.x = 35;
			tutoBitmap.y = 220;
			drawTuto();
			
			GoGameButtonMC.x = 125;
			GoGameButtonMC.y = 380;
			GoGameButtonMC.addEventListener(MouseEvent.CLICK, GoGameButtonHandler);
			
			
			
			_mc.addChild(arrowUpVolMC);
			_mc.addChild(arrowDownVolMC);
			_mc.addChild(arrowUpLevelMC);
			_mc.addChild(arrowDownLevelMC);
			_mc.addChild(volBitmap);
			_mc.addChild(levelBitmap);
			_mc.addChild(tutoBitmap);
			_mc.addChild(GoGameButtonMC);
			
			
			
			PMC.addChild(_mc);
		}
		
		private function drawVol() {
			var str:String = "Volume : "+ _vol.toFixed(1);
			volBD = new BitmapData(str.length*9+5, 25, true, 0x55000000);
			MyText.draw(volBD, str, 5, 5);
			volBitmap.bitmapData = volBD;
		}
		
		private function drawLevel() {
			var s:String = "";
			if (_level < 10) s="0";
			var str:String = "Level : " + s + _level;
			levelBD = new BitmapData(str.length*9+5, 25, true, 0x55000000);
			MyText.draw(levelBD, str, 5, 5);
			levelBitmap.bitmapData = levelBD;
		}
		
		private function drawTuto() {
			var str:String = "Move the Ship with the ARROW KEYS of your\nkeyboard.\nFire missiles with the SPACE BAR.\nAccess the next level by shooting all the\nasteroids.\nBlue STICKY BALLS cannot be destroyed...\n\nEnjoy Yourself...";
			tutoBD = new BitmapData(340, 130, true, 0x55000000);
			MyText.draw(tutoBD, str, 5, 5);
			tutoBitmap.bitmapData = tutoBD;
		}
		
		private function arrowUpVolHandler(e:MouseEvent) {
			if (_vol <= 0.9) _vol += 0.1;
			drawVol();
		}
		private function arrowDownVolHandler(e:MouseEvent) {
			if (_vol >= 0.1) _vol -= 0.1;
			drawVol();
		}
		private function arrowUpLevelHandler(e:MouseEvent) {
			if (_level <= 49) _level += 1;
			drawLevel();
		}
		private function arrowDownLevelHandler(e:MouseEvent) {
			if (_level >= 2) _level -= 1;
			drawLevel();
		}
		
		private function GoGameButtonHandler(e:MouseEvent) {
			PMC.setLevel(_level);
			PMC.setGlobalVol(_vol);
			MySM.SetNextState(new GameState(MySM, PMC, "GameState"));
			MySM.GotoNextState();
		}
		
		
		
		override public function Exit():void
		{
			PMC.removeChild(_mc);			

//			PMC.stage.removeEventListener(MouseEvent.CLICK,GoGame);
//			PMC.stage.removeEventListener(KeyboardEvent.KEY_DOWN,GoGame);
		}
		
		override public function Update(time:int):void
		{
			// Boucle du jeux ICI
			trace("option");
		}
		
	}
}