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

var EmergencymodeAssistant = Class.create({
	// constants
	EMERGENCY_MODE_POWER_ACTIVITY: "com.palm.app.phone.emergencyModeExpiration",
	POWER_SERVICE_URI: "palm://com.palm.power/com/palm/power",
	
    initialize: function(number){
        QDLogger.log( "EmergencymodeAssistant#initialize", number);
		this.appAssistant = Mojo.Controller.getAppController().assistant;
        this.eventListener = this.appAssistant.telephonyEventListener;
        this.announcer = this.appAssistant.announcer;
		if (number !== undefined) {
			this.updateRedialNumber(number);
		}
    },
    
	// assumes that this function will run once and only once upon entering
	// emergency mode.
    setup: function(){
        QDLogger.log( "EmergencymodeAssistant#setup");
		
		this.controller.get('call_emgcy_button').addEventListener(Mojo.Event.tap, this.callEmergency.bindAsEventListener(this));
        this.controller.get('exit_emgcymode_button').addEventListener(Mojo.Event.tap, this.exitEmergencyMode.bindAsEventListener(this));
		
		// if there's already an incoming call, copy the last popup's settings
		// and raise a dialog
		if (this.eventListener.incomingCallExists()) {
			this.announcer.dupeIncomingPopupToDialog();
		}
    },
	
	incomingDialogLaunch: function() {
		this.announcer.remoteDialogScene = this.controller;
		this.announcer.dupeIncomingPopupToDialog();
	},
	
	activate: function() {
		QDLogger.log( "EmergencymodeAssistant#activate");
		
		if ( this.appAssistant.emergencyModeExpirationMs > 0 ) {
			QDLogger.error( "Starting Emergency Mode timer for %j ms",this.appAssistant.emergencyModeExpirationMs);
			
			this.emergencyModeExpiresTimer = this.controller.window.setTimeout(this.exitEmergencyMode.bind(this), this.appAssistant.emergencyModeExpirationMs);
			this.controller.serviceRequest(this.POWER_SERVICE_URI, {
				method: "activityStart",
				parameters: {
					id: this.EMERGENCY_MODE_POWER_ACTIVITY,
					duration_ms: this.appAssistant.emergencyModeExpirationMs + 5000 /* good faith buffer for setTimeout margin of error */
				},
				onFailure: function(r) {
					QDLogger.error( "EmergencymodeAssistant#activate timer failure with message %j",r);
				}
			});
		}
	},
	
	deactivate: function() {
		if ( this.emergencyModeExpiresTimer ) {
			this.controller.window.clearInterval(this.emergencyModeExpiresTimer);
			this.controller.serviceRequest(this.POWER_SERVICE_URI, {
				method: "activityEnd",
				parameters: {
					id: this.EMERGENCY_MODE_POWER_ACTIVITY
				},
				onFailure: function(r) {
					QDLogger.error( "EmergencymodeAssistant#deactivate timer failure with message %j",r);
				}
			});
		}
	},
	
	cleanup: function() {
		QDLogger.log( "EmergencymodeAssistant#cleanup");
		this.announcer.remoteDialogScene = undefined;
	},
	
	updateRedialNumber: function(number) {
		QDLogger.log( "EmergencymodeAssistant::updateRedialNumber", number); 
		this.redialNumber = number;
	},
    
	// figures out which emergency number to call. 
	// precedence: the last dialed number, then the first emergency number in the list,
	// then 911
	getEmergencyNumber: function() {
		if (this.redialNumber && this.redialNumber !== "" && PlatformType.isEmergencyNumber(this.redialNumber)) 
			return this.redialNumber;
		else if (this.appAssistant.contacts.lastDialedNumber 
			&& this.appAssistant.contacts.lastDialedNumber.number 
			&& this.appAssistant.contacts.lastDialedNumber.number !== ""
			&& PlatformType.isEmergencyNumber(this.appAssistant.contacts.lastDialedNumber.number)) 
			return this.appAssistant.contacts.lastDialedNumber.number;
		else if (this.appAssistant.contacts.lastEmergencyNumber
			&& this.appAssistant.contacts.lastEmergencyNumber !== "")
			return this.appAssistant.contacts.lastEmergencyNumber;
		else 
			return PlatformType.getDefaultEmergencyNumber();
		
	},
	
    callEmergency: function(){
        QDLogger.log( "EmergencymodeAssistant::callEmergency");
		var number = this.getEmergencyNumber();      
	    this.appAssistant.showLimitedDialpad(this.controller.stageController, {
			"action":"fill","number":number
		});
    },
    
    exitEmergencyMode: function(){
        QDLogger.error( "EmergencymodeAssistant::exitEmergencyMode");
        TelephonyCommands.exitEmergencyMode();
        this.appAssistant.closeEmergencyStage();
    },
    
 
    
});

