package net.fdms.Function 
{
	import flash.display.DisplayObject;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	/**
	 * 结婚日期正计时、倒计时
	 * @author Dawnboy
	 */
	public class MarryTimer 
	{
		private static var _marryDate:Date = new Date();	//结婚日期
		private static var _textSets:Array = [];			//输出设置
		private static var countTime:Object = { };			//计时对象
		private static var TimerBox:Timer = new Timer(1000);//计时器
		
		public function MarryTimer() 
		{
			
		}
		
		/**
		 * 计时器初始化
		 * @param	marryDate
		 */
		public static function init(oldTime:String):void {
			//trace("接收结婚日期:" + oldTime + "\n---------------------------------");
			var tmpDate:Date = new Date();
			var tmp1:Array;
			var tmp2:Array;
			var tmp3:Array;
			tmp1 = (oldTime.indexOf(" ")) ? oldTime.split(" ") : tmp1[0] = oldTime;
			tmp2 = String(tmp1[0]).split("-");
			
			_marryDate.setFullYear(Number(tmp2[0]), Number(tmp2[1])-1, Number(tmp2[2]));
			if (tmp1[1]) {
				tmp3 = String(tmp1[1]).split(":");
				_marryDate.setHours(Number(tmp3[0]), Number(tmp3[1]), Number(tmp3[2]));
			}else {
				_marryDate.setHours(0,0,0,0);
			}
			
			countTime = { 
				Year:0,
				Month:0,
				FullMonth:0,
				Day:0,
				FullDay:0,
				Hour:0,
				FullHour:0,
				Min:0,
				FullMin:0,
				Sec:0,
				FullSec:0,
				Ms:0,
				FullMs:0
				};
		}
		
		/**
		 * 开始计时
		 * @param	TextSets
		 */
		public static function startTimer(...TextSets):void {
			_textSets = TextSets;
			var oldT:Number = Number(_marryDate);
			var nowT:Number = Number(new Date());
			setTimer(oldT - nowT);
		}
		
		/**
		 * 获取结婚日期
		 * @param	format
		 * @return
		 */
		public static function getMarryDate():Date {
			return _marryDate;
		}
		
		/**
		 * 清除计时器
		 */
		public static function clearTimer():void {
			stopTimer();
			_textSets = null;
		}
		
		/**
		 * 设置计时器
		 * @param	lessTime
		 */
		private static function setTimer(lessTime:Number):void {
			if (lessTime > 0) {
				countTime.FullMs = lessTime;
				TimerBox.addEventListener(TimerEvent.TIMER, downTimer);
			}else {
				countTime.FullMs = Math.abs(lessTime);
				TimerBox.addEventListener(TimerEvent.TIMER, upTimer);
			}
			count();
			TimerBox.start();
		}
		
		/**
		 * 停止计时器
		 */
		private static function stopTimer():void {
			TimerBox.removeEventListener(TimerEvent.TIMER, downTimer);
			TimerBox.removeEventListener(TimerEvent.TIMER, upTimer);
			TimerBox.stop();
		}
		
		/**
		 * 正计时
		 * @param	e
		 */
		private static function upTimer(e:TimerEvent):void {
			countTime.Sec += 1;
			check(true);
			layout(true);
		}
		
		/**
		 * 倒计时
		 * @param	e
		 */
		private static function downTimer(e:TimerEvent):void {
			countTime.Sec -= 1;
			check(false);
			layout(false);
		}
		
		/**
		 * 检查时间超出
		 */
		private static function check(countType:Boolean = false):void {
			
			if (countType) {
				countTime.FullMs += 1000;
				if (countTime.Sec >= 60) {
					countTime.Sec = 0;
					countTime.Min += 1;
				}
				if (countTime.Min >= 60) {
					countTime.Min = 0;
					countTime.Hour += 1;

				}
				if (countTime.Hour >= 24) {
					countTime.Hour = 0;
					countTime.Day += 1;

				}
				if (countTime.Day >= 30) {
					countTime.Day = 0;
					countTime.Month += 1;

				}
				if (countTime.Month >= 12) {
					countTime.Month = 0;
					countTime.Year += 1;

				}
				count();
			}else {
				if (countTime.Sec == 0 && countTime.Min==0 && countTime.Hour==0 && countTime.Day==0 && countTime.Month==0 &&  countTime.Year==0) {
					refresh();
					return;
				}
				countTime.FullMs -= 1000;
				
				if (countTime.Sec < 0) {
					countTime.Sec = 59;
					countTime.Min -= 1;

				}
				if (countTime.Min < 0) {
					countTime.Min = 59;
					countTime.Hour -= 1;
				}
				if (countTime.Hour < 24) {
					countTime.Hour = 23;
					countTime.Day -= 1;

				}
				if (countTime.Day < 30) {
					countTime.Day = 29;
					countTime.Month -= 1;

				}
				if (countTime.Month < 12) {
					countTime.Month = 11;
					countTime.Year -= 1;

				}
				count();
			}
			
			
		}
		
		/**
		 * 换算时间
		 */
		private static function count():void {
			
			countTime.FullSec = Math.floor(countTime.FullMs / 1000);
			countTime.FullMin = Math.floor(countTime.FullSec / 60);
			countTime.FullHour = Math.floor(countTime.FullMin / 60);
			countTime.FullDay = Math.floor(countTime.FullHour / 24);
			countTime.FullMonth = Math.floor(countTime.FullDay / 30);
			countTime.Year = Math.floor(countTime.FullMonth / 12);
			
			countTime.Ms = countTime.FullMs - (countTime.FullSec * 1000);
			countTime.Sec = countTime.FullSec - (countTime.FullMin * 60);
			countTime.Min = countTime.FullMin - (countTime.FullHour * 60);
			countTime.Hour = countTime.FullHour - (countTime.FullDay * 24);
			countTime.Day = countTime.FullDay - (countTime.FullMonth * 30);
			countTime.Month = countTime.FullMonth - (countTime.Year * 12);
			
		}
		
		/**
		 * 倒计时为0转正
		 */
		private static function refresh():void {
			trace("时间刷新");
			stopTimer();
			setTimer(0);
		}
		
		/**
		 * 输出文字
		 * @param	timerType	TRUE:
		 */
		private static function layout(timerType:Boolean = false):void {
			var n:int = _textSets.length;
			var a:int = 0;
			var type:int = (timerType)?1:0;
			for (a; a < n;a++ ) {
				if (int(_textSets[a][0]) == type && _textSets[a][1] is TextField && _textSets[a][2] is String) {
					_textSets[a][1].htmlText = textFormat(_textSets[a][2]);
					
				}
			}

		}
		
		/**
		 * 格式化文字
		 * @param	formatStr
		 * @return
		 */
		private static function textFormat(formatStr:String):String {
			var tmpStr:String = formatStr;
			tmpStr = tmpStr.split("YYYY").join(countTime.Year);
			tmpStr = tmpStr.split("MM").join(countTime.Month);
			tmpStr = tmpStr.split("DD").join(countTime.Day);
			tmpStr = tmpStr.split("HH").join(countTime.Hour);
			tmpStr = tmpStr.split("MIN").join(countTime.Min);
			tmpStr = tmpStr.split("SEC").join(countTime.Sec);
			
			tmpStr = tmpStr.split("FMon").join(countTime.FullMonth);
			tmpStr = tmpStr.split("FD").join(countTime.FullDay);
			tmpStr = tmpStr.split("FH").join(countTime.FullHour);
			tmpStr = tmpStr.split("FMin").join(countTime.FullMin);
			tmpStr = tmpStr.split("FS").join(countTime.FullSec);
			return tmpStr;
			
		}
		
		
	}

}