package net.fdms.Resource 
{
	import com.greensock.events.LoaderEvent;
	import flash.events.EventDispatcher;
	import com.greensock.loading.SWFLoader;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.text.TextField;
	import com.greensock.TweenLite;
	import flash.events.Event;
	import net.fdms.fdms;
	import net.fdms.Function.Align.AlignSets;
	import com.greensock.TweenMax;
	/**
	 * 2011/5/27 11:42
	 * @author Dawnboy
	 */
	public class ProgressBar extends Sprite
	{
		private static var startFunc:Function;
		private static var progressFunc:Function;
		private static var finishFunc:Function;
		private static var _container:Object;
		private static var progressBar:*;
		private static var _config:Object;
		private static var tempStatus:int = 0;
		private static var _listener:Object = null;
		
		public function ProgressBar() 
		{
			throw new Error("无法实例化");
		}
		
		/**
		 * 初始化进度条
		 * @param	container	进度条存放容器
		 * @param	config		
		 */
		public static function Init(container:Object, config:Object = null,listener:Object = null):void {
			_container = container;
			_listener = listener;
			if (config) {
				_config = config;
				if (_config.src != undefined && _config.src != "") {
					new SWFLoader(_config.src.toString(), { onComplete:resLoaded, onError:resLoadErr } ).load();
				}else {
					if (_config.set != undefined && _config.set != "") {
						
						var foo:Array = _config.set.split(",");
						var temp:Object = new Object();
						temp.width = (foo[0])?foo[0]:200;
						temp.height = (foo[1])?foo[1]:30;
						temp.bgcolor = (foo[2])?foo[2]:"#000000";
						temp.fontcolor = (foo[3])?foo[3]:"#ffffff";
						temp.alpha = (foo[4])?foo[4]:0.8;
						createBar(temp.width, temp.height, temp.bgcolor, temp.fontcolor, temp.alpha);
					}else {
						createBar();
					}
				}
			}else {
				createBar();
			}
		}
		
		/*public static function get StartFunc():Function {
			return startFunc;
		}*/
		
		public static function get ProgressFunc():Function {
			return progressFunc;
		}
		
		/*public static function get FinishFunc():Function {
			return finishFunc;
		}*/
		
		public static function get Bar():* {
			return progressBar;
		}
		
		/**
		 * 外部进度条载入完成后处理
		 * @param	e
		 */
		private static function resLoaded(e:LoaderEvent):void {
			//清除原始进度条
			clearDefault();
			//引用到新进度条
			progressBar = e.target.content.getChildAt(0);
			_container.addChild(progressBar);
			//获取进度条方法
			setNotifyFunc(
				(_config.onStartFunc != undefined && _config.onStartFunc != "")? startFunc = progressBar[_config.onStartFunc]:null,
				(_config.onProgressFunc != undefined && _config.onProgressFunc != "")? progressFunc = progressBar[_config.onProgressFunc]:null,
				(_config.onFinishFunc != undefined && _config.onFinishFunc != "")? finishFunc = progressBar[_config.onFinishFunc]:null
			)
			AlignSets.addAlignRes(progressBar, _config);
			e.target.unload();
			onFinish();
			creatComplete();
		}
		
		
		public static function onStart():void {
			TweenMax.killTweensOf(progressBar, false, { alpha:true } );
			TweenMax.to(progressBar, 0.5, { alpha:1,overwrite:2 } );
			if (startFunc != null) startFunc();
			
		}
		
		private static function onProgress(e:Event):void {
			if(progressFunc != null) progressFunc(e);
		}
		
		public static function onFinish():void {
			if (finishFunc != null) finishFunc();
			TweenMax.killTweensOf(progressBar, false, { alpha:true } );
			TweenMax.to(progressBar,0.5, { alpha:0, overwrite:2 } );
	
		}
		
		/**
		 * 外部进度条载入失败,重置为原始默认进度条
		 * @param	e
		 */
		private static function resLoadErr(e:LoaderEvent):void {
			_config.src = null;
			//Init(_container, _config);
		}
		
		/**
		 * 注册进度条方法
		 * @param	onStartFun
		 * @param	onProgressFun
		 * @param	onFinishFun
		 */
		public static function setNotifyFunc(onStartFun:Function = undefined, onProgressFun:Function = undefined, onFinishFun:Function = undefined):void {
			startFunc = onStartFun;
			progressFunc = onProgressFun;
			finishFunc = onFinishFun;
		}
		
		/**
		 * 生成默认进度条样式
		 * @param	width
		 * @param	height
		 * @param	bgColor
		 * @param	fontColor
		 * @param	alpha
		 */
		private static function createBar(width:uint = 200, height:uint = 30, bgColor:String = "#000000", fontColor:String = "#ffffff", alpha:Number = 0.8):void {
			if(progressBar is DisplayObjectContainer && progressBar.numChildren){
				clearDefault();
			}
			progressBar = new Sprite();
			//添加进度条背景
			var pgbg:Shape = new Shape();
			pgbg.graphics.beginFill(transColor(bgColor), alpha);
			pgbg.graphics.drawRoundRect(0, 0, width, height, 5, 5);
			pgbg.graphics.endFill();
			progressBar.addChild(pgbg);
			//添加进度条文字栏
			var progressText:TextField = new TextField();
			progressText.name = "test";
			progressText.selectable = false;
			progressText.width = pgbg.width - 20;
			progressText.x = 10;
			progressText.height = height;
			progressText.htmlText = "<font size='12px' color='" + fontColor + "'> </font>";
			progressText.y = (height - progressText.textHeight) / 2-2;
			progressBar.addChild(progressText);
			//progressBar.alpha = 0;
			//定义并注册方法
			var onStartFun:Function = function():void {
				//TweenLite.to(progressBar, 0.5, { alpha:1 } );
				progressText.htmlText = "<font size='12px' color='" + fontColor + "'>" + "配置信息初始化,请稍后..." + "</font>";
			}
			var onProgressFun:Function = function(e:Event):void {
				progressText.htmlText = "<font size='12px' color='" + fontColor + "'>" + "资源载入中,请稍后...          " +Math.floor(e.target.bytesLoaded / e.target.bytesTotal * 100).toString()+ "%</font>";
			}
			
			var onFinishFun:Function = function():void {
				progressText.htmlText = "<font size='12px' color='" + fontColor + "'>" + "载入完成</font>";
			}
			setNotifyFunc(onStartFun, onProgressFun, onFinishFun);
			_container.addChild(progressBar);
			AlignSets.addAlignRes(progressBar, { alignSets:["Center", 0] } );
		}
		
		/**
		 * 清除默认进度条
		 */
		private static function clearDefault():void {
			AlignSets.delAlign(progressBar);
			var num:int = progressBar.numChildren;
			for (var a:int = 0 ; a < num; a++) {
				progressBar.removeChildAt(0);
			}
			startFunc = progressFunc = finishFunc = null;
		}
		
		/**
		 * 转换颜色格式
		 * @param	color
		 * @return
		 */
		private static function transColor(color:String):Number {
			if (!color.indexOf("#")) {
				var temp:String = color.replace("#", "0x");
				return Number(temp);
			}else {
				return Number(color);
			}
			
		}
		
		private static function creatComplete():void {
			if (_listener) _listener.dispatchEvent(new Event("COMPLETE"));
		}
		
	}

}