﻿package comp.AnalogClock
{	
	import comp.AnalogClock.AnalogClockFace; 
	import flash.events.Event;
	import flash.utils.Timer;
	import flash.geom.ColorTransform;
	
	public class AlarmClock extends SimpleClock 
	{
		
			
		public var alarmTime:Date;
		
		public var alarmMessage:String;
		private var cnt:Number=0
		
		public static const ALARM:String = "alarm";
		
		/**
		 * The Timer that will be used for the alarm.
		 */
		public var alarmTimer:Timer;
		
		public static var MILLISECONDS_PER_DAY:Number = 1000 * 60 * 60 * 12;
		
		
		public function setColors(foreColor, backColor):void{
			var myForeColorTransform:ColorTransform = new ColorTransform();
			myForeColorTransform.color = foreColor;
			face.hand_mc.transform.colorTransform = myForeColorTransform
			
			var myBackColorTransform:ColorTransform = new ColorTransform();
			myBackColorTransform.color = backColor;
			face.bg.transform.colorTransform = myBackColorTransform
		}
		
		/**
		 * Instantiates a new AlarmClock of a given size
		 */
		public override function initClock(faceSize:Number = 200):void
		{
			super.initClock(faceSize);
			
			this.addEventListener("OnAlarm", onAlarm);
			alarmTimer = new Timer(0, 1);
			
        }
		public function DrawClock(direction,hh,mm,ss):void
		{
			face.Direction=direction
			face.setCurrentTime(hh,mm,ss);
			face.draw();
		}

        /**
         * Sets the time at which the alarm should go off.
         * @param hour		The hour portion of the alarm time
         * @param minutes	The minutes portion of the alarm time
         * @param message	The message to display when the alarm goes off.
         * @return The time at which the alarm will go off.
         */
        public function setAlarm(hour:Number,minutes:Number,seconds:Number,message):Date
        {
        	this.alarmMessage = message;
        	
         	var now:Date = new Date();
        	
        	// create this time on today's date
        	//alarmTime = new Date(now.fullYear, now.month, now.date, hour, minutes);
        	
        	// determine if the specified time has already passed today
        	/*if (alarmTime <= now)
        	{
        		alarmTime.setTime(alarmTime.time + MILLISECONDS_PER_DAY);
        	}*/
			
			var miliseconds=0
			
			if(hour!=0)
			{
				 miliseconds=miliseconds+((((hour*60))*60)*1000)
			}
			
			if(minutes!=0)
			{
				miliseconds=miliseconds+((minutes*60)*1000);
			}
        	miliseconds=miliseconds+(seconds*1000)
			//trace("miliseconds before "+miliseconds+"face.Direction"+face.Direction+""+AlarmClock.MILLISECONDS_PER_DAY);
			/*if(face.Direction=="anticlockwise")
			{
				miliseconds=AlarmClock.MILLISECONDS_PER_DAY-miliseconds
			}*/
        	// reset the alarm timer if it's currently set
        	alarmTimer.reset();
			
			//trace("miliseconds"+miliseconds);
			if(miliseconds!=0)
			{
				//alarmTimer = new Timer(0, 1);
				//alarmTimer.delay=miliseconds
        		//alarmTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onAlarm);
				//alarmTimer.addEventListener(TimerEvent.TIMER, onTimer);
				//alarmTimer.delay = miliseconds//Math.max(1000, alarmTime.time - now.time);
				//trace("alarmTimer.delay"+alarmTimer.delay);
				//alarmTimer.start();
				SetAlarm((miliseconds/1000));
				
			}
			
        	
			return alarmTime;
        }

		public function onTimer(evt:Event)
		{
			cnt++;
			//trace("onTimer!"+cnt);
		}
		/**
		 * Called when the Timer event is received
		 */
		public function onAlarm(e:Event):void 
		{
			trace("Alarm!");
			//var alarm:AlarmEvent = new AlarmEvent(this.alarmMessage);
			alarmTimer.stop();
			super.StopTimer();
			this.dispatchEvent(new Event(ALARM));
		}
	}
}