package ui
{
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.text.TextField;
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	
	public class GameRank extends Sprite
	{
		
		public function GameRank(welcomeFunc:Function)
		{
			super();
			this.dpo = new Bitmap();
			this.btnBack = new Sprite();
			this.btnBack.addChild(new Bitmap(GameUI.btnAreaSrc2));
			this.welcomeFunc = welcomeFunc;
			this.initRankList();
		}
		
		private static var RankBmp:Class = GameRank_RankBmp;
		
		public var data:Data;
		
		private var dpo:Bitmap;
		
		private const RANK_NUM:int = 5;
		
		private var rankName:TextField;
		
		private var rankScore:TextField;
		
		private var btnBack:Sprite;
		
		private var welcomeFunc:Function;
		
		public function show():void
		{
			this.dpo.bitmapData = new RankBmp().bitmapData;
			this.dpo.visible = this.btnBack.visible = this.rankName.visible = this.rankScore.visible = true;
			this.btnBack.alpha = 0;
			this.updateRank();
		}
		
		private function initRankList():void
		{
			this.rankName = new TextField();
			this.rankScore = new TextField();
		}
		
		public function showAs(dpoc:DisplayObjectContainer):void
		{
			this.dpo.visible = this.btnBack.visible = this.rankName.visible = this.rankScore.visible = false;
			dpoc.addChild(this.dpo);
			dpoc.addChild(this.rankName);
			dpoc.addChild(this.btnBack);
			this.btnBack.alpha = 0.3;
			this.btnBack.x = 30;
			this.btnBack.y = 393;
			this.btnBack.addEventListener(MouseEvent.MOUSE_DOWN, this.mosueDown);
			this.btnBack.addEventListener(MouseEvent.MOUSE_UP, this.returnWelcome);
			dpoc.addChild(this.rankScore);
			this.rankName.y = this.rankScore.y = 200;
			this.rankName.x = 180;
			this.rankScore.x = 417;
		}
		
		private function updateRank():void
		{
			this.rankName.text = "";
			this.rankScore.text = "";
			var tmpFormat:TextFormat = new TextFormat(null, 30, 16777215, true);
			var i:int = 0;
			while (i < this.RANK_NUM)
			{
				if(this.data.scoreRecordList) {
					this.rankName.appendText((this.data.scoreRecordList[i]) ? this.data.scoreRecordList[i].name + "\n" : "");
					this.rankScore.appendText((this.data.scoreRecordList[i]) ? this.data.scoreRecordList[i].score + "\n" : "0");
				}
				i++;
			}
			this.rankName.selectable = false;
			this.rankScore.selectable = false;
			this.rankName.autoSize = TextFieldAutoSize.LEFT;
			this.rankScore.autoSize = TextFieldAutoSize.LEFT;
			this.rankName.setTextFormat(tmpFormat);
			this.rankScore.setTextFormat(tmpFormat);
		}
		
		private function mosueDown(e:MouseEvent):void
		{
			e.target.alpha = 0.3;
			(e.currentTarget as Sprite).addEventListener(MouseEvent.MOUSE_UP, this.mouseUp);
		}
		
		private function mouseUp(e:MouseEvent):void
		{
			e.currentTarget.alpha = 0;
			(e.currentTarget as Sprite).removeEventListener(MouseEvent.MOUSE_UP, this.mouseUp);
		}
		
		private function returnWelcome(e:MouseEvent):void
		{
			this.dpo.visible = this.btnBack.visible = this.rankName.visible = this.rankScore.visible = false;
			this.dpo.bitmapData.dispose();
			this.dpo.bitmapData = null;
			this.welcomeFunc();
		}
	}
}
