/* Copyright 2009 Palm, Inc.  All rights reserved. */

var RingAssistant = Class.create({
	displayUri: 'palm://com.palm.display/control',
	initialize: function(params){
		this.appControl = Mojo.Controller.getAppController();
		this.appAssistant = this.appControl.assistant;
		
		this.alarmOff = this.alarmOff.bindAsEventListener(this);
		this.snooze = this.snooze.bindAsEventListener(this);
		this.onNameTap = this.onNameTap.bindAsEventListener(this);
		this.stageName = "ring";
		this.alarm = params.alarm;
		this.dnastSub = undefined;
	},
	
		
	setup: function() {
		this.controller.get('close_button').addEventListener(Mojo.Event.tap, this.alarmOff);
		this.controller.get('snooze_button').addEventListener(Mojo.Event.tap, this.snooze);
		this.controller.get('alarm_name').textContent = this.alarm.title;
		this.controller.get('alarm_time').textContent = this.alarm.niceTime;
		
		this.controller.get('message').addEventListener(Mojo.Event.tap, this.onNameTap);
		
		this.controller.listen(this.controller.document, Mojo.Event.deactivate, this.onBlur.bind(this));
		
		this.controller.stageController.indicateNewContent(true);
		
		this.displayLockOn();
	},
	
	alarmOff: function() {
		this.dismissAlarm = true;
		this.exit();
	},
	
	snooze: function() {
		this.exit();
	},

	exit: function() {
		Mojo.Controller.appController.closeStage(this.stageName);
	},
	
	ring: function() {
		// NOP
	},
	
	// schedules next alarm firing
	cleanup: function() {	
		this.displayOff();
		if(this.dismissAlarm){
			this.alarm.alarmScheduleNext(this.updateAppAlarmScenes.bind(this));
		} else if (this.shortSnooze) {
			this.alarm.snooze(true /* short snooze */);
		} else {
			this.alarm.snooze();
		}
	},
	
	updateAppAlarmScenes: function(){
		this.appAssistant.updateAlarmDashboard();
		var alarmStage = this.appControl.getStageProxy('clock');
		alarmStage.delegateToSceneAssistant('onAlarmSet');
		
	},
	
	// snoozes when interrupted by another popup
	onBlur: function() {
		
		this.shortSnooze = true;
		this.exit();
	},
	
	onNameTap: function() {
		this.appAssistant.launchAlarmList();
	}, 
	
	// turn display on and lock it there
	displayLockOn: function() {
		Mojo.Log.error('display on');
		this.displayOn();
		if (!this.dnastSub) {
			this.dnastSub = this.displayChangeBlockWithTimeout(undefined);
		}
	},
	
	// unlock display
	displayOff: function() {
		Mojo.Log.error('display off');
		if(this.dnastSub) {
			this.dnastSub.cancel();
			delete this.dnastSub;	
		}
	},

    displayDNAST: function(enable, callback) {
		Mojo.Log.error('creating request');
        var request = new Mojo.Service.Request(this.displayUri, {
            method: 'setProperty',
            parameters: {
                'requestBlock': enable,
                'client': "clock"
            },
            onSuccess: function(payload){
                if (callback) callback(payload);
            },
            onFailure: function() {
                Mojo.Log.error( "Clock::displayDNAST", "failure");
            }
        });

        return request;
    },
	
	// turns the screen on
	displayOn: function() {
		Mojo.Log.info( "displayOn");
		this.displayOnRequest = new Mojo.Service.Request('palm://com.palm.display', {
				method: 'control/setState',
				parameters: {state: "on"},
				onSuccess: function(response){
					Mojo.Log.info("displayOn", "you are success.  a winner is you.");
				}			,
				onFailure: function(response){
					Mojo.Log.info("displayOn", "we are fail");
				}				
    	});	
		
	},

    displayChangeBlockWithTimeout: function(callback) {
        var req = this.displayDNAST(true, callback);
        if (req) {
            req = this.cancelRequestAfterTimeout(req, 60 * 1000);
        }
        return req;
    },

	cancelRequestAfterTimeout: function(request, timeout) {
	        var r = {};
	        if (request) {
	            r.req = request;
	            r.cancel = function() {
					Mojo.Log.error('cancelling request');
	                if (this.req) {
	                    this.req.cancel();
	                    clearTimeout(r.timer);
	                    delete this.req;
	                }
	            }

	            r.timer = setTimeout(function() {
	                r.cancel();
	            }, timeout);
	        }

	        return r;

	    }
	
});
	