package ui
{
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import game.Panda;
	import flash.display.DisplayObjectContainer;
	import flash.text.TextFieldType;
	import flash.events.MouseEvent;
	import tdh.utils.BitmapInt;
	
	public class GameOver extends Sprite
	{
		
		public function GameOver(start:Function, store:Function, welcome:Function)
		{
			super();
			this.dpo = new Bitmap();
			this.btnBack.addChild(new BtnBambooBmp());
			this.btnBack.alpha = 0;
			this.btnUpload.addChild(new BtnBambooBmp());
			this.btnUpload.alpha = 0;
			this.btnReplay.addChild(new BtnBambooBmp());
			this.btnReplay.alpha = 0;
			this.btnEnterStore.addChild(new BtnBambooBmp());
			this.btnEnterStore.alpha = 0;
			this.txtUserName.width = 200;
			this.txtUserName.setTextFormat(this.txtFormat);
			this.startFunc = start;
			this.storeFunc = store;
			this.welcomFunc = welcome;
		}
		
		private static var OverBmp:Class = GameOver_OverBmp;
		
		private static var BtnBambooBmp:Class = GameUI_BtnArea2;
		
		private var dpo:Bitmap;
		
		private const btnBack:Sprite = new Sprite();
		
		private const btnUpload:Sprite = new Sprite();
		
		private const btnReplay:Sprite = new Sprite();
		
		private const btnEnterStore:Sprite = new Sprite();
		
		private const txtUserName:TextField = new TextField();
		
		private const txtFormat:TextFormat = new TextFormat(null, 15, 0, true);
		
		public var panda:Panda;
		
		public var data:Data;
		
		private var startFunc:Function;
		
		private var storeFunc:Function;
		
		private var welcomFunc:Function;
		
		public function showAs(dpoc:DisplayObjectContainer):void
		{
			this.txtUserName.text = this.data.prevUserName;
			this.dpo.visible = false;
			dpoc.addChild(this.dpo);
			this.scoreBmp.y = 160;
			this.scoreBmp.showAs(dpoc);
			this.bestScoreBmp.y = 260;
			this.bestScoreBmp.value = this.data.bestScore;
			this.bestScoreBmp.visible = false;
			this.bestScoreBmp.showAs(dpoc);
			this.bambooBmp.y = 310;
			this.bambooBmp.showAs(dpoc);
			dpoc.addChild(this.txtUserName);
			this.txtUserName.type = TextFieldType.INPUT;
			this.txtUserName.x = 235;
			this.txtUserName.y = 405;
			this.txtUserName.visible = false;
			this.btnBack.visible = false;
			this.btnBack.x = 23;
			this.btnBack.y = 392;
			dpoc.addChild(this.btnBack);
			this.btnUpload.visible = false;
			this.btnUpload.x = 174;
			this.btnUpload.y = 367;
			dpoc.addChild(this.btnUpload);
			this.btnReplay.visible = false;
			this.btnReplay.x = 502;
			this.btnReplay.y = 367;
			dpoc.addChild(this.btnReplay);
			this.btnEnterStore.visible = false;
			this.btnEnterStore.x = 593;
			this.btnEnterStore.y = 367;
			dpoc.addChild(this.btnEnterStore);
			this.btnBack.addEventListener(MouseEvent.MOUSE_DOWN, this.mosueDown);
			this.btnBack.addEventListener(MouseEvent.MOUSE_UP, this.goWelcome);
			this.btnUpload.addEventListener(MouseEvent.MOUSE_DOWN, this.mosueDown);
			this.btnUpload.addEventListener(MouseEvent.MOUSE_UP, this.btnUploadHide);
			this.btnUpload.addEventListener(MouseEvent.CLICK, this.uploadUserScore);
			this.btnReplay.addEventListener(MouseEvent.MOUSE_DOWN, this.mosueDown);
			this.btnReplay.addEventListener(MouseEvent.MOUSE_UP, this.replayGame);
			this.btnEnterStore.addEventListener(MouseEvent.MOUSE_DOWN, this.mosueDown);
			this.btnEnterStore.addEventListener(MouseEvent.MOUSE_UP, this.goStore);
		}
		
		private function btnUploadHide(event:MouseEvent):void
		{
			this.btnUpload.alpha = 0;
		}
		
		private function mosueDown(e:MouseEvent):void
		{
			e.currentTarget.alpha = 1;
		}
		
		private function goWelcome(event:MouseEvent):void
		{
			this.btnBack.alpha = 0;
			this.hide();
			this.welcomFunc();
			this.data.prevUserName = this.txtUserName.text;
			this.data.save();
		}
		
		public function replayGame(e:MouseEvent):void
		{
			this.btnReplay.alpha = 0;
			this.hide();
			this.startFunc();
			this.data.prevUserName = this.txtUserName.text;
			this.data.save();
		}
		
		private function goStore(e:MouseEvent):void
		{
			this.btnEnterStore.alpha = 0;
			this.hide();
			this.storeFunc();
			this.data.prevUserName = this.txtUserName.text;
			this.data.save();
		}
		
		private function hide():void
		{
			this.dpo.visible = this.btnReplay.visible = false;
			this.btnEnterStore.visible = this.btnBack.visible = false;
			this.scoreBmp.visible = this.bestScoreBmp.visible = false;
			this.bambooBmp.visible = this.txtUserName.visible = false;
			this.dpo.bitmapData.dispose();
			this.dpo.bitmapData = null;
		}
		
		public function show():void
		{
			this.dpo.bitmapData = new OverBmp().bitmapData;
			this.txtUserName.setTextFormat(this.txtFormat);
			this.btnUpload.alpha = this.btnReplay.alpha = this.btnEnterStore.alpha = 0;
			this.btnUpload.visible = this.btnReplay.visible = this.btnEnterStore.visible = this.btnBack.visible = this.dpo.visible = true;
			this.scoreBmp.visible = this.bestScoreBmp.visible = this.bambooBmp.visible = this.txtUserName.visible = true;
			this.scoreBmp.value = this.panda.runningDistance;
			this.scoreBmp.x = 600 - this.scoreBmp.width;
			if (this.scoreBmp.value > this.data.bestScore)
			{
				this.bestScoreBmp.value = this.data.bestScore = this.scoreBmp.value;
			}
			else
			{
				this.bestScoreBmp.value = this.data.bestScore;
			}
			this.bestScoreBmp.x = 600 - this.bestScoreBmp.width;
			this.bambooBmp.value = this.panda.numBamboo;
			this.bambooBmp.x = 600 - this.bambooBmp.width;
			this.data.bamboo = this.panda.numBamboo;
			this.data.save();
		}
		
		private function uploadUserScore(e:MouseEvent):void
		{
			var obj:Object = null;
			this.btnUpload.alpha = 0;
			this.btnUpload.visible = false;
			var i:int = 0;
			while (i < this.data.scoreRecordList.length)
			{
				if (this.scoreBmp.value >= Number(this.data.scoreRecordList[i].score))
				{
					if (this.scoreBmp.value >= Number(this.data.scoreRecordList[i].score))
					{
						obj = {"name": "", "score": String(this.scoreBmp.value)};
						if ((this.txtUserName.text == "Player Name") || (this.txtUserName.text == ""))
						{
							obj.name = "Player";
						}
						else
						{
							obj.name = this.txtUserName.text;
						}
						this.data.scoreRecordList.splice(i, 0, obj);
						this.data.scoreRecordList.pop();
						break;
					}
				}
				i++;
			}
			this.data.prevUserName = this.txtUserName.text;
			this.data.save();
		}
		
		private const scoreBmp:BitmapInt = new BitmapInt(GameUI.numSrc2);
		
		private const bambooBmp:BitmapInt = new BitmapInt(GameUI.numSrc3);
		
		private const bestScoreBmp:BitmapInt = new BitmapInt(GameUI.numSrc3);
	}
}
